Skip to content

Fix #604 Chat Completion model raises runtime error when response.choices is empty - #935

Merged
seratch merged 3 commits into
mainfrom
issue-604-chat-completions-runtime-error
Jun 25, 2025
Merged

Fix #604 Chat Completion model raises runtime error when response.choices is empty#935
seratch merged 3 commits into
mainfrom
issue-604-chat-completions-runtime-error

Conversation

@seratch

@seratch seratch commented Jun 25, 2025

Copy link
Copy Markdown
Member

This pull request resolves #604; just checking the existence of the data and avoiding the runtime exception should make sense.

@seratch
seratch requested a review from rm-openai June 25, 2025 01:15
@seratch seratch added bug Something isn't working feature:chat-completions labels Jun 25, 2025
@seratch

seratch commented Jun 25, 2025

Copy link
Copy Markdown
Member Author

now this is ready for reviews

Comment thread src/agents/models/openai_chatcompletions.py Outdated
@seratch
seratch force-pushed the issue-604-chat-completions-runtime-error branch from 951b9c7 to 403aeea Compare June 25, 2025 23:26
@seratch
seratch merged commit 94f8035 into main Jun 25, 2025
5 checks passed
@seratch
seratch deleted the issue-604-chat-completions-runtime-error branch June 25, 2025 23:28
gn00295120 added a commit to gn00295120/openai-agents-python that referenced this pull request Oct 22, 2025
Add defensive checks before accessing response.choices[0] to prevent
IndexError when Gemini or other providers return an empty choices array.

This follows the same pattern as PR openai#935 which fixed the identical issue
in openai_chatcompletions.py.

Changes:
- Add null checks for response.choices before array access
- Return empty output when choices array is empty
- Preserve usage information even when choices is empty
- Add appropriate type annotations for litellm types
gn00295120 added a commit to gn00295120/openai-agents-python that referenced this pull request Oct 24, 2025
Add defensive checks before accessing response.choices[0] to prevent
IndexError when Gemini or other providers return an empty choices array.

This follows the same pattern as PR openai#935 which fixed the identical issue
in openai_chatcompletions.py.

Changes:
- Add null checks for response.choices before array access
- Return empty output when choices array is empty
- Preserve usage information even when choices is empty
- Add appropriate type annotations for litellm types
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