From 3ca985451e5a416b33595a0031187cc7aa629fd0 Mon Sep 17 00:00:00 2001 From: Yuneng Jiang Date: Mon, 27 Apr 2026 23:37:09 -0700 Subject: [PATCH 1/2] 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) --- litellm/llms/vertex_ai/common_utils.py | 10 +-- .../test_amazing_vertex_completion.py | 70 +++++++++++++++++-- .../vertex_ai/test_vertex_ai_common_utils.py | 6 +- 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/litellm/llms/vertex_ai/common_utils.py b/litellm/llms/vertex_ai/common_utils.py index ccd4d4f2934..9b23520dcd2 100644 --- a/litellm/llms/vertex_ai/common_utils.py +++ b/litellm/llms/vertex_ai/common_utils.py @@ -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) diff --git a/tests/local_testing/test_amazing_vertex_completion.py b/tests/local_testing/test_amazing_vertex_completion.py index 9070a9feab5..3b4ecb82b1d 100644 --- a/tests/local_testing/test_amazing_vertex_completion.py +++ b/tests/local_testing/test_amazing_vertex_completion.py @@ -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(): diff --git a/tests/test_litellm/llms/vertex_ai/test_vertex_ai_common_utils.py b/tests/test_litellm/llms/vertex_ai/test_vertex_ai_common_utils.py index ef93375c3cd..dc3be7114f1 100644 --- a/tests/test_litellm/llms/vertex_ai/test_vertex_ai_common_utils.py +++ b/tests/test_litellm/llms/vertex_ai/test_vertex_ai_common_utils.py @@ -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}, ] }, From 1af11d4371ac5aed4c0263a2d34061c28d9e3ba3 Mon Sep 17 00:00:00 2001 From: Yuneng Jiang Date: Tue, 28 Apr 2026 09:23:55 -0700 Subject: [PATCH 2/2] fix(vertex): synthesize items for array types missing items entirely MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Companion to the prior commit. process_items only converted empty `items: {}` to `{"type": "object"}`. But anyOf branches like `{"type": "array"}` (no items field at all) were untouched, so after convert_anyof_null_to_nullable stripped the null branch and added nullable, the array branch was sent to Vertex as `{"type": "array", "nullable": true}` — which Vertex rejects with INVALID_ARGUMENT (`any_of[0].items: missing field`). Make process_items synthesize `items: {"type": "object"}` for any `type == "array"` schema where items is missing or empty. Also: - Convert test_gemini_tool_calling_working_demo to a hermetic mock test asserting items is present on the array branch in the sent body. Was previously a real-network call to Vertex and was the test the user reported still failing in CI. - Add unit test test_build_vertex_schema_array_branch_missing_items_in_anyof covering the missing-items shape directly. Co-Authored-By: Claude Opus 4.7 (1M context) --- litellm/llms/vertex_ai/common_utils.py | 9 ++- .../test_amazing_vertex_completion.py | 70 +++++++++++++++++-- .../vertex_ai/test_vertex_ai_common_utils.py | 37 ++++++++++ 3 files changed, 111 insertions(+), 5 deletions(-) diff --git a/litellm/llms/vertex_ai/common_utils.py b/litellm/llms/vertex_ai/common_utils.py index 9b23520dcd2..b4bfde5f541 100644 --- a/litellm/llms/vertex_ai/common_utils.py +++ b/litellm/llms/vertex_ai/common_utils.py @@ -597,7 +597,14 @@ def process_items(schema, depth=0): f"Max depth of {DEFAULT_MAX_RECURSE_DEPTH} exceeded while processing schema. Please check the schema for excessive nesting." ) if isinstance(schema, dict): - if "items" in schema and schema["items"] == {}: + # Vertex requires `items` whenever `type == "array"` (even inside anyOf). + # Normalize: empty `items: {}` and missing-items both become {"type": "object"}. + type_val = schema.get("type") + if ( + isinstance(type_val, str) + and type_val.lower() == "array" + and ("items" not in schema or schema.get("items") == {}) + ): schema["items"] = {"type": "object"} for key, value in schema.items(): if isinstance(value, dict): diff --git a/tests/local_testing/test_amazing_vertex_completion.py b/tests/local_testing/test_amazing_vertex_completion.py index 3b4ecb82b1d..9782bf3c2af 100644 --- a/tests/local_testing/test_amazing_vertex_completion.py +++ b/tests/local_testing/test_amazing_vertex_completion.py @@ -3493,8 +3493,14 @@ def test_litellm_api_base(monkeypatch, provider, route): def test_gemini_tool_calling_working_demo(): - load_vertex_ai_credentials() - litellm._turn_on_debug() + """ + Regression test: tool params with anyOf containing a `{"type": "array"}` + branch (no items field at all) must synthesize items before the request + is sent to Vertex (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": [ { @@ -3564,8 +3570,64 @@ def test_gemini_tool_calling_working_demo(): ], "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_gemini_tool_calling_not_working(): diff --git a/tests/test_litellm/llms/vertex_ai/test_vertex_ai_common_utils.py b/tests/test_litellm/llms/vertex_ai/test_vertex_ai_common_utils.py index dc3be7114f1..95507390df9 100644 --- a/tests/test_litellm/llms/vertex_ai/test_vertex_ai_common_utils.py +++ b/tests/test_litellm/llms/vertex_ai/test_vertex_ai_common_utils.py @@ -292,6 +292,43 @@ def test_process_items_basic(): process_items(schema) assert schema["properties"]["nested"]["items"] == {"type": "object"} + # Vertex rejects array types missing `items` entirely (not just empty). + # Synthesize {"type": "object"} so the request validates. + schema = {"type": "array"} + process_items(schema) + assert schema["items"] == {"type": "object"} + + +def test_build_vertex_schema_array_branch_missing_items_in_anyof(): + """ + Regression: an `anyOf` branch with `{"type": "array"}` (no items) must + end up with synthesized `items: {"type": "object"}` after the schema + transform — Vertex returns INVALID_ARGUMENT otherwise. + """ + from litellm.llms.vertex_ai.common_utils import _build_vertex_schema + + parameters = { + "properties": { + "callbacks": { + "anyOf": [ + {"type": "array"}, + {"type": "object"}, + {"type": "null"}, + ] + } + }, + "type": "object", + } + + result = _build_vertex_schema(parameters) + callbacks_anyof = result["properties"]["callbacks"]["anyOf"] + array_branches = [b for b in callbacks_anyof if b.get("type") == "array"] + assert array_branches, "expected an array branch to remain after transform" + for branch in array_branches: + assert branch.get("items") == { + "type": "object" + }, f"array branch must have items synthesized; got {branch}" + def test_vertex_ai_complex_response_schema(): import json