Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(brain): recover Gemini's parameterless thinking_config 400 — realtime fallback un-bricked

Personal Jarvis Maintainerefce12d4770e
diff --git a/jarvis/plugins/brain/gemini.py b/jarvis/plugins/brain/gemini.py
index 2dec34a34f..f188ba4b58 100644
--- a/jarvis/plugins/brain/gemini.py
+++ b/jarvis/plugins/brain/gemini.py
@@ -118,6 +118,23 @@ def _is_thinking_config_rejected_error(exc: Exception) -> bool:
return "invalid_argument" in msg or "invalid argument" in msg
+def _is_generic_invalid_argument_error(exc: Exception) -> bool:
+ """True for Gemini's parameterless 400 INVALID_ARGUMENT rejection.
+
+ Some models reject ``thinking_config.thinking_budget=0`` with the bare
+ "Request contains an invalid argument." — no field name, no "thinking"
+ token — so the targeted predicate above never matches (live 2026-07-21:
+ ``gemini-3.6-flash`` answered every delegated realtime turn this way and
+ the whole tier died unrecovered). The message names nothing, so blame is
+ assigned by the retry itself: dropping ``thinking_config`` and having the
+ request accepted is the capability probe (AP-21).
+ """
+ msg = str(exc).lower()
+ if "400" not in msg:
+ return False
+ return "invalid_argument" in msg or "invalid argument" in msg
+
+
def _to_gemini_contents(
messages: tuple[BrainMessage, ...],
tool_name_map: dict[str, str] | None = None,
@@ -536,6 +553,11 @@ class GeminiBrain:
# thinking" Gemini 3.x does. ``None`` = SDK default (auto, higher
# latency). ``0`` = off, ``-1`` = dynamic, ``>0`` = fixed cap.
self._thinking_budget = thinking_budget
+ # 2026-07-21: some models reject ``thinking_config`` with a
+ # parameterless 400 ("Request contains an invalid argument."). Once
+ # a retry without the field proves that blame, remember it for this
+ # instance so later turns skip the doomed extra round trip.
+ self._thinking_config_unsupported = False
# Latency-Sprint-2: context-cache name (lazily created on the first
# call with system+tools). Key: (system_hash, tools_hash) → cache_name.
# ``_cached_content_name``/``_cache_signature`` hold the most recently
@@ -752,7 +774,10 @@ class GeminiBrain:
# step failed "unterminated JSON" with thoughts=304 of
# max_tokens=320. An explicit constructor budget always wins.
effective_thinking_budget = 0
- if effective_thinking_budget is not None:
+ if (
+ effective_thinking_budget is not None
+ and not self._thinking_config_unsupported
+ ):
try:
from google.genai import types as _genai_types
config_dict["thinking_config"] = _genai_types.ThinkingConfig(
@@ -861,6 +886,10 @@ class GeminiBrain:
# the from-scratch retry yields no duplicate chunks.
attempt = 0
yielded_delta = False
+ # Set when a parameterless 400 forced a retry without
+ # ``thinking_config``; promoted to the instance flag only once the
+ # retried request is accepted (that acceptance IS the blame proof).
+ pending_thinking_config_blame = False
while True:
attempt += 1
try:
@@ -951,6 +980,17 @@ class GeminiBrain:
}
if final_usage is not None:
yield BrainDelta(usage=final_usage)
+ if pending_thinking_config_blame:
+ # The retry without ``thinking_config`` was accepted, so
+ # the parameterless 400 is attributed to it. Later turns
+ # on this instance skip the field (and the extra 400)
+ # entirely.
+ self._thinking_config_unsupported = True
+ log.info(
+ "Gemini model %s accepts requests only without "
+ "thinking_config — omitting it from now on",
+ self._model,
+ )
return
except Exception as exc: # noqa: BLE001 — BUG-019 stale-cache recovery
if (
@@ -969,21 +1009,37 @@ class GeminiBrain:
if tools_payload:
config_dict["tools"] = tools_payload
continue
+ thinking_config_blamed = _is_thinking_config_rejected_error(exc)
if (
not yielded_delta
and "thinking_config" in config_dict
- and _is_thinking_config_rejected_error(exc)
+ and (
+ thinking_config_blamed
+ or _is_generic_invalid_argument_error(exc)
+ )
):
# Some models REQUIRE thinking mode and 400 on budget=0
# ("Budget 0 is invalid. This model only works in thinking
- # mode."). Capability recovery, not a model-name pin
- # (AP-21): drop the config and retry once. Safe: the
- # rejection fires before any token is generated.
+ # mode."); others answer the bare "Request contains an
+ # invalid argument." (live 2026-07-21: gemini-3.6-flash —
+ # it bricked every delegated realtime fallback turn).
+ # Capability recovery, not a model-name pin (AP-21): drop
+ # the config and retry once. Safe: the rejection fires
+ # before any token is generated. If something else caused
+ # the parameterless 400, the retry fails the same way and
+ # the error still propagates — one extra round trip, no
+ # behavior change.
log.info(
"Gemini model %s rejected thinking_config — retrying "
"once without it: %s", self._model, exc,
)
config_dict.pop("thinking_config", None)
+ if thinking_config_blamed:
+ # The message names the thinking config — no further
+ # evidence needed.
+ self._thinking_config_unsupported = True
+ else:
+ pending_thinking_config_blame = True
continue
if (
not yielded_delta
diff --git a/tests/unit/brain/test_gemini_reasoning_effort.py b/tests/unit/brain/test_gemini_reasoning_effort.py
index 0c4aa1e892..436c1b832e 100644
--- a/tests/unit/brain/test_gemini_reasoning_effort.py
+++ b/tests/unit/brain/test_gemini_reasoning_effort.py
@@ -47,10 +47,12 @@ class _FakeGeminiClient:
"400 INVALID_ARGUMENT. Budget 0 is invalid. This model only "
"works in thinking mode."
),
+ reject_thinking_generic: bool = False,
) -> None:
self.calls: list[dict[str, Any]] = []
self.reject_thinking = reject_thinking
self.reject_message = reject_message
+ self.reject_thinking_generic = reject_thinking_generic
self.aio = SimpleNamespace(models=self)
async def generate_content_stream(
@@ -63,6 +65,17 @@ class _FakeGeminiClient:
self.calls.append(dict(config))
if self.reject_thinking and config.get("thinking_config") is not None:
raise RuntimeError(self.reject_message)
+ if (
+ self.reject_thinking_generic
+ and config.get("thinking_config") is not None
+ ):
+ # The parameterless rejection shape gemini-3.6-flash produced
+ # live 2026-07-21 — no field name, no "thinking" token.
+ raise RuntimeError(
+ "400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': "
+ "'Request contains an invalid argument.', 'status': "
+ "'INVALID_ARGUMENT'}}"
+ )
async def _stream() -> AsyncIterator[Any]:
yield SimpleNamespace(
@@ -174,3 +187,28 @@ async def test_generic_invalid_argument_recovers_without_the_field() -> None:
assert fake.calls[0].get("thinking_config") is not None
assert "thinking_config" not in fake.calls[1]
assert any(d.content for d in deltas), "recovered stream must yield text"
+
+
+async def test_parameterless_400_recovers_and_is_remembered() -> None:
+ """Live 2026-07-21: ``gemini-3.6-flash`` rejects ``thinking_budget=0``
+ with the bare "Request contains an invalid argument." — no "thinking" in
+ the message. The recovery must (a) retry once without ``thinking_config``
+ and yield the answer, and (b) once that retry is accepted, skip the field
+ on every later turn so each delegated realtime turn doesn't pay a doomed
+ extra round trip."""
+ provider = GeminiBrain(model="gemini-3.6-flash")
+ fake = _FakeGeminiClient(reject_thinking_generic=True)
+ provider._client = fake # type: ignore[assignment]
+
+ deltas = await _drain(provider.complete(_request("none")))
+
+ assert len(fake.calls) == 2
+ assert fake.calls[0].get("thinking_config") is not None
+ assert "thinking_config" not in fake.calls[1]
+ assert any(d.content for d in deltas), "recovered stream must yield text"
+
+ # Second turn: the blame is proven — no thinking_config, no extra 400.
+ await _drain(provider.complete(_request("none")))
+ assert len(fake.calls) == 3
+ assert "thinking_config" not in fake.calls[2]