Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(vertex): synthesize items for array types missing items entirely

Yuneng Jiang1af11d4371ac
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