Skip to content

Fix multi reasoning_summary for openai #2162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 12 additions & 3 deletions pydantic_ai_slim/pydantic_ai/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,10 @@ class OpenAIResponsesStreamedResponse(StreamedResponse):
_response: AsyncIterable[responses.ResponseStreamEvent]
_timestamp: datetime

# Track reasoning summary parts to handle separate parts correctly
_reasoning_summary_part_counter: int = field(default=0, init=False)
_current_reasoning_summary_vendor_id: str | None = field(default=None, init=False)

async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]: # noqa: C901
async for chunk in self._response:
if isinstance(chunk, responses.ResponseCompletedEvent):
Expand Down Expand Up @@ -1093,17 +1097,22 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
pass

elif isinstance(chunk, responses.ResponseReasoningSummaryPartAddedEvent):
pass # there's nothing we need to do here
# Start a new reasoning summary part
self._reasoning_summary_part_counter += 1
self._current_reasoning_summary_vendor_id = f"{chunk.item_id}_part_{self._reasoning_summary_part_counter}"

elif isinstance(chunk, responses.ResponseReasoningSummaryPartDoneEvent):
pass # there's nothing we need to do here
# Mark the end of the current reasoning summary part
self._current_reasoning_summary_vendor_id = None

elif isinstance(chunk, responses.ResponseReasoningSummaryTextDoneEvent):
pass # there's nothing we need to do here

elif isinstance(chunk, responses.ResponseReasoningSummaryTextDeltaEvent):
# Use the current reasoning summary part's unique vendor_part_id
vendor_part_id = self._current_reasoning_summary_vendor_id or chunk.item_id
yield self._parts_manager.handle_thinking_delta(
vendor_part_id=chunk.item_id,
vendor_part_id=vendor_part_id,
content=chunk.delta,
signature=chunk.item_id,
)
Expand Down
Loading