From 0f114d399ce2f434984015bec06c79520130d18d Mon Sep 17 00:00:00 2001 From: mikemikimike <13286568797@163.com> Date: Tue, 18 Aug 2026 10:02:19 +0800 Subject: [PATCH] fix(azure): preserve stream model on null chunks --- .../litellm_core_utils/streaming_handler.py | 5 ++-- .../test_streaming_handler.py | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.py index f6340426c1b..e73fc4cd56a 100644 --- a/litellm/litellm_core_utils/streaming_handler.py +++ b/litellm/litellm_core_utils/streaming_handler.py @@ -1454,8 +1454,9 @@ class CustomStreamWrapper: LlmProviders.AZURE_AI.value, ]: if isinstance(chunk, BaseModel) and hasattr(chunk, "model"): - # for azure, we need to pass the model from the original chunk - self.model = getattr(chunk, "model", self.model) + chunk_model: Final = getattr(chunk, "model", None) + if chunk_model is not None: + self.model = chunk_model response_obj = self.handle_openai_chat_completion_chunk(chunk) if response_obj is None: return _ProviderChunkEarlyReturn(None) diff --git a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py b/tests/test_litellm/litellm_core_utils/test_streaming_handler.py index b5e33a4e421..7b47696b6cf 100644 --- a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py +++ b/tests/test_litellm/litellm_core_utils/test_streaming_handler.py @@ -3203,6 +3203,35 @@ def test_chunk_creator_drops_empty_finish_chunk( assert initialized_custom_stream_wrapper.received_finish_reason == "stop" +@pytest.mark.parametrize( + ("chunk_model", "expected_model"), + [(None, "azure/request-model"), ("azure/chunk-model", "azure/chunk-model")], +) +def test_azure_chunk_model_preserves_or_updates_request_model( + chunk_model: str | None, expected_model: str +): + wrapper = CustomStreamWrapper( + completion_stream=None, + model="azure/request-model", + logging_obj=MagicMock(), + custom_llm_provider="azure", + ) + chunk = ModelResponseStream( + model=chunk_model, + choices=[ + StreamingChoices( + finish_reason=None, + index=0, + delta=Delta(content="hello", role="assistant"), + ) + ], + ) + + wrapper.chunk_creator(chunk=chunk) + + assert wrapper.model == expected_model + + def test_chunk_creator_stops_iteration_on_trailing_chunk( initialized_custom_stream_wrapper: CustomStreamWrapper, ):