Skip to content

Official Codebase of 3DCodeVerse

Python

models: gemini's 'max_output_tokens exhausted by thinking' error names finish_reason=MAX_TOKENS — the planner's grow-the-budget retry matches that string and never fired for the case it was written for (bug B7)

gaoypeng5c9531d56254
diff --git a/harness/codeverse3d/models/gemini.py b/harness/codeverse3d/models/gemini.py
index 35c6585b..fbba4c12 100644
--- a/harness/codeverse3d/models/gemini.py
+++ b/harness/codeverse3d/models/gemini.py
@@ -172,10 +172,11 @@ def extract_candidate(
if finish == "MAX_TOKENS":
# Gemini 3 models always think (budget 0 is accepted but ignored); a tiny
# max_output_tokens is eaten by thoughts. Retrying cannot help — and this is
- # the most expensive failure on Gemini 3: every thought token was billed.
+ # the most expensive failure on Gemini 3: every thought token was billed. The
+ # finish reason stays in the text: the planner's grow-the-budget retry keys on it.
thoughts = resp.usage_metadata.thoughts_token_count if resp.usage_metadata else None
raise ModelError(
- f"Gemini produced no content: max_output_tokens exhausted by thinking "
+ f"Gemini produced no content (finish_reason=MAX_TOKENS): max_output_tokens exhausted by thinking "
f"(thoughts_tokens={thoughts}); raise max_output_tokens",
retryable=False, usage=usage,
)
diff --git a/harness/tests/models/test_gemini.py b/harness/tests/models/test_gemini.py
index 93470154..6274a0c5 100644
--- a/harness/tests/models/test_gemini.py
+++ b/harness/tests/models/test_gemini.py
@@ -25,6 +25,7 @@ from codeverse3d.models.gemini import (
to_contents,
)
from codeverse3d.models.retry import KeyPool, failure_outcome
+from codeverse3d.tracks.planner import _truncated
PNG_1PX = base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
@@ -366,6 +367,7 @@ def test_max_output_tokens_eaten_by_thinking_carries_the_thought_tokens():
with pytest.raises(ModelError) as e:
m.generate(ChatRequest(messages=[ChatMessage.user("x")]))
assert "exhausted by thinking" in str(e.value)
+ assert _truncated(e.value), "the planner's grow-the-budget retry must fire on exactly this case"
assert e.value.usage.thoughts_tokens == 8000 and e.value.usage.cost_usd > 0