Skip to content

The agent that grows with you

test(gemini): cover nested/list/non-pointer ref cases; document false-positive tolerance

Aintworthce51f535d33a
diff --git a/agent/gemini_native_adapter.py b/agent/gemini_native_adapter.py
index 61624b2ac5f..fa517605612 100644
--- a/agent/gemini_native_adapter.py
+++ b/agent/gemini_native_adapter.py
@@ -368,6 +368,13 @@ def _looks_like_json_schema(node: Any) -> bool:
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.
+
+ Detection is deliberately structural, not semantic: any ``$ref`` value
+ shaped like a JSON pointer (``#/...``) demotes the whole result. Non-schema
+ data that happens to carry such a pointer is a false positive, but the raw
+ content is preserved verbatim either way, so the cost is fidelity-free.
+ The recursive walk is O(n) over the parsed value; tool-result payloads are
+ small, so this is negligible per turn.
"""
if isinstance(node, dict):
for key, value in node.items():
diff --git a/tests/agent/test_gemini_native_adapter.py b/tests/agent/test_gemini_native_adapter.py
index 95406a638fc..3adf1aa7e1e 100644
--- a/tests/agent/test_gemini_native_adapter.py
+++ b/tests/agent/test_gemini_native_adapter.py
@@ -180,6 +180,67 @@ def test_plain_json_tool_result_remains_structured():
assert out["functionResponse"]["response"] == {"status": "ok", "count": 3}
+def test_deeply_nested_ref_is_detected():
+ """A ``$ref`` pointer buried several levels deep through mixed lists and
+ dicts still demotes the result to opaque text (recursion coverage)."""
+ from agent.gemini_native_adapter import _translate_tool_result_to_gemini
+
+ deep = {"a": [{"b": {"c": [{"$ref": "#/$defs/Deep"}]}}]}
+ msg = {
+ "role": "tool",
+ "tool_call_id": "call_3",
+ "name": "some_tool",
+ "content": json.dumps(deep),
+ }
+
+ out = _translate_tool_result_to_gemini(msg)
+
+ response = out["functionResponse"]["response"]
+ assert "output" in response
+ assert "#/$defs/Deep" in response["output"]
+
+
+def test_top_level_json_array_is_wrapped_as_opaque_text():
+ """A top-level JSON array is never forwarded as a structured response.
+
+ ``response = parsed if isinstance(parsed, dict) else {"output": content}``
+ already wraps lists, so a list of schemas cannot reach the Gemini 400 path.
+ """
+ from agent.gemini_native_adapter import _translate_tool_result_to_gemini
+
+ arr = [{"$ref": "#/$defs/SetCookieParam", "type": "object"}]
+ msg = {
+ "role": "tool",
+ "tool_call_id": "call_4",
+ "name": "some_tool",
+ "content": json.dumps(arr),
+ }
+
+ out = _translate_tool_result_to_gemini(msg)
+
+ response = out["functionResponse"]["response"]
+ assert "output" in response
+ assert "$ref" not in response
+
+
+def test_ref_value_without_pointer_prefix_remains_structured():
+ """Only values shaped like a JSON pointer (``#/...``) demote a result; a
+ ``$ref`` value that is not a pointer leaves the structured path intact."""
+ from agent.gemini_native_adapter import _translate_tool_result_to_gemini
+
+ payload = {"$ref": "not-a-pointer", "status": "ok"}
+ msg = {
+ "role": "tool",
+ "tool_call_id": "call_5",
+ "name": "some_tool",
+ "content": json.dumps(payload),
+ }
+
+ out = _translate_tool_result_to_gemini(msg)
+
+ assert out["functionResponse"]["response"] == payload
+
+
def test_translate_native_response_surfaces_reasoning_and_tool_calls():