Fix /analyze 502 on complex products: raise Gemini token caps so thinking can't truncate JSON#23
Merged
Bhagat-Atul merged 1 commit intoJul 12, 2026
Conversation
gemini-2.5-flash counts internal thinking tokens against max_output_tokens. The 700/512 caps let thinking consume the whole budget on complex labels, truncating the structured JSON mid-string and returning HTTP 502 (model_response_unparseable) from /analyze; /chat had the same latent risk. Raise defaults to 4096 (analysis) and 2048 (chat) so thinking + answer both fit. Env-var overrides still work; README updated. Verified live on Cloud Run: a 15-ingredient product that previously 502'd now returns NOVA 4 with correct markers/allergens, and a complex /chat question returns a scoped answer. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
POST /analyzereturns 502model_response_unparseablefor complex / ultra-processed products, while simple inputs succeed. Reproduced live on Cloud Run (ultraprocessed-ai-proxy,us-east1).Root cause
gemini-2.5-flashcounts internal thinking tokens againstmax_output_tokens. With the current caps (GEMINI_MAX_OUTPUT_TOKENS=700,GEMINI_CHAT_MAX_OUTPUT_TOKENS=512) a complex label makes the model spend the whole budget thinking, leaving almost nothing for the JSON answer, so it is truncated mid-string andjson.loadsraises:/chat(512 cap) has the same latent risk.Fix
Raise defaults to
4096(analysis) and2048(chat) so thinking + answer both fit. Env-var overrides still work; README table updated. If a pathological label ever still truncates, bound thinking withtypes.ThinkingConfig(thinking_budget=N)(noted in a code comment).Verification (live on Cloud Run,
us-east1)/chatquestion with history: 200, correct scan-scoped answer.🤖 Generated with Claude Code