fix: handle None choices in ChatCompletion response#2850
Open
Spectual wants to merge 1 commit intoopenai:mainfrom
Open
fix: handle None choices in ChatCompletion response#2850Spectual wants to merge 1 commit intoopenai:mainfrom
Spectual wants to merge 1 commit intoopenai:mainfrom
Conversation
…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.
seratch
requested changes
Apr 7, 2026
Member
seratch
left a comment
There was a problem hiding this comment.
Can you resolve the format issue?
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 #604
Some LLM providers (e.g. OpenRouter, proxies under rate-limit or authentication failure) return an error payload where the
choicesfield isNonerather than raising an HTTP-level error. Previously, this caused the SDK to crash with an opaqueTypeError: 'NoneType' object is not subscriptabledeep insideopenai_chatcompletions.py, before the calling application had any chance to inspect the actual provider error.Changes
OpenAIChatCompletionsModel.get_response()that checksresponse.choicesbefore any access tochoices[0].choicesisNoneor empty, aModelBehaviorErroris raised with a clear message. If the response object carries anerrorfield (as provider error payloads typically do), that information is included in the exception message.Root cause
openai_chatcompletions.pyassumed every successful API call would return at least one choice. Providers that surface errors as JSON bodies (rather than HTTP error codes) return aChatCompletion-shaped object withchoices=None, triggering the crash.Test plan
ModelBehaviorErroris raised instead ofTypeError.pytest tests/).