Skip to content

Conversation

@1208nn
Copy link

@1208nn 1208nn commented Dec 8, 2025

This pull request introduces a small update to the get_completion function in sgpt/handlers/handler.py to improve robustness when handling streaming responses.

  • Added a check to skip response chunks that do not contain any choices, preventing potential errors when accessing chunk.choices[0].

Copilot AI review requested due to automatic review settings December 8, 2025 17:08
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a defensive check to prevent errors when processing streaming API responses that may contain chunks with empty choices lists. This is a small but important robustness improvement that prevents potential IndexError exceptions when accessing chunk.choices[0].

Key changes:

  • Added a guard condition to skip chunks with empty choices before accessing chunk.choices[0].delta and chunk.choices[0].finish_reason

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +117 to +118
if not chunk.choices:
continue
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

The new check for empty chunk.choices improves robustness, but there's no test coverage for this edge case. Consider adding a test that verifies the function handles chunks with empty choices correctly. This could be done by extending the mock_comp utility in tests/utils.py to optionally include chunks with empty choices lists.

Copilot uses AI. Check for mistakes.
@bbartels
Copy link

@TheR1D Could you have a look at this? This happens when some openai proxies automatically add stream_options: { "include_usage": True } and result in the last line containing the usage chunk without a choices object.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants