fix(python): guard empty choices on final streaming chunk - #922
Closed
dipeshpandit12 wants to merge 1 commit into
Closed
fix(python): guard empty choices on final streaming chunk#922dipeshpandit12 wants to merge 1 commit into
dipeshpandit12 wants to merge 1 commit into
Conversation
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.
|
@dipeshpandit12 is attempting to deploy a commit to the MSFT-AIP Team on Vercel. A member of the Team first needs to authorize it. |
Collaborator
|
Still not working, ported your changes in #926 |
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>
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.
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_chatcan carry an emptychoiceslist. The documented example inchat_client.pyand the streaming samples indexedchunk.choices[0]unconditionally, raisingIndexErrorafter the full response had already been produced.Fix
Guard with
if not chunk.choices: continuein:sdk/python/src/openai/chat_client.py(documented example in the docstring)samples/python/native-chat-completions/src/app.pysamples/python/tutorial-chat-assistant/src/app.pyTesting
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.pyover a 3-turn conversation and a single-prompt run.