Skip to content

fix(avatar): prevent thinking-token truncation dropping the color-scheme block - #1556

Merged
dolho merged 1 commit into
devfrom
fix/avatar-refinement-thinking-token-truncation
Jul 10, 2026
Merged

dolho merged 1 commit into
devfrom
fix/avatar-refinement-thinking-token-truncation

Conversation

@vybe

@vybe vybe commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Problem

Agent avatars were coming out wildly inconsistent and off-palette — not following the color scheme.

Root cause: Avatar consistency lives entirely in a fixed technical block that the refinement model (GEMINI_TEXT_MODEL) is asked to append to every prompt — background #1a1f2e/#111827, indigo rim #6366f1, head-and-shoulders framing, 85mm lens, 5600K key light. The refinement call in _call_gemini_text capped maxOutputTokens at 512, but since #1130 the default model is gemini-3.5-flash, a thinking model whose reasoning phase draws from the output budget. Reasoning consumed ~491 of 512 tokens, truncating the refined prompt (finishReason=MAX_TOKENS) to a ~90-char fragment that dropped the entire technical block. The image model then received only a bare subject description → inconsistent, off-scheme avatars.

This was a silent regression from #1130: the previous default gemini-2.0-flash was non-thinking, so all 512 tokens went to visible output and the block survived.

Fix (src/backend/services/image_generation_service.py)

Verification

  • End-to-end against the live model: all 6 technical-block tokens now survive refinement (#1a1f2e, #111827, #6366f1, head-and-shoulders, 85mm, 5600K) — was 0/6 before.
  • Unit tests: 3 new regression tests + full suite green (43 passed).
tests/unit/test_image_generation_service.py::TestCallGeminiText::test_disables_thinking_and_has_output_headroom PASSED
tests/unit/test_image_generation_service.py::TestCallGeminiText::test_retries_without_thinking_config_on_400 PASSED
tests/unit/test_image_generation_service.py::TestCallGeminiText::test_persistent_400_still_raises PASSED

Scope / notes

  • Code-only change (no schema) — no dual migration needed.
  • Existing already-generated avatars are cached image files; they'll benefit only on regeneration (POST /api/agents/avatars/generate-defaults or per-agent Regenerate).
  • Follow-up (not in this PR): consistency still relies on an LLM copying a verbatim block, which is inherently brittle (the social use case was also observed leaking markdown). A more durable design would append the fixed technical block in code after refinement. Happy to do that separately.

🤖 Generated with Claude Code

…eme block

Avatar refinement runs each identity prompt through GEMINI_TEXT_MODEL to append
a fixed technical block (background #1a1f2e/#111827, indigo rim #6366f1,
head-and-shoulders framing, 85mm lens, 5600K key) — the sole source of avatar
color scheme and cross-generation consistency.

The refinement call capped maxOutputTokens at 512. Since #1130 the default model
is gemini-3.5-flash, a *thinking* model whose reasoning phase draws from the
output budget. Reasoning consumed ~491 of 512 tokens, truncating the refined
prompt (finishReason=MAX_TOKENS) to a ~90-char fragment that dropped the entire
technical block. Avatars were then generated from a bare subject description —
wildly inconsistent and off-palette.

Fix in _call_gemini_text:
- Disable thinking (thinkingConfig.thinkingBudget=0) — refinement is a
  deterministic rewrite that needs no chain-of-thought.
- Raise maxOutputTokens 512 -> 4096 as headroom even when thinking stays on.
- Retry once WITHOUT thinkingConfig on HTTP 400: a thinking-mandatory model
  (e.g. gemini-2.5-pro) rejects budget=0, and refine_prompt silently falls back
  to the raw prompt on error, so an unhandled 400 would re-break avatars.

Verified end-to-end against the live model: all 6 technical-block tokens now
survive refinement (was 0/6). Adds 3 regression tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@dolho dolho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review — LGTM, one non-blocking suggestion

Root-cause writeup is excellent and the fix is correct: disabling thinking (thinkingBudget=0) + raising the cap to 4096 addresses both the squeeze and the headroom, and the 400-retry gracefully covers thinking-mandatory models. Nice touch that the same retry also makes it robust for a non-thinking model that rejects an unknown thinkingConfig field. Test coverage is solid — happy path, 400-retry, and persistent-400-still-raises all guarded.

Non-blocking suggestion — the silent-truncation class isn't fully closed. _call_gemini_text still returns candidates[0].content.parts[0].text without inspecting finishReason. The 4096 headroom makes truncation unlikely, but on the retry path (thinking on, for a thinking-mandatory model) reasoning still draws from the output budget — if it ever eats 4096, you get the exact original failure again: a truncated fragment that drops the AVATAR technical block, returned silently and passed to the image model.

Consider making a future recurrence loud rather than silent — e.g. after parsing:

cand = data["candidates"][0]
if cand.get("finishReason") == "MAX_TOKENS":
    raise RuntimeError("Gemini text refinement truncated (MAX_TOKENS)")

refine_prompt already falls back to the raw prompt on a raise, so this degrades no worse than today but turns a silent off-palette avatar into a caught, logged failure. (Alternatively, assert the refined text still contains a sentinel from the fixed block.) Not a merge blocker — the headroom + thinking-off almost certainly suffices for the default model.

@dolho
dolho merged commit 86660f5 into dev Jul 10, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants