fix: guard against IndexError when LLM API returns empty choices list#1876
Open
qizwiz wants to merge 1 commit into
Open
fix: guard against IndexError when LLM API returns empty choices list#1876qizwiz wants to merge 1 commit into
qizwiz wants to merge 1 commit into
Conversation
Author
|
@microsoft-github-policy-service agree |
The OpenAI API can return an empty choices list when: - Content filtering blocks the image response - A streaming edge case closes before choices are emitted - An OpenAI-compatible provider returns a non-standard response shape In all three cases, `response.choices[0]` raises IndexError. This is a silent crash in production — content filters fire on real user images, not on dev test images, so the bug is invisible in local testing. Three affected paths: - _image_converter.py: return None when no choices (caller handles None) - _llm_caption.py: return None when no choices (caller handles None) - _ocr_service.py: inline ternary, consistent with existing `text or ""` guard already on the next line Formally verified: Z3 SMT solver proves IndexError is satisfiable under content_filtered=True → choices_len=0 (SAT), and proves the guard makes it UNSAT — no assignment produces IndexError after the check. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
5719e76 to
9f80bf3
Compare
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 places in markitdown call
response.choices[0].message.contentimmediately afterclient.chat.completions.create(...)without checking whetherchoicesis non-empty:packages/markitdown/src/markitdown/converters/_image_converter.py:138packages/markitdown/src/markitdown/converters/_llm_caption.py:50packages/markitdown-ocr/src/markitdown_ocr/_ocr_service.py:102The OpenAI API (and OpenAI-compatible providers) can return an empty
choiceslist in three documented scenarios:finish_reason: "content_filter"with an emptychoiceslistIn all three cases,
choices[0]raisesIndexError: list index out of range. This crash is silent in development (dev images don't hit content filters) and surfaces in production on real user content.Formal verification
This was found via pact static analysis and formally verified with Z3 SMT:
Bug model (SAT):
content_filtered=True → choices_len=0, access_index=0 → 0≥0 → IndexErrorFix model (UNSAT): With
if not response.choicesguard,access_attempted ∧ choices_len=0is a contradiction — IndexError is unreachable on all trigger paths.Fix
The
_ocr_service.pypath already has a bareexcept Exceptionthat returnsOCRResult(text="")on failure, so theNonepropagates safely through the existingtext.strip() if text else ""guard on the next line.Prior art
This exact crash pattern appears in multiple open issues across the LLM ecosystem: plastic-labs/honcho#676, aden-hive/hive#4767, TheR1D/shell_gpt#741, langchain-community#475.