From ebcf307a2f24e90cc3a22a9cac01df5af3375930 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Thu, 18 Sep 2025 14:13:36 +0000 Subject: [PATCH] Fix StreamedResponse.model_name for Azure OpenAI --- pydantic_ai_slim/pydantic_ai/models/openai.py | 5 ++++- tests/models/test_openai.py | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pydantic_ai_slim/pydantic_ai/models/openai.py b/pydantic_ai_slim/pydantic_ai/models/openai.py index 2b83fcad30..f98e24d907 100644 --- a/pydantic_ai_slim/pydantic_ai/models/openai.py +++ b/pydantic_ai_slim/pydantic_ai/models/openai.py @@ -586,9 +586,12 @@ async def _process_streamed_response( 'Streamed response ended without content or tool calls' ) + # ChatCompletionChunk.model is required to be set, but Azure OpenAI omits it so we fall back to the model name set by the user. + model_name = first_chunk.model or self._model_name + return OpenAIStreamedResponse( model_request_parameters=model_request_parameters, - _model_name=first_chunk.model, + _model_name=model_name, _model_profile=self.profile, _response=peekable_response, _timestamp=number_to_datetime(first_chunk.created), diff --git a/tests/models/test_openai.py b/tests/models/test_openai.py index 3ae08f6898..547c0d72e8 100644 --- a/tests/models/test_openai.py +++ b/tests/models/test_openai.py @@ -397,8 +397,11 @@ async def test_stream_text(allow_model_requests: None): async def test_stream_text_finish_reason(allow_model_requests: None): + first_chunk = text_chunk('hello ') + # Test that we fall back to the model name set by the user if the model name is not set in the first chunk, like on Azure OpenAI. + first_chunk.model = '' stream = [ - text_chunk('hello '), + first_chunk, text_chunk('world'), text_chunk('.', finish_reason='stop'), ] @@ -418,7 +421,7 @@ async def test_stream_text_finish_reason(allow_model_requests: None): ModelResponse( parts=[TextPart(content='hello world.')], usage=RequestUsage(input_tokens=6, output_tokens=3), - model_name='gpt-4o-123', + model_name='gpt-4o', timestamp=IsDatetime(), provider_name='openai', provider_details={'finish_reason': 'stop'},