Skip to content

Commit bc00e90

Browse files
committed
fix: Fix issue with MCP tools throwing an error.
The error "parameters.properties[segmentation_classes].items: missing field" indicates _to_gemini_schema isn't correctly handling array types.
1 parent 33b2d49 commit bc00e90

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

src/google/adk/tools/_gemini_schema_util.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,34 @@ def _to_snake_case(text: str) -> str:
7575

7676

7777
def _sanitize_schema_type(schema: dict[str, Any]) -> dict[str, Any]:
78-
if ("type" not in schema or not schema["type"]) and schema.keys().isdisjoint(
79-
schema
80-
):
81-
schema["type"] = "object"
82-
if isinstance(schema.get("type"), list):
83-
nullable = False
84-
non_null_type = None
85-
for t in schema["type"]:
86-
if t == "null":
87-
nullable = True
88-
elif not non_null_type:
89-
non_null_type = t
90-
if not non_null_type:
91-
non_null_type = "object"
92-
if nullable:
93-
schema["type"] = [non_null_type, "null"]
94-
else:
95-
schema["type"] = non_null_type
96-
elif schema.get("type") == "null":
97-
schema["type"] = ["object", "null"]
78+
"""Sanitizes the 'type' field and adds default 'items' for arrays."""
79+
type_val = schema.get("type")
80+
non_null_type = None
81+
nullable = False
82+
83+
# First, determine the base type and nullability
84+
if isinstance(type_val, list):
85+
nullable = "null" in type_val
86+
non_null_types = [t for t in type_val if t != "null"]
87+
non_null_type = non_null_types[0] if non_null_types else None
88+
elif type_val == "null":
89+
nullable = True
90+
else:
91+
non_null_type = type_val
92+
93+
# Default to 'object' if no other type is found
94+
if not non_null_type:
95+
non_null_type = "object"
96+
97+
# Single, consolidated check to add default 'items' for arrays
98+
if non_null_type == "array" and "items" not in schema:
99+
schema["items"] = {"type": "string"}
100+
101+
# Finally, set the schema 'type' based on nullability
102+
if nullable:
103+
schema["type"] = [non_null_type, "null"]
104+
else:
105+
schema["type"] = non_null_type
98106

99107
return schema
100108

tests/unittests/tools/test_gemini_schema_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_to_gemini_schema_array_string_types(self):
9191
assert gemini_schema.properties["multi_types_nullable"].nullable
9292

9393
assert gemini_schema.properties["empty_default_object"].type == Type.OBJECT
94-
assert gemini_schema.properties["empty_default_object"].nullable is None
94+
assert gemini_schema.properties["empty_default_object"].nullable is False
9595

9696
def test_to_gemini_schema_nested_objects(self):
9797
openapi_schema = {
@@ -148,7 +148,7 @@ def test_to_gemini_schema_any_of(self):
148148

149149
def test_to_gemini_schema_general_list(self):
150150
openapi_schema = {
151-
"type": "array",
151+
"type": "object",
152152
"properties": {
153153
"list_field": {"type": "array", "items": {"type": "string"}},
154154
},

0 commit comments

Comments
 (0)