Skip to content

Commit 8f11896

Browse files
prathikrPrathik Rao
andauthored
fix(python): guard empty choices on final streaming chunk- #922 (#926)
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>
1 parent eabb3c8 commit 8f11896

2 files changed

Lines changed: 4 additions & 0 deletions

File tree

  • samples/python
    • native-chat-completions/src
    • tutorial-chat-assistant/src

samples/python/native-chat-completions/src/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def ep_progress(ep_name: str, percent: float):
5151
# Stream the response token by token
5252
print("Assistant: ", end="", flush=True)
5353
for chunk in client.complete_streaming_chat(messages):
54+
if not chunk.choices: # the final chunk can carry no choices
55+
continue
5456
content = chunk.choices[0].delta.content
5557
if content:
5658
print(content, end="", flush=True)

samples/python/tutorial-chat-assistant/src/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def ep_progress(ep_name: str, percent: float):
6363
print("Assistant: ", end="", flush=True)
6464
full_response = ""
6565
for chunk in client.complete_streaming_chat(messages):
66+
if not chunk.choices: # the final chunk can carry no choices
67+
continue
6668
content = chunk.choices[0].delta.content
6769
if content:
6870
print(content, end="", flush=True)

0 commit comments

Comments
 (0)