Skip to content

Commit 0abdb57

Browse files
Improve empty choices handling in streaming chunks (#131)
1 parent 9454c9e commit 0abdb57

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

app/openai_ops.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,12 @@ def consume_openai_stream_to_write_reply(
319319
spent_seconds = time.time() - start_time
320320
if timeout_seconds < spent_seconds:
321321
raise TimeoutError()
322-
# Some versions of the Azure OpenAI API return an empty choices array in the first chunk
323-
if context.get("OPENAI_API_TYPE") == "azure" and not chunk.choices:
322+
# Streaming may emit control/usage/keep-alive chunks without choices,
323+
# so guard before indexing
324+
if not chunk.choices:
325+
logging.debug("Stream chunk missing choices; skipping")
324326
continue
327+
325328
item = chunk.choices[0].model_dump()
326329
if item.get("finish_reason") is not None:
327330
finish_reason = item.get("finish_reason")

0 commit comments

Comments
 (0)