From cfedf072f553ef212447f00263406ecfadebb8ad Mon Sep 17 00:00:00 2001 From: Unai Infante Date: Sat, 15 Feb 2025 16:58:32 +0100 Subject: [PATCH 1/3] Fix bug related to Azure OpenAI streaming response --- pydantic_ai_slim/pydantic_ai/models/openai.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pydantic_ai_slim/pydantic_ai/models/openai.py b/pydantic_ai_slim/pydantic_ai/models/openai.py index ff148199e9..225699ee24 100644 --- a/pydantic_ai_slim/pydantic_ai/models/openai.py +++ b/pydantic_ai_slim/pydantic_ai/models/openai.py @@ -353,9 +353,11 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]: continue # Handle the text part of the response - content = choice.delta.content - if content is not None: - yield self._parts_manager.handle_text_delta(vendor_part_id='content', content=content) + if ( + (delta := choice.delta) is not None + and (content := delta.content) is not None + ): + yield self._parts_manager.handle_text_delta(vendor_part_id="content", content=content) for dtc in choice.delta.tool_calls or []: maybe_event = self._parts_manager.handle_tool_call_delta( From 0c967261b1bad36189e149595712dda622a318c1 Mon Sep 17 00:00:00 2001 From: Unai Infante Date: Sat, 15 Feb 2025 17:07:08 +0100 Subject: [PATCH 2/3] Fix bug related to Azure OpenAI streaming response --- pydantic_ai_slim/pydantic_ai/models/openai.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pydantic_ai_slim/pydantic_ai/models/openai.py b/pydantic_ai_slim/pydantic_ai/models/openai.py index 225699ee24..486b7d3042 100644 --- a/pydantic_ai_slim/pydantic_ai/models/openai.py +++ b/pydantic_ai_slim/pydantic_ai/models/openai.py @@ -353,10 +353,7 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]: continue # Handle the text part of the response - if ( - (delta := choice.delta) is not None - and (content := delta.content) is not None - ): + if (delta := choice.delta) is not None and (content := delta.content) is not None: yield self._parts_manager.handle_text_delta(vendor_part_id="content", content=content) for dtc in choice.delta.tool_calls or []: From 0c0bfa598c68c5fd6ebfd1e67a2847611db6465d Mon Sep 17 00:00:00 2001 From: Unai Infante Date: Sat, 15 Feb 2025 17:08:38 +0100 Subject: [PATCH 3/3] Fix linting issue --- pydantic_ai_slim/pydantic_ai/models/openai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydantic_ai_slim/pydantic_ai/models/openai.py b/pydantic_ai_slim/pydantic_ai/models/openai.py index 486b7d3042..b162f76e20 100644 --- a/pydantic_ai_slim/pydantic_ai/models/openai.py +++ b/pydantic_ai_slim/pydantic_ai/models/openai.py @@ -354,7 +354,7 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]: # Handle the text part of the response if (delta := choice.delta) is not None and (content := delta.content) is not None: - yield self._parts_manager.handle_text_delta(vendor_part_id="content", content=content) + yield self._parts_manager.handle_text_delta(vendor_part_id='content', content=content) for dtc in choice.delta.tool_calls or []: maybe_event = self._parts_manager.handle_tool_call_delta(