fix: guard against IndexError on empty LLM choices/content list#3874
Open
qizwiz wants to merge 2 commits into
Open
fix: guard against IndexError on empty LLM choices/content list#3874qizwiz wants to merge 2 commits into
qizwiz wants to merge 2 commits into
Conversation
Three call sites access choices[0] or content[0] without checking whether the API returned any results — content-policy filters, token limits, and rate-limit fallbacks can all return an empty list. - api_provider.py: early return when res.choices is empty - classify/label.py: skip iteration when completion.choices is empty - classify/label.py: skip Anthropic iteration when response.content is empty - criteria_labeling.py: skip iteration when completion.choices is empty
…t-filter) Gemini 2.5 Flash returns HTTP 200 with choices[0].message=None when content is filtered (finish_reason: PROHIBITED_CONTENT), causing AttributeError even when choices is non-empty. Extends all three guards from `if not choices:` to `if not choices or choices[0].message is None:`.
Author
|
Update: extended guards to cover a second crash vector Testing against live providers revealed that Gemini 2.5 Flash returns HTTP 200 with Pushed a follow-up commit (cab948d) extending all three guards:
New guard pattern: |
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
Three files access
choices[0]orcontent[0]without first checking whether the API returned any results. An empty list can occur when:choices=[]content=[]on a refusalThis raises a bare
IndexErrorthat propagates as an unhandled exception.Changes
fastchat/serve/api_provider.pyif not res.choices: returnbefore accessingchoices[0]fastchat/serve/monitor/classify/label.pyif not completion.choices: breakbefore accessingchoices[0]fastchat/serve/monitor/classify/label.pyif not response.content: breakbefore accessing Anthropiccontent[0]fastchat/serve/monitor/criteria_labeling.pyif not completion.choices: breakbefore accessingchoices[0]All three files already use retry loops —
breakon empty response exits the loop and returns theAPI_ERROR_OUTPUTsentinel, which is the same behaviour as any other API error.No new files, no behaviour change on successful API responses.
Found by pact static analysis —
llm_response_unguardedmode.