From 6a04ea67c0e75ef1218bf4ceaac071c2f16a0701 Mon Sep 17 00:00:00 2001 From: Max Freedom Pollard <272618364+MaxFreedomPollard@users.noreply.github.com> Date: Fri, 4 Sep 2026 13:54:14 -0400 Subject: [PATCH] fix(gemini): collapse array-typed tool schemas instead of crashing translation The enum-compatibility check evaluated `[...] in {...}` on an array `type`, raising TypeError: unhashable type: 'list' and aborting translation of the whole tool catalog rather than the one offending tool. Addresses both review points: - Reuses tools.schema_sanitizer._normalize_type_array instead of picking the first non-null member, so a real union becomes an anyOf of single-type branches and no branch is dropped. - Derives the type outside the key loop and sets nullable after it, so the flag implied by "null" in the array beats an input nullable: false whichever key the producer emitted first. Both orders are pinned by a parametrized test. --- agent/gemini_schema.py | 22 ++++++ ...MaxFreedomPollard@users.noreply.github.com | 2 + tests/agent/test_gemini_schema.py | 74 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 contributors/emails/272618364+MaxFreedomPollard@users.noreply.github.com diff --git a/agent/gemini_schema.py b/agent/gemini_schema.py index 41b4a3e03f..6822558837 100644 --- a/agent/gemini_schema.py +++ b/agent/gemini_schema.py @@ -5,6 +5,8 @@ from __future__ import annotations import math from 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 = None for 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`` wins else: 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/272618364+MaxFreedomPollard@users.noreply.github.com b/contributors/emails/272618364+MaxFreedomPollard@users.noreply.github.com new file mode 100644 index 0000000000..420636af43 --- /dev/null +++ b/contributors/emails/272618364+MaxFreedomPollard@users.noreply.github.com @@ -0,0 +1,2 @@ +MaxFreedomPollard +# PR #55643 diff --git a/tests/agent/test_gemini_schema.py b/tests/agent/test_gemini_schema.py index 075e620cea..7c2d5f7350 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"]}