Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pydantic_ai_slim/pydantic_ai/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 5 additions & 2 deletions tests/models/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]
Expand All @@ -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'},
Expand Down