The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gemini): wrap schema-bearing tool results as opaque text
Aintworth03477166f965
diff --git a/agent/gemini_native_adapter.py b/agent/gemini_native_adapter.pyindex 01e14c2c58e..61624b2ac5f 100644--- a/agent/gemini_native_adapter.py+++ b/agent/gemini_native_adapter.py@@ -360,6 +360,26 @@ def _translate_tool_call_to_gemini(return part+def _looks_like_json_schema(node: Any) -> bool:+ """True if a parsed value contains a JSON-Schema-style ``$ref`` pointer.++ Gemini 3 resolves ``$ref``/``$defs`` references inside a+ functionResponse.response payload and rejects unknown pointers with+ HTTP 400 INVALID_ARGUMENT. A tool result that is itself a JSON Schema+ (e.g. the output of ``tool_describe`` for an MCP tool) must therefore be+ forwarded as opaque text rather than as a structured response.+ """+ if isinstance(node, dict):+ for key, value in node.items():+ if key == "$ref" and isinstance(value, str) and value.startswith("#/"):+ return True+ if _looks_like_json_schema(value):+ return True+ elif isinstance(node, list):+ return any(_looks_like_json_schema(item) for item in node)+ return False++def _translate_tool_result_to_gemini(message: Dict[str, Any],tool_name_by_call_id: Optional[Dict[str, str]] = None,@@ -382,6 +402,14 @@ def _translate_tool_result_to_gemini(parsed = json.loads(content) if content.strip().startswith(("{", "[")) else Noneexcept json.JSONDecodeError:parsed = None+ # Gemini 3 resolves JSON-Schema ``$ref`` pointers inside a+ # functionResponse.response payload and rejects unknown references with+ # HTTP 400 INVALID_ARGUMENT ("referenced name '#/$defs/...' does not match+ # a display_name"; see vercel/ai#14369). A tool result that is itself a+ # JSON Schema (e.g. tool_describe output for an MCP tool) must therefore+ # be forwarded as opaque text, not as a structured response.+ if isinstance(parsed, dict) and _looks_like_json_schema(parsed):+ parsed = Noneresponse = parsed if isinstance(parsed, dict) else {"output": content}function_response: Dict[str, Any] = {"name": name,diff --git a/tests/agent/test_gemini_native_adapter.py b/tests/agent/test_gemini_native_adapter.pyindex 4db7344e0d1..95406a638fc 100644--- a/tests/agent/test_gemini_native_adapter.py+++ b/tests/agent/test_gemini_native_adapter.py@@ -131,6 +131,55 @@ def test_consecutive_user_messages_merge_for_gemini_alternation():assert roles == ["user", "model"], roles+def test_schema_bearing_tool_result_is_wrapped_as_opaque_text():+ """A tool result whose content is itself a JSON Schema must not be+ forwarded as a structured functionResponse.response.++ Gemini 3 resolves ``$ref``/``$defs`` pointers inside a function response+ payload and rejects unknown references with HTTP 400 INVALID_ARGUMENT+ ("referenced name '#/$defs/...' does not match a display_name"; see+ vercel/ai#14369). ``tool_describe`` output for an MCP tool is exactly such+ a schema, so it must be wrapped as opaque text instead.+ """+ from agent.gemini_native_adapter import _translate_tool_result_to_gemini++ schema = {+ "$defs": {"SetCookieParam": {"type": "object"}},+ "properties": {"cookies": {"$ref": "#/$defs/SetCookieParam"}},+ }+ msg = {+ "role": "tool",+ "tool_call_id": "call_1",+ "name": "tool_describe",+ "content": json.dumps(schema),+ }++ out = _translate_tool_result_to_gemini(msg, include_ids=True)++ response = out["functionResponse"]["response"]+ assert "$defs" not in response+ assert "output" in response+ # The raw schema text is preserved verbatim in the wrapped output.+ assert "#/$defs/SetCookieParam" in response["output"]+++def test_plain_json_tool_result_remains_structured():+ """Ordinary JSON tool results without a ``$ref`` pointer keep the+ structured form (no regression to the existing structured-response path)."""+ from agent.gemini_native_adapter import _translate_tool_result_to_gemini++ msg = {+ "role": "tool",+ "tool_call_id": "call_2",+ "name": "some_tool",+ "content": json.dumps({"status": "ok", "count": 3}),+ }++ out = _translate_tool_result_to_gemini(msg)++ assert out["functionResponse"]["response"] == {"status": "ok", "count": 3}++def test_translate_native_response_surfaces_reasoning_and_tool_calls():