Repository navigation
fix(avatar): prevent thinking-token truncation dropping the color-scheme block - #1556
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
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_textcappedmaxOutputTokensat 512, but since #1130 the default model isgemini-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-flashwas non-thinking, so all 512 tokens went to visible output and the block survived.Fix (
src/backend/services/image_generation_service.py)thinkingConfig.thinkingBudget=0) — prompt refinement is a deterministic rewrite that needs no chain-of-thought.maxOutputTokens512 → 4096 — headroom so the block survives even when thinking stays on.thinkingConfigon HTTP 400 — a thinking-mandatory model (e.g.gemini-2.5-pro, in bug: Telegram voice transcription + image-gen prompt refinement fail — hardcoded retired Gemini model gemini-2.0-flash returns 404 #1130's model list) rejectsbudget=0with a 400. Sincerefine_promptsilently falls back to the raw prompt on any error (the exact trap that hid this bug), an unhandled 400 would re-break avatars on that model.Verification
#1a1f2e,#111827,#6366f1,head-and-shoulders,85mm,5600K) — was 0/6 before.43 passed).Scope / notes
POST /api/agents/avatars/generate-defaultsor per-agent Regenerate).socialuse 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