Skip to content

fix(python): guard empty choices on final streaming chunk - #922

Closed
dipeshpandit12 wants to merge 1 commit into
microsoft:mainfrom
dipeshpandit12:fix-streaming-empty-choices
Closed

fix(python): guard empty choices on final streaming chunk#922
dipeshpandit12 wants to merge 1 commit into
microsoft:mainfrom
dipeshpandit12:fix-streaming-empty-choices

Conversation

@dipeshpandit12

Copy link
Copy Markdown

Related issue: #905
Supersedes #919 (closed after renaming the branch to remove the / so Azure Pipelines can resolve it).

Problem

The final chunk yielded by complete_streaming_chat can carry an empty choices list. The documented example in chat_client.py and the streaming samples indexed chunk.choices[0] unconditionally, raising IndexError after the full response had already been produced.

Fix

Guard with if not chunk.choices: continue in:

  • sdk/python/src/openai/chat_client.py (documented example in the docstring)
  • samples/python/native-chat-completions/src/app.py
  • samples/python/tutorial-chat-assistant/src/app.py

Testing

Reproduced on macOS (arm64), Python 3.13, foundry-local-sdk 1.2.3, model qwen2.5-0.5b via the WebGPU EP. After the fix, streaming completes cleanly with no exception — verified with the interactive app.py over a 3-turn conversation and a single-prompt run.

The final chunk yielded by complete_streaming_chat can carry an empty
choices list. The documented example in chat_client and the streaming
samples indexed chunk.choices[0] unconditionally, raising IndexError
after the full response had already been produced.

Guard with 'if not chunk.choices: continue' in the SDK docstring example
and in the native-chat-completions and tutorial-chat-assistant samples.
Copilot AI review requested due to automatic review settings July 28, 2026 01:58
@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown

@dipeshpandit12 is attempting to deploy a commit to the MSFT-AIP Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@prathikr

Copy link
Copy Markdown
Collaborator

Still not working, ported your changes in #926

@prathikr prathikr closed this Jul 28, 2026
prathikr added a commit that referenced this pull request Jul 29, 2026
Related issue: #905

## Problem
The final chunk yielded by `complete_streaming_chat` can carry an empty
`choices` list. The documented example in `chat_client.py` and the
streaming samples indexed `chunk.choices[0]` unconditionally, raising
`IndexError` **after** the full response had already been produced.

## Fix
Guard with `if not chunk.choices: continue` in:
- `samples/python/native-chat-completions/src/app.py`
- `samples/python/tutorial-chat-assistant/src/app.py`

## Testing
Reproduced on macOS (arm64), Python 3.13, `foundry-local-sdk` 1.2.3,
model `qwen2.5-0.5b` via the WebGPU EP. After the fix, streaming
completes cleanly with no exception — verified with the interactive
`app.py` over a 3-turn conversation and a single-prompt run.

Co-authored-by: Prathik Rao <prathikrao@microsoft.com>
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.

3 participants