Skip to content

The agent that grows with you

fix(gemini): drop deprecated thinkingBudget and sampling params for Gemini 3.6+

Dietmar446508f44b0c
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index 10a5b1814eb..9a0c21fc4e7 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -641,6 +641,19 @@ def _is_openai_default_temperature_only(model: Optional[str]) -> bool:
return bare.startswith(("gpt-5", "o1", "o3", "o4")) and not bare.startswith("gpt-5-chat")
+def _is_gemini_sampling_deprecated(model: Optional[str]) -> bool:
+ """True for Gemini 3.6+ models, where Google deprecated the sampling controls
+ (``temperature``, ``top_p``, ``top_k``): the backend ignores them today and they are
+ slated for removal. Omit ``temperature`` up front rather than send a dead (or, on some
+ surfaces, rejected) parameter. Older Gemini families (2.x, 3.0–3.5) keep it."""
+ bare = _bare_model(model)
+ match = re.match(r"^gemini-(\d+)(?:\.(\d+))?", bare)
+ if not match:
+ return False
+ major, minor = int(match.group(1)), int(match.group(2) or 0)
+ return major > 3 or (major == 3 and minor >= 6)
+
+
# Routes (host + model) that rejected ``temperature`` at runtime; the next call omits it up front
# instead of paying the 400 round-trip again (the retry alone left #51083's first call to time out).
_TEMPERATURE_REJECTED_ROUTES: set = set()
@@ -664,6 +677,9 @@ def _fixed_temperature_for_model(
if _is_openai_default_temperature_only(model):
logger.debug("Omitting temperature for %r (accepts only the default)", model)
return OMIT_TEMPERATURE
+ if _is_gemini_sampling_deprecated(model):
+ logger.debug("Omitting temperature for %r (Gemini 3.6+ deprecated sampling)", model)
+ return OMIT_TEMPERATURE
from agent.auxiliary_structured_output import _route_key
if (_route_key(provider, base_url), _bare_model(model)) in _TEMPERATURE_REJECTED_ROUTES:
logger.debug("Omitting temperature for %r (route rejected it earlier)", model)
diff --git a/agent/gemini_native_adapter.py b/agent/gemini_native_adapter.py
index 9376bf7e3d5..61e7e56966e 100644
--- a/agent/gemini_native_adapter.py
+++ b/agent/gemini_native_adapter.py
@@ -489,9 +489,14 @@ def _thinking_requests_output_headroom(thinking_config: Any) -> bool:
"""True when Gemini will spend output tokens on thinking: thought tokens bill against ``maxOutputTokens``,
so a global 4096/16384 cap can be consumed entirely by high thinking (``finishReason=MAX_TOKENS``, no answer)."""
normalized = _normalize_thinking_config(thinking_config) or {}
- budget, has_level = normalized.get("thinkingBudget"), "thinkingLevel" in normalized
if normalized.get("includeThoughts") is False:
- return has_level or bool(budget)
+ # Hidden thoughts = reasoning disabled/minimised: never ask for headroom, or a small
+ # caller budget (title generation's 64 tokens) balloons to the model ceiling. Gemini 3
+ # still carries a "low" thinkingLevel here (no off-switch), which must not re-enable the
+ # bump. (#91927)
+ return False
+ budget = normalized.get("thinkingBudget")
+ has_level = "thinkingLevel" in normalized
return bool(normalized) and not (isinstance(budget, int) and budget <= 0 and not has_level)
diff --git a/agent/transports/chat_completions.py b/agent/transports/chat_completions.py
index c84c7161caa..d4032305c12 100644
--- a/agent/transports/chat_completions.py
+++ b/agent/transports/chat_completions.py
@@ -183,13 +183,18 @@ def _build_gemini_thinking_config(model: str, reasoning_config: dict | None) ->
# ``includeThoughts: False`` only omits thought parts from the returned
# response; the model may still reason internally and bill thought
# tokens against maxOutputTokens, starving small budgets (title
- # generation's 64 tokens). Set thinkingBudget to 0 to actually disable
- # thinking on families that document it: Gemini 2.5 and 3+ (plus the
- # ``gemini-flash-latest`` alias); future majors are added only when the
- # API documents thinkingBudget for them. (#91927)
+ # generation's 64 tokens). Bound thinking as low as each family allows:
+ # Gemini 2.5 documents ``thinkingBudget: 0`` as the off switch, while
+ # Gemini 3+ deprecated that field in favour of the ``thinkingLevel``
+ # string enum (which has no "none"), so use its lowest universally
+ # supported level — "low" (3.6's "minimal" is not accepted by 3.7+ /
+ # 3.1 Pro). ``_thinking_requests_output_headroom`` keeps the small
+ # budget intact for this hidden-thoughts path. (#91927)
config: dict[str, Any] = {"includeThoughts": False}
- if normalized_model == "gemini-flash-latest" or normalized_model.startswith(("gemini-2.5-", "gemini-3")):
+ if normalized_model.startswith("gemini-2.5-"):
config["thinkingBudget"] = 0
+ elif normalized_model == "gemini-flash-latest" or normalized_model.startswith("gemini-3"):
+ config["thinkingLevel"] = "low"
return config
thinking_config: dict[str, Any] = {"includeThoughts": True}
# Gemini 2.5 takes thinkingBudget; don't guess one from coarse effort levels.
diff --git a/tests/agent/test_gemini_thinking_config.py b/tests/agent/test_gemini_thinking_config.py
index 91169643b1c..3b95049b3dc 100644
--- a/tests/agent/test_gemini_thinking_config.py
+++ b/tests/agent/test_gemini_thinking_config.py
@@ -1,9 +1,11 @@
-"""Disabling reasoning must actually stop Gemini thinking (#91927).
+"""Disabling reasoning must actually stop (or minimise) Gemini thinking (#91927).
``includeThoughts: False`` only hides thought parts; the model still reasons
internally and bills thought tokens against maxOutputTokens, starving small
-budgets (title generation's 64 tokens). ``thinkingBudget: 0`` is the real
-off switch on families that document it.
+budgets (title generation's 64 tokens). Gemini 2.5 documents ``thinkingBudget: 0``
+as the real off switch; Gemini 3+ deprecated that field in favour of the
+``thinkingLevel`` string enum (which has no "none"), so the disable path uses its
+lowest universally supported level ("low").
"""
import pytest
@@ -15,40 +17,48 @@ from agent.transports.chat_completions import (
@pytest.mark.parametrize(
- "model,expect_budget_zero",
+ "model,disabled_field",
[
- ("gemini-2.5-flash", True),
- ("gemini-3.6-flash", True),
- ("gemini-3.1-pro", True),
- ("gemini-flash-latest", True),
- ("gemini-1.5-flash", False), # pre-2.5: thinkingBudget undocumented
+ ("gemini-2.5-flash", ("thinkingBudget", 0)), # 2.5 still documents thinkingBudget
+ ("gemini-3.6-flash", ("thinkingLevel", "low")), # 3+ deprecated thinkingBudget → thinkingLevel
+ ("gemini-3.1-pro", ("thinkingLevel", "low")),
+ ("gemini-flash-latest", ("thinkingLevel", "low")),
+ ("gemini-1.5-flash", None), # pre-2.5: thinking control undocumented
],
)
-def test_disabled_reasoning_zeroes_thinking_budget_where_supported(model, expect_budget_zero):
+def test_disabled_reasoning_bounds_thinking_where_supported(model, disabled_field):
for reasoning in ({"enabled": False}, {"effort": "none"}):
config = _build_gemini_thinking_config(model, reasoning)
assert config is not None
assert config.get("includeThoughts") is False
- assert (config.get("thinkingBudget") == 0) is expect_budget_zero
- if not expect_budget_zero:
+ if disabled_field is None:
assert "thinkingBudget" not in config
+ assert "thinkingLevel" not in config
+ else:
+ field, value = disabled_field
+ assert config.get(field) == value
+ other = "thinkingLevel" if field == "thinkingBudget" else "thinkingBudget"
+ assert other not in config
-def test_enabled_reasoning_never_zeroes_budget_and_non_gemini_gets_nothing():
- # Enabled reasoning must not be silently strangled by a zero budget.
+def test_enabled_reasoning_never_bounds_thinking_and_non_gemini_gets_nothing():
+ # Enabled reasoning must not be silently strangled by a zero budget / low level.
for reasoning in ({"enabled": True}, {"effort": "medium"}):
config = _build_gemini_thinking_config("gemini-2.5-flash", reasoning)
assert config is not None
assert config.get("includeThoughts") is True
assert "thinkingBudget" not in config
+ assert "thinkingLevel" not in config
# Non-Gemini models on the same provider 400 on the field entirely (#17426).
assert _build_gemini_thinking_config("gpt-4o", {"enabled": False}) is None
assert _build_gemini_thinking_config("gemma-2b", {"enabled": False}) is None
-def test_snake_case_translation_carries_thinking_budget():
+def test_snake_case_translation_carries_thinking_controls():
translated = _snake_case_gemini_thinking_config({"includeThoughts": False, "thinkingBudget": 0})
assert translated == {"include_thoughts": False, "thinking_budget": 0}
+ translated = _snake_case_gemini_thinking_config({"includeThoughts": False, "thinkingLevel": "low"})
+ assert translated == {"include_thoughts": False, "thinking_level": "low"}
translated = _snake_case_gemini_thinking_config({"includeThoughts": False})
assert translated == {"include_thoughts": False}
diff --git a/tests/agent/test_unsupported_temperature_retry.py b/tests/agent/test_unsupported_temperature_retry.py
index 28babcdcc44..41d7d54b195 100644
--- a/tests/agent/test_unsupported_temperature_retry.py
+++ b/tests/agent/test_unsupported_temperature_retry.py
@@ -56,6 +56,19 @@ def test_openai_default_only_families_omit_temperature_up_front(model):
assert _build_call_kwargs("openai-api", accepts, [], temperature=0.1)["temperature"] == 0.1
[email protected]("model", ["gemini-3.6-flash", "google/gemini-3.7-flash", "gemini-3.8-flash", "gemini-4.0-flash"])
+def test_gemini_36_plus_omits_temperature_up_front(model):
+ """Gemini 3.6+ deprecated the sampling controls (temperature/top_p/top_k); omit temperature up
+ front instead of sending a dead (or, on some surfaces, rejected) parameter. Older Gemini
+ families (2.x, 3.0-3.5) keep the caller's value."""
+ assert _fixed_temperature_for_model(model) is OMIT_TEMPERATURE
+ kwargs = _build_call_kwargs("gemini", model, [{"role": "user", "content": "hi"}], temperature=0.3)
+ assert "temperature" not in kwargs
+ for keeps in ("gemini-2.5-flash", "gemini-3.5-flash-lite", "gemini-3-pro", "gemini-3.1-pro"):
+ assert _fixed_temperature_for_model(keeps) is None
+ assert _build_call_kwargs("gemini", keeps, [], temperature=0.3)["temperature"] == 0.3
+
+
def test_route_that_rejected_temperature_omits_it_next_call():
"""#51083: after one ``unsupported_value`` on temperature the route+model is remembered and the
next call sends a single request without it; a different model on the route is unaffected."""
diff --git a/tests/fixtures/provider_wire_snapshot.json b/tests/fixtures/provider_wire_snapshot.json
index 325e4a78b53..a47bf5f2f57 100644
--- a/tests/fixtures/provider_wire_snapshot.json
+++ b/tests/fixtures/provider_wire_snapshot.json
@@ -6099,7 +6099,7 @@
"extra_body": {
"thinking_config": {
"includeThoughts": false,
- "thinkingBudget": 0
+ "thinkingLevel": "low"
}
},
"messages": [
@@ -11534,7 +11534,7 @@
"google": {
"thinking_config": {
"include_thoughts": false,
- "thinking_budget": 0
+ "thinking_level": "low"
}
}
}