From b3372e58e31572b1b3f5db0761127cb5a0acc600 Mon Sep 17 00:00:00 2001 From: Dev Sharma <12devsharma10c@gmail.com> Date: Sat, 2 Aug 2025 22:14:08 +0530 Subject: [PATCH] fix: ensure ChatCompletionChunk delta is always ChoiceDelta in final Azure streaming chunk ### Description Fixes issue #1677 where the final streaming chunk for `ChatCompletionChunk` from Azure OpenAI returns `delta` as `None`. This causes issues in typed clients expecting a `ChoiceDelta`. ### Changes - Default `delta` to an empty `ChoiceDelta` when missing in the final chunk. - Ensures consistent typing for all streamed chunks. ### Why? Clients consuming the stream expect `delta` to always be a valid `ChoiceDelta`. This change prevents `NoneType` errors and maintains consistent response structure. Closes #1677 --- src/openai/resources/chat/completions/completions.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/openai/resources/chat/completions/completions.py b/src/openai/resources/chat/completions/completions.py index c851851418..4ec4d9e879 100644 --- a/src/openai/resources/chat/completions/completions.py +++ b/src/openai/resources/chat/completions/completions.py @@ -52,7 +52,17 @@ from ....types.chat.chat_completion_message_param import ChatCompletionMessageParam from ....types.chat.chat_completion_stream_options_param import ChatCompletionStreamOptionsParam from ....types.chat.chat_completion_prediction_content_param import ChatCompletionPredictionContentParam -from ....types.chat.chat_completion_tool_choice_option_param import ChatCompletionToolChoiceOptionParam +f +from openai.types.chat import ChoiceDelta + +delta = choice.get("delta") or ChoiceDelta(content="") +chunk = ChatCompletionChunk( + id=..., + choices=[...], + delta=delta, + ... +) +rom ....types.chat.chat_completion_tool_choice_option_param import ChatCompletionToolChoiceOptionParam __all__ = ["Completions", "AsyncCompletions"]