fix: guard unguarded .choices[0] accesses to prevent IndexError on empty LLM responses#310
Open
qizwiz wants to merge 1 commit into
Open
Conversation
…nses Adds `if not response.choices: raise ValueError(...)` before every unguarded `.choices[0]` access in four files, preventing IndexError crashes when an LLM returns an empty choices list (ref issue algorithmicsuperintelligence#292). Files fixed: - optillm/plugins/deep_research/research_engine.py (7 sites) - optillm/plugins/spl/evaluation.py (2 sites) - optillm/mars/agent.py (3 sites) - optillm/plugins/coc_plugin.py (4 sites) Verified with pact_sheaf: all 16 openai-chat#choices-nonempty violations resolved (Ȟ¹ = 0, Z3 UNSAT certificate on fixed files).
|
|
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.
Summary
Fixes unguarded
.choices[0]accesses in four files that crash withIndexErrorwhen an LLM returns an emptychoiceslist. This addresses the class of errors reported in #292 (json response error — thechoiceslist can be empty on API errors, rate-limit responses, or model refusals).Each fix adds an explicit guard immediately before the first
.choices[0]access in each call-site:This turns a cryptic
IndexError: list index out of rangeinto a clear, actionable error message.Files changed
optillm/plugins/deep_research/research_engine.pyoptillm/plugins/spl/evaluation.pyoptillm/mars/agent.pyoptillm/plugins/coc_plugin.pyTotal: 16 unguarded access sites eliminated.
Verification
Fixes were verified with pact_sheaf, a sheaf-cohomological contract checker:
openai-chat#choices-nonemptyviolations resolvedBefore fix: pact reported
h1_semantic = 7, 3, 3, 3across the four files.After fix:
h1_semantic = 0, 0, 0, 0forchoices-spec violations.Test plan
pytestor equivalent)choices=[]and confirm the newValueErroris raised with a clear message instead ofIndexErrorruff checkandblack --checkon the four changed files (already applied)