Skip to content

Commit 406380c

Browse files
authored
Fix StreamedResponse.model_name for Azure OpenAI (#2945)
1 parent 8a46192 commit 406380c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

pydantic_ai_slim/pydantic_ai/models/openai.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,12 @@ async def _process_streamed_response(
586586
'Streamed response ended without content or tool calls'
587587
)
588588

589+
# ChatCompletionChunk.model is required to be set, but Azure OpenAI omits it so we fall back to the model name set by the user.
590+
model_name = first_chunk.model or self._model_name
591+
589592
return OpenAIStreamedResponse(
590593
model_request_parameters=model_request_parameters,
591-
_model_name=first_chunk.model,
594+
_model_name=model_name,
592595
_model_profile=self.profile,
593596
_response=peekable_response,
594597
_timestamp=number_to_datetime(first_chunk.created),

tests/models/test_openai.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,11 @@ async def test_stream_text(allow_model_requests: None):
397397

398398

399399
async def test_stream_text_finish_reason(allow_model_requests: None):
400+
first_chunk = text_chunk('hello ')
401+
# 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.
402+
first_chunk.model = ''
400403
stream = [
401-
text_chunk('hello '),
404+
first_chunk,
402405
text_chunk('world'),
403406
text_chunk('.', finish_reason='stop'),
404407
]
@@ -418,7 +421,7 @@ async def test_stream_text_finish_reason(allow_model_requests: None):
418421
ModelResponse(
419422
parts=[TextPart(content='hello world.')],
420423
usage=RequestUsage(input_tokens=6, output_tokens=3),
421-
model_name='gpt-4o-123',
424+
model_name='gpt-4o',
422425
timestamp=IsDatetime(),
423426
provider_name='openai',
424427
provider_details={'finish_reason': 'stop'},

0 commit comments

Comments
 (0)