Skip to content

fix: handle None choices in ChatCompletion response#2850

Open
Spectual wants to merge 1 commit intoopenai:mainfrom
Spectual:fix/handle-none-choices-provider-error
Open

fix: handle None choices in ChatCompletion response#2850
Spectual wants to merge 1 commit intoopenai:mainfrom
Spectual:fix/handle-none-choices-provider-error

Conversation

@Spectual
Copy link
Copy Markdown

@Spectual Spectual commented Apr 7, 2026

Summary

Fixes #604

Some LLM providers (e.g. OpenRouter, proxies under rate-limit or authentication failure) return an error payload where the choices field is None rather than raising an HTTP-level error. Previously, this caused the SDK to crash with an opaque TypeError: 'NoneType' object is not subscriptable deep inside openai_chatcompletions.py, before the calling application had any chance to inspect the actual provider error.

Changes

  • Added a guard clause in OpenAIChatCompletionsModel.get_response() that checks response.choices before any access to choices[0].
  • When choices is None or empty, a ModelBehaviorError is raised with a clear message. If the response object carries an error field (as provider error payloads typically do), that information is included in the exception message.
  • This makes the failure mode explicit and catchable, allowing callers to log the real provider error or trigger retry logic.

Root cause

openai_chatcompletions.py assumed every successful API call would return at least one choice. Providers that surface errors as JSON bodies (rather than HTTP error codes) return a ChatCompletion-shaped object with choices=None, triggering the crash.

Test plan

  • Reproduce with a dummy key pointed at a provider that returns an error payload (e.g. OpenRouter with an invalid key) — confirm ModelBehaviorError is raised instead of TypeError.
  • Verify existing unit tests pass (pytest tests/).
  • Verify that normal completions (choices present) are unaffected.

…ayloads)

When a provider (e.g. OpenRouter) returns an error payload, the ChatCompletion
response object has choices=None. Previously this caused a silent no-op or a
downstream TypeError. Now a ModelBehaviorError is raised before choices[0] is
accessed, including the provider's error field in the message when present.
Copy link
Copy Markdown
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you resolve the format issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working feature:chat-completions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: SDK crashes when choices is None (provider-error payload)

2 participants