fix(vertex): preserve items on array branches inside anyOf with null

convert_anyof_null_to_nullable was stripping the items field from array
branches inside anyOf when a sibling null branch was present, leaving
{"type": "array"} without items. Vertex requires items whenever
type == "array" (even inside anyOf) and rejects the call with
INVALID_ARGUMENT.

Leave the (possibly empty) items in place so the downstream process_items
step can convert {} to {"type": "object"}, which is what Vertex wants.

Also:
- Update test_build_vertex_schema expected output, which was codifying
  the broken shape.
- Convert test_gemini_tool_calling_not_working to a hermetic mock test
  that asserts the request body sent to Vertex includes items inside
  the callbacks anyOf array branch. The previous form made a real
  network call and was flaky in CI.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Yuneng Jiang 2026-04-27 23:37:09 -07:00
parent 62920a0cb2
commit 3ca985451e
3 changed files with 74 additions and 12 deletions

View File

@ -710,14 +710,10 @@ def convert_anyof_null_to_nullable(schema, depth=0):
if contains_null:
# set all types to nullable following guidance found here: https://cloud.google.com/vertex-ai/generative-ai/docs/samples/generativeaionvertexai-gemini-controlled-generation-response-schema-3#generativeaionvertexai_gemini_controlled_generation_response_schema_3-python
# Empty `items: {}` on array branches is left in place; downstream
# process_items() converts it to {"type": "object"}, which Vertex
# requires whenever type == "array" (even inside anyOf).
for atype in anyof:
# Remove items field if type is array and items is empty
if (
atype.get("type") == "array"
and "items" in atype
and not atype["items"]
):
atype.pop("items")
atype["nullable"] = True
properties = schema.get("properties", None)

View File

@ -3569,8 +3569,14 @@ def test_gemini_tool_calling_working_demo():
def test_gemini_tool_calling_not_working():
load_vertex_ai_credentials()
litellm._turn_on_debug()
"""
Regression test: tool params with anyOf containing both an empty-items
array branch and a null branch must serialize with items present on the
array branch (Vertex rejects array types missing `items`).
"""
from litellm.llms.custom_httpx.http_handler import HTTPHandler
from litellm.llms.vertex_ai.vertex_llm_base import VertexBase
args = {
"messages": [
{
@ -3637,8 +3643,64 @@ def test_gemini_tool_calling_not_working():
],
"vertex_location": "global",
}
response = completion(model="vertex_ai/gemini-3-flash-preview", **args)
print(response)
client = HTTPHandler()
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.headers = {"Content-Type": "application/json"}
mock_response.json.return_value = {
"candidates": [
{
"content": {
"role": "model",
"parts": [{"text": "Hello!"}],
},
"finishReason": "STOP",
}
],
"usageMetadata": {
"promptTokenCount": 10,
"candidatesTokenCount": 5,
"totalTokenCount": 15,
},
}
with (
patch.object(client, "post", return_value=mock_response) as mock_post,
patch.object(
VertexBase,
"_ensure_access_token",
return_value=("fake-token", "fake-project"),
),
):
completion(
model="vertex_ai/gemini-3-flash-preview",
client=client,
**args,
)
sent_body = mock_post.call_args.kwargs.get(
"json"
) or mock_post.call_args.kwargs.get("data")
assert sent_body is not None, "expected request body to be sent"
if isinstance(sent_body, str):
sent_body = json.loads(sent_body)
function_decl = sent_body["tools"][0]["function_declarations"][0]
callbacks_schema = function_decl["parameters"]["properties"]["config"][
"properties"
]["callbacks"]
array_branches = [
branch
for branch in callbacks_schema["anyOf"]
if branch.get("type", "").lower() == "array"
]
assert array_branches, "expected an array branch in callbacks anyOf"
for branch in array_branches:
assert "items" in branch and branch["items"], (
f"array branch in callbacks.anyOf must include non-empty items "
f"(Vertex rejects array types missing items). Got: {branch}"
)
def test_vertex_ai_llama_tool_calling():

View File

@ -225,7 +225,11 @@ def test_build_vertex_schema():
"metadata": {"type": "object"},
"callbacks": {
"anyOf": [
{"type": "array", "nullable": True},
{
"type": "array",
"items": {"type": "object"},
"nullable": True,
},
{"type": "object", "nullable": True},
]
},