langchain - 💡(How to fix) Fix bug: LLM response .choices[0] accessed without empty-list guard

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Several files in the langchain codebase access response.choices[0] or response.choices[0].message without first checking whether the choices list is empty. When an LLM provider returns an empty choices list (content filtering, quota, streaming edge cases), this produces an IndexError instead of a meaningful error.

Error Message

Several files in the langchain codebase access response.choices[0] or response.choices[0].message without first checking whether the choices list is empty. When an LLM provider returns an empty choices list (content filtering, quota, streaming edge cases), this produces an IndexError instead of a meaningful error.

Root Cause

Several files in the langchain codebase access response.choices[0] or response.choices[0].message without first checking whether the choices list is empty. When an LLM provider returns an empty choices list (content filtering, quota, streaming edge cases), this produces an IndexError instead of a meaningful error.

Fix Action

Fix

Add an empty-choices guard before each unguarded access. A PR implementing this fix is available at #37516 — requesting maintainer review.

Code Example

# Bad — crashes with IndexError if choices is empty
text = response.choices[0].text
content = response.choices[0].message.content

# Good
if not response.choices:
    raise ValueError("LLM returned empty response")
text = response.choices[0].text
RAW_BUFFERClick to expand / collapse

Description

Several files in the langchain codebase access response.choices[0] or response.choices[0].message without first checking whether the choices list is empty. When an LLM provider returns an empty choices list (content filtering, quota, streaming edge cases), this produces an IndexError instead of a meaningful error.

Affected patterns

# Bad — crashes with IndexError if choices is empty
text = response.choices[0].text
content = response.choices[0].message.content

# Good
if not response.choices:
    raise ValueError("LLM returned empty response")
text = response.choices[0].text

Reproducible

Any LLM call that triggers content filtering (e.g., OpenAI content_filter finish_reason) will return choices=[]. Azure OpenAI and Gemini 2.5 Flash also return empty choices on some safety events.

Fix

Add an empty-choices guard before each unguarded access. A PR implementing this fix is available at #37516 — requesting maintainer review.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

langchain - 💡(How to fix) Fix bug: LLM response .choices[0] accessed without empty-list guard