Skip to content

Build and run agents you can see, understand and trust.

fix(gemini): remove unsupported $schema from tool parameters (#2277)

qbc495d2876f348
diff --git a/src/agentscope/model/_gemini/_model.py b/src/agentscope/model/_gemini/_model.py
index 35e47e5ea..a4fee3791 100644
--- a/src/agentscope/model/_gemini/_model.py
+++ b/src/agentscope/model/_gemini/_model.py
@@ -28,6 +28,7 @@ def _sanitize_schema_for_gemini(schema: Any) -> Any:
Gemini API does not support certain JSON Schema constructs. This
function removes or rewrites the following:
+ - ``$schema``: removed entirely.
- ``additionalProperties``: removed entirely.
- ``const``: converted to an equivalent single-value ``enum``,
since Gemini's ``Schema`` model does not support ``const``.
@@ -55,6 +56,9 @@ def _sanitize_schema_for_gemini(schema: Any) -> Any:
schema = dict(schema)
+ # Gemini's Schema model does not support the JSON Schema dialect marker.
+ schema.pop("$schema", None)
+
# Gemini (and many third-party proxies) reject `null` as a standalone
# functionDeclaration property type. Some MCP servers emit
# {"type": "null"} directly (not wrapped in anyOf) for parameters that
diff --git a/tests/model_gemini_test.py b/tests/model_gemini_test.py
index a95bd7415..1de053cf4 100644
--- a/tests/model_gemini_test.py
+++ b/tests/model_gemini_test.py
@@ -578,6 +578,30 @@ class TestGeminiFormatTools(unittest.TestCase):
self.assertEqual(fmt_tools, _FT_TOOLS_GEMINI)
self.assertIsNone(fmt_choice)
+ def test_schema_keyword_removed_from_parameters(self) -> None:
+ """The unsupported `$schema` keyword is removed before the call."""
+ tools = [
+ {
+ "type": "function",
+ "function": {
+ "name": "search",
+ "description": "Search for text",
+ "parameters": {
+ "$schema": "https://example.com/schema.json",
+ "type": "object",
+ "properties": {
+ "query": {"type": "string"},
+ },
+ },
+ },
+ },
+ ]
+
+ fmt_tools, _ = self.model._format_tools(tools, None)
+ parameters = fmt_tools[0]["function_declarations"][0]["parameters"]
+
+ self.assertNotIn("$schema", parameters)
+
# ---------------------------------------------------------------------------
# Tests for _sanitize_schema_for_gemini and _flatten_json_schema
@@ -587,6 +611,31 @@ class TestGeminiFormatTools(unittest.TestCase):
class TestGeminiSchemaUtils(unittest.TestCase):
"""Tests for _sanitize_schema_for_gemini and _flatten_json_schema."""
+ def test_sanitize_removes_schema_keyword_recursively(self) -> None:
+ """The unsupported `$schema` keyword is removed recursively."""
+ result = _sanitize_schema_for_gemini(
+ {
+ "$schema": "https://example.com/schema.json",
+ "type": "object",
+ "properties": {
+ "query": {
+ "$schema": "https://example.com/schema.json",
+ "type": "string",
+ },
+ },
+ },
+ )
+
+ self.assertEqual(
+ result,
+ {
+ "type": "object",
+ "properties": {
+ "query": {"type": "string"},
+ },
+ },
+ )
+
def test_sanitize_removes_additional_properties_and_inlines_optional(
self,
) -> None: