Skip to content

The agent that grows with you

fix(agent): skip corrupted Gemini thought signatures

KoNit-Kaf4a3eba0a36
diff --git a/agent/transports/chat_completions.py b/agent/transports/chat_completions.py
index 2e4a6d95e19..de56b9fa8d7 100644
--- a/agent/transports/chat_completions.py
+++ b/agent/transports/chat_completions.py
@@ -220,6 +220,23 @@ def _model_consumes_thought_signature(model: Any) -> bool:
return "gemini" in m or "gemma" in m
+def _has_replayable_thought_signature(extra_content: Any) -> bool:
+ """Whether OpenRouter's Gemini sidecar contains a usable thought signature.
+
+ Gemini accepts the signature either directly or under its ``google``
+ namespace. Replaying an empty or non-string value makes a multimodal
+ request fail with ``Corrupted thought signature``; omit that sidecar while
+ leaving the stored history untouched.
+ """
+ if not isinstance(extra_content, dict):
+ return False
+ candidate = extra_content.get("thought_signature")
+ google = extra_content.get("google")
+ if candidate is None and isinstance(google, dict):
+ candidate = google.get("thought_signature")
+ return isinstance(candidate, str) and bool(candidate.strip())
+
+
def _attr_or_model_extra(obj: Any, name: str) -> Any:
"""``obj.<name>``, else the same key from pydantic ``model_extra`` (some SDKs park fields there)."""
value = getattr(obj, name, None)
@@ -332,7 +349,9 @@ def _sanitize_message(msg: Any, strip_extra_content: bool) -> dict | None:
if not isinstance(tc, dict):
continue
keys = [k for k in _STRIP_TC_KEYS if k in tc]
- if strip_extra_content and "extra_content" in tc:
+ if "extra_content" in tc and (
+ strip_extra_content or not _has_replayable_thought_signature(tc["extra_content"])
+ ):
keys.append("extra_content")
if keys:
if copied_tool_calls is None:
diff --git a/tests/agent/test_provider_parity.py b/tests/agent/test_provider_parity.py
index 5deba0ebcd1..5d89af1500d 100644
--- a/tests/agent/test_provider_parity.py
+++ b/tests/agent/test_provider_parity.py
@@ -202,6 +202,68 @@ class TestBuildApiKwargsOpenRouter:
"google": {"thought_signature": "opaque"}
}
+ def test_gemini_image_request_drops_corrupted_thought_signature(self, monkeypatch):
+ """An empty Gemini signature must not be replayed with an image request.
+
+ OpenRouter can return a signature container without the actual signature.
+ Google rejects that request as a corrupted thought signature instead of
+ processing the image.
+ """
+ agent = _make_agent(monkeypatch, "openrouter", model="google/gemini-3.8-flash")
+ messages = [
+ {"role": "user", "content": "use a tool first"},
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [{
+ "id": "call_123",
+ "type": "function",
+ "function": {"name": "terminal", "arguments": "{}"},
+ "extra_content": {"google": {"thought_signature": ""}},
+ }],
+ },
+ {"role": "tool", "tool_call_id": "call_123", "content": "ok"},
+ {"role": "user", "content": [
+ {"type": "text", "text": "describe this image"},
+ {"type": "image_url", "image_url": {"url": "data:image/png;base64,AA=="}},
+ ]},
+ ]
+
+ kwargs = agent._build_api_kwargs(messages)
+
+ assert "extra_content" not in kwargs["messages"][1]["tool_calls"][0]
+ assert messages[1]["tool_calls"][0]["extra_content"] == {
+ "google": {"thought_signature": ""}
+ }
+
+ def test_gemini_image_request_keeps_nonempty_thought_signature(self, monkeypatch):
+ """A valid signature remains available for the required tool-call replay."""
+ agent = _make_agent(monkeypatch, "openrouter", model="google/gemini-3.8-flash")
+ messages = [
+ {"role": "user", "content": "use a tool first"},
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [{
+ "id": "call_123",
+ "type": "function",
+ "function": {"name": "terminal", "arguments": "{}"},
+ "extra_content": {"google": {"thought_signature": "signed-call"}},
+ }],
+ },
+ {"role": "tool", "tool_call_id": "call_123", "content": "ok"},
+ {"role": "user", "content": [
+ {"type": "text", "text": "describe this image"},
+ {"type": "image_url", "image_url": {"url": "data:image/png;base64,AA=="}},
+ ]},
+ ]
+
+ kwargs = agent._build_api_kwargs(messages)
+
+ assert kwargs["messages"][1]["tool_calls"][0]["extra_content"] == {
+ "google": {"thought_signature": "signed-call"}
+ }
+
def test_gemini_native_passes_base_url_for_top_level_thinking_config(self, monkeypatch):
agent = _make_agent(
monkeypatch,
@@ -921,4 +983,3 @@ class TestReasoningEffortDefaults:
assert kwargs["reasoning"]["effort"] == "medium"
-