fix: guard choices access before indexing in LLM API calls (10 files) - #1057
fix: guard choices access before indexing in LLM API calls (10 files)#1057qizwiz wants to merge 2 commits into
Conversation
LLM APIs (OpenAI-compatible) can return empty choices lists on content filtering, rate-limit errors, or streaming edge cases. Add an explicit `if not <response>.choices` check before `choices[0]` access in 10 files across groq templates, end-to-end examples, and getting-started scripts. Found by pact static analysis (llm_response_unguarded mode). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Hi @qizwiz! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Gemini 2.5 Flash returns HTTP 200 with choices[0].message=None when its safety filter triggers (finish_reason='content_filter: PROHIBITED_CONTENT'). The previous guard `if not response.choices` catches the empty-list case but misses this: one choice is present but the message object is None, causing AttributeError: 'NoneType' object has no attribute 'content'. Reproduced live against Gemini 2.5 Flash — crash confirmed in unguarded code, silent with the comprehensive guard. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Update: guard strengthened after live reproduction (commit 7e52db5) While building a proof script, we found the original guard ( Gemini 2.5 Flash (OpenAI-compatible endpoint) returns HTTP 200 with All 10 files updated. This covers: empty choices list (any provider), null message on content filter (Gemini and potentially others). Note: still waiting on CLA confirmation before this can be reviewed — will sign at code.facebook.com/cla. |
|
@meta-cla-bot recheck |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Summary
LLM APIs (OpenAI, Groq, and OpenAI-compatible endpoints) can return an empty
choiceslist in several legitimate scenarios:n=0Accessing
response.choices[0]without a length check raisesIndexErrorin these cases, turning a recoverable API error into an unhandled crash.This PR adds an explicit
if not <response>.choicesguard before every unguardedchoices[0]access across 10 files:3p-integrations/groq/.../groq-quickstart-conversational-chatbot/main.pycontinueloop iteration3p-integrations/groq/.../presidential-speeches-rag-with-pinecone/main.pyreturn ""3p-integrations/groq/.../text-to-sql-json-mode/main.pyreturn ""3p-integrations/groq/.../verified-sql-function-calling/main.pyreturn Noneend-to-end-use-cases/Contextual-Chunking-RAG/helper.pyreturn ""end-to-end-use-cases/Multi-Modal-RAG/scripts/final_demo.pyreturnfallbackend-to-end-use-cases/github_triage/llm.pyend-to-end-use-cases/research_paper_analyzer/research_analyzer.pyreturn []end-to-end-use-cases/structured_parser/src/utils.pyraise ValueErrorgetting-started/inference/api_inference/api_inference.pyreturn ""Found by
pactstatic analysis (llm_response_unguardedmode), which uses a Z3 Fixedpoint Datalog engine to track unguarded LLM API response accesses across Python call graphs.Test plan
llm_response_unguardedviolations on patched files