Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(gemini): sanitize const schema field for Gemini tool parameters (#2016)
qbc3256750a85be
diff --git a/src/agentscope/model/_gemini/_model.py b/src/agentscope/model/_gemini/_model.pyindex 3c7f1fc6f..f221ea470 100644--- a/src/agentscope/model/_gemini/_model.py+++ b/src/agentscope/model/_gemini/_model.py@@ -29,6 +29,8 @@ def _sanitize_schema_for_gemini(schema: Any) -> Any:function removes or rewrites the following:- ``additionalProperties``: removed entirely.+ - ``const``: converted to an equivalent single-value ``enum``,+ since Gemini's ``Schema`` model does not support ``const``.- ``anyOf`` containing a ``{"type": "null"}`` entry: simplified tothe single non-null type. If there is exactly one non-nullalternative it is inlined directly; otherwise the ``anyOf`` is@@ -56,6 +58,12 @@ def _sanitize_schema_for_gemini(schema: Any) -> Any:# Remove additionalProperties — not supported by Geminischema.pop("additionalProperties", None)+ # Convert `const` into an equivalent single-value `enum` — Gemini's+ # Schema model does not support the `const` keyword.+ if "const" in schema:+ const_value = schema.pop("const")+ schema.setdefault("enum", [const_value])+# Simplify anyOf that only differs by a null type, e.g. Optional[X]if "anyOf" in schema and isinstance(schema["anyOf"], list):any_of = schema["anyOf"]diff --git a/tests/model_gemini_test.py b/tests/model_gemini_test.pyindex 5788f6684..c5dd3d9ad 100644--- a/tests/model_gemini_test.py+++ b/tests/model_gemini_test.py@@ -600,6 +600,35 @@ class TestGeminiSchemaUtils(unittest.TestCase):},)+ def test_sanitize_converts_const_to_enum(self) -> None:+ """`const` is converted into a single-value `enum` for Gemini."""+ self.assertEqual(+ _sanitize_schema_for_gemini(+ {+ "type": "object",+ "properties": {+ "topic": {"type": "string", "const": "general"},+ "query": {+ "type": "string",+ "description": "Search query",+ },+ },+ "required": ["query"],+ },+ ),+ {+ "type": "object",+ "properties": {+ "topic": {"type": "string", "enum": ["general"]},+ "query": {+ "type": "string",+ "description": "Search query",+ },+ },+ "required": ["query"],+ },+ )+def test_flatten_resolves_ref_and_removes_defs(self) -> None:"""$ref inlined with extra keys merged; $defs removed."""self.assertEqual(