Skip to content

Commit 1a4261a

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: Fix issue with MCP tools throwing an error
Fixes: #3082 PiperOrigin-RevId: 824620839
1 parent 36ca4f1 commit 1a4261a

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

src/google/adk/tools/_gemini_schema_util.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,29 @@ 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-
):
78+
if not schema:
8179
schema["type"] = "object"
8280
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"
81+
types_no_null = [t for t in schema["type"] if t != "null"]
82+
nullable = len(types_no_null) != len(schema["type"])
83+
if "array" in types_no_null:
84+
non_null_type = "array"
85+
else:
86+
non_null_type = types_no_null[0] if types_no_null else "object"
9287
if nullable:
9388
schema["type"] = [non_null_type, "null"]
9489
else:
9590
schema["type"] = non_null_type
9691
elif schema.get("type") == "null":
9792
schema["type"] = ["object", "null"]
9893

94+
schema_type = schema.get("type")
95+
is_array = schema_type == "array" or (
96+
isinstance(schema_type, list) and "array" in schema_type
97+
)
98+
if is_array and "items" not in schema:
99+
schema["items"] = {"type": "string"}
100+
99101
return schema
100102

101103

tests/unittests/tools/test_gemini_schema_util.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ def test_to_gemini_schema_array_string_types(self):
6868
"object_nullable": {"type": "null"},
6969
"multi_types_nullable": {"type": ["string", "null", "integer"]},
7070
"empty_default_object": {},
71+
"empty_list_type": {"type": []},
72+
"multi_type_with_array_nullable": {
73+
"type": ["string", "array", "null"]
74+
},
75+
"multi_type_with_array_nonnullable": {"type": ["integer", "array"]},
7176
},
7277
}
7378
gemini_schema = _to_gemini_schema(openapi_schema)
@@ -93,6 +98,23 @@ def test_to_gemini_schema_array_string_types(self):
9398
assert gemini_schema.properties["empty_default_object"].type == Type.OBJECT
9499
assert gemini_schema.properties["empty_default_object"].nullable is None
95100

101+
assert gemini_schema.properties["empty_list_type"].type == Type.OBJECT
102+
assert not gemini_schema.properties["empty_list_type"].nullable
103+
104+
assert (
105+
gemini_schema.properties["multi_type_with_array_nullable"].type
106+
== Type.ARRAY
107+
)
108+
assert gemini_schema.properties["multi_type_with_array_nullable"].nullable
109+
110+
assert (
111+
gemini_schema.properties["multi_type_with_array_nonnullable"].type
112+
== Type.ARRAY
113+
)
114+
assert not gemini_schema.properties[
115+
"multi_type_with_array_nonnullable"
116+
].nullable
117+
96118
def test_to_gemini_schema_nested_objects(self):
97119
openapi_schema = {
98120
"type": "object",
@@ -137,6 +159,20 @@ def test_to_gemini_schema_nested_array(self):
137159
gemini_schema = _to_gemini_schema(openapi_schema)
138160
assert gemini_schema.items.properties["name"].type == Type.STRING
139161

162+
def test_to_gemini_schema_array_without_items_gets_default(self):
163+
openapi_schema = {"type": "array"}
164+
gemini_schema = _to_gemini_schema(openapi_schema)
165+
assert gemini_schema.type == Type.ARRAY
166+
assert not gemini_schema.nullable
167+
assert gemini_schema.items.type == Type.STRING
168+
169+
def test_to_gemini_schema_nullable_array_without_items_gets_default(self):
170+
openapi_schema = {"type": ["array", "null"]}
171+
gemini_schema = _to_gemini_schema(openapi_schema)
172+
assert gemini_schema.type == Type.ARRAY
173+
assert gemini_schema.nullable
174+
assert gemini_schema.items.type == Type.STRING
175+
140176
def test_to_gemini_schema_any_of(self):
141177
openapi_schema = {
142178
"anyOf": [{"type": "string"}, {"type": "integer"}],
@@ -533,8 +569,10 @@ def test_sanitize_schema_formats_for_gemini_nullable(self):
533569
"type": "string",
534570
},
535571
"next_page_token": {
536-
"anyOf": [{"type": "string"}, {"type": "null"}],
537-
"default": None,
572+
"any_of": [
573+
{"type": "string"},
574+
{"type": ["object", "null"]},
575+
],
538576
"description": (
539577
"The nextPageToken to fetch the next page of results."
540578
),

0 commit comments

Comments
 (0)