The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gemini): collapse array-typed tool schemas instead of crashing translation
Max Freedom Pollard6a04ea67c0e7
diff --git a/agent/gemini_schema.py b/agent/gemini_schema.pyindex 41b4a3e03f6..68225588370 100644--- a/agent/gemini_schema.py+++ b/agent/gemini_schema.py@@ -5,6 +5,8 @@ from __future__ import annotationsimport mathfrom typing import Any, Dict+from tools.schema_sanitizer import _normalize_type_array+# Gemini's ``FunctionDeclaration.parameters`` accepts only a subset of OpenAPI 3.0 /# JSON Schema (the ``Schema`` object); everything else is stripped._GEMINI_SCHEMA_ALLOWED_KEYS = {@@ -30,6 +32,7 @@ def sanitize_gemini_schema(schema: Any) -> Dict[str, Any]:if not isinstance(schema, dict):return {}cleaned: Dict[str, Any] = {}+ type_array: Any = Nonefor key, value in schema.items():if key not in _GEMINI_SCHEMA_ALLOWED_KEYS:continue@@ -41,9 +44,28 @@ def sanitize_gemini_schema(schema: Any) -> Dict[str, Any]:elif key == "anyOf":if isinstance(value, list):cleaned[key] = [sanitize_gemini_schema(item) for item in value if isinstance(item, dict)]+ elif key == "type" and isinstance(value, list):+ type_array = value # normalized after the loop, so the derived ``nullable`` winselse:cleaned[key] = value+ # JSON Schema allows an array ``type`` (``["string", "null"]``, ``["string", "integer"]``).+ # Gemini's ``Schema`` accepts only a single string, and the enum check below would evaluate+ # ``[...] in {...}`` -> TypeError: unhashable type: 'list', aborting translation of the WHOLE+ # tool catalog. Reuse the sanitizer's normalization so a multi-type array becomes an ``anyOf``+ # of single-type branches (no branch dropped) rather than keeping only the first.+ if type_array is not None:+ derived: Dict[str, Any] = {}+ _normalize_type_array(type_array, derived)+ if "anyOf" in derived:+ cleaned["anyOf"] = derived["anyOf"]+ else:+ cleaned["type"] = derived["type"]+ if derived.get("nullable"):+ # Derived from "null" in the array. Set AFTER the loop so it beats an input+ # ``nullable: false`` regardless of which key the producer emitted first.+ cleaned["nullable"] = True+# Gemini requires every ``enum`` entry to be a string even for# integer/number/boolean types; the declared type stays intact and Gemini# still emits typed tool arguments at runtime. dict.fromkeys = ordered dedupe.diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]new file mode 100644index 00000000000..420636af436--- /dev/null+++ b/contributors/emails/[email protected]@@ -0,0 +1,2 @@+MaxFreedomPollard+# PR #55643diff --git a/tests/agent/test_gemini_schema.py b/tests/agent/test_gemini_schema.pyindex 075e620cea7..7c2d5f73504 100644--- a/tests/agent/test_gemini_schema.py+++ b/tests/agent/test_gemini_schema.py@@ -1,5 +1,7 @@"""Tests for agent.gemini_schema — OpenAI→Gemini tool parameter translation."""+import pytest+from agent.gemini_schema import (sanitize_gemini_schema,sanitize_gemini_tool_parameters,@@ -167,3 +169,75 @@ class TestSanitizeGeminiToolParameters:assert "1440" in aad["description"]# And the string-enum sibling is untouched.assert cleaned["properties"]["action"]["enum"] == ["create_thread"]+++class TestArrayTypeNormalization:+ """JSON Schema allows an array ``type``; Gemini's ``Schema`` accepts one string.++ The enum-compatibility check evaluated ``[...] in {...}`` on that list and raised+ ``TypeError: unhashable type: 'list'``, aborting translation of the WHOLE tool+ catalog, not just the offending tool. #55645+ """++ def test_nullable_form_collapses_and_keeps_the_enum(self):+ cleaned = sanitize_gemini_schema({"type": ["string", "null"], "enum": ["low", "high"]})+ assert cleaned["type"] == "string"+ assert cleaned["nullable"] is True+ assert cleaned["enum"] == ["low", "high"]++ def test_nullable_form_without_enum(self):+ cleaned = sanitize_gemini_schema({"type": ["integer", "null"]})+ assert cleaned["type"] == "integer"+ assert cleaned["nullable"] is True++ def test_real_union_becomes_anyof_with_every_branch_kept(self):+ """No branch may be dropped. ``tools/schema_sanitizer._normalize_type_array``+ is the canonical behaviour and is reused here rather than reimplemented."""+ cleaned = sanitize_gemini_schema({"type": ["string", "integer"]})+ assert "type" not in cleaned+ assert cleaned["anyOf"] == [{"type": "string"}, {"type": "integer"}]+ assert "nullable" not in cleaned++ def test_three_way_union_keeps_all_three(self):+ cleaned = sanitize_gemini_schema({"type": ["string", "integer", "boolean"]})+ assert cleaned["anyOf"] == [+ {"type": "string"}, {"type": "integer"}, {"type": "boolean"}]++ @pytest.mark.parametrize("schema", [+ {"nullable": False, "type": ["string", "null"]}, # nullable emitted first+ {"type": ["string", "null"], "nullable": False}, # nullable emitted last+ ])+ def test_derived_nullable_beats_an_input_nullable_either_order(self, schema):+ """The array says null is permitted, so an input ``nullable: false`` is wrong.++ Deriving inside the key loop made this order-dependent: whichever key the+ producer emitted last won.+ """+ cleaned = sanitize_gemini_schema(schema)+ assert cleaned["nullable"] is True+ assert cleaned["type"] == "string"++ def test_all_null_array_becomes_the_null_type(self):+ assert sanitize_gemini_schema({"type": ["null"]})["type"] == "null"++ def test_empty_type_array_falls_back_to_object(self):+ assert sanitize_gemini_schema({"type": []})["type"] == "object"++ def test_integer_union_still_stringifies_its_enum(self):+ cleaned = sanitize_gemini_schema({"type": ["integer", "null"], "enum": [1, 2, 3]})+ assert cleaned["type"] == "integer"+ assert cleaned["enum"] == ["1", "2", "3"]++ def test_nested_property_is_normalized_too(self):+ cleaned = sanitize_gemini_schema({+ "type": "object",+ "properties": {"color": {"type": ["string", "null"], "enum": ["red", "green"]}},+ })+ color = cleaned["properties"]["color"]+ assert color["type"] == "string" and color["nullable"] is True+ assert color["enum"] == ["red", "green"]++ def test_plain_string_type_is_untouched(self):+ """Control: the non-array path must be unchanged."""+ cleaned = sanitize_gemini_schema({"type": "string", "enum": ["a"]})+ assert cleaned == {"type": "string", "enum": ["a"]}