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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Fix: properties: should be non-empty for OBJECT type
Sameer Kankutea0538e3d5481
diff --git a/litellm/llms/vertex_ai/common_utils.py b/litellm/llms/vertex_ai/common_utils.pyindex 6bb11430f20..03fa5b98928 100644--- a/litellm/llms/vertex_ai/common_utils.py+++ b/litellm/llms/vertex_ai/common_utils.py@@ -640,14 +640,28 @@ def add_object_type(schema):if properties is not None:if "required" in schema and schema["required"] is None:schema.pop("required", None)- schema["type"] = "object"- for name, value in properties.items():- add_object_type(value)+ # Gemini doesn't accept empty properties for object types+ # If properties is empty, remove it and the type field+ if not properties:+ schema.pop("properties", None)+ schema.pop("type", None)+ schema.pop("required", None)+ else:+ schema["type"] = "object"+ for name, value in properties.items():+ add_object_type(value)items = schema.get("items", None)if items is not None:add_object_type(items)+ for key in ["anyOf", "oneOf", "allOf"]:+ values = schema.get(key, None)+ if values is not None and isinstance(values, list):+ for value in values:+ if isinstance(value, dict):+ add_object_type(value)+def strip_field(schema, field_name: str):schema.pop(field_name, None)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.pyindex a5eee9e37b1..f850b53e12b 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@@ -984,6 +984,7 @@ async def test_vertex_ai_token_counter_routes_partner_models():to the partner models token counter instead of the Gemini token counter."""from unittest.mock import AsyncMock, patch+from litellm.llms.vertex_ai.common_utils import VertexAITokenCounterfrom litellm.types.utils import TokenCountResponse@@ -1027,6 +1028,7 @@ async def test_vertex_ai_token_counter_routes_gemini_models():to the Gemini token counter (not partner models)."""from unittest.mock import AsyncMock, patch+from litellm.llms.vertex_ai.common_utils import VertexAITokenCounterfrom litellm.types.utils import TokenCountResponse@@ -1124,3 +1126,73 @@ def test_vertex_ai_moonshot_uses_openai_handler():assert VertexAIPartnerModels.should_use_openai_handler("moonshotai/kimi-k2-thinking-maas")+++def test_build_vertex_schema_empty_properties():+ """+ Test _build_vertex_schema handles empty properties objects correctly.++ This test verifies the fix for the issue where Gemini rejects schemas+ with empty properties objects like {"properties": {}, "type": "object"}.++ Error from Gemini: "GenerateContentRequest.generation_config.response_schema+ .properties[\"action\"].items.any_of[0].properties[\"go_back\"].properties:+ should be non-empty for OBJECT type"++ The fix removes empty properties objects and their associated type/required fields.+ """+ from litellm.llms.vertex_ai.common_utils import _build_vertex_schema++ # Input: Schema with empty properties (the problematic case from real request)+ input_schema = {+ "properties": {+ "action": {+ "description": "List of actions to execute",+ "items": {+ "anyOf": [+ {+ "properties": {+ "go_back": {+ "properties": {},+ "type": "object",+ "additionalProperties": False,+ "description": "Go back",+ "required": []+ }+ },+ "required": ["go_back"],+ "type": "object",+ "additionalProperties": False+ }+ ]+ },+ "type": "array"+ }+ },+ "type": "object",+ "additionalProperties": False+ }++ # Apply the transformation+ result = _build_vertex_schema(input_schema)++ # Verify the transformation removed empty properties+ # Navigate to the go_back schema+ go_back_schema = result["properties"]["action"]["items"]["anyOf"][0]["properties"]["go_back"]++ # Verify empty properties was removed+ assert "properties" not in go_back_schema, "Empty properties should be removed"++ # Verify type was also removed (since object without properties is invalid in Gemini)+ assert "type" not in go_back_schema, "Type should be removed when properties is empty"++ # Verify required was also removed+ assert "required" not in go_back_schema, "Required should be removed when properties is empty"++ # Verify description is preserved+ assert go_back_schema.get("description") == "Go back", "Description should be preserved"++ # Verify parent schema still has proper structure+ parent_schema = result["properties"]["action"]["items"]["anyOf"][0]+ assert parent_schema["type"] == "object", "Parent schema should still have object type"+ assert "go_back" in parent_schema["properties"], "go_back should still be in parent properties"