-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix streaming thinking tags split across multiple chunks #3206
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
dsfaccini
wants to merge
19
commits into
pydantic:main
Choose a base branch
from
dsfaccini:handle-streamed-thinking-over-multiple-chunks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6e145e6
Refactor handle_text_delta() to generator pattern with split tag buff…
dsfaccini 11b5f1f
fix test suite for generator pattern and ensure coverage
dsfaccini 0f876de
Merge main into fix-split-thinking-tags-v2
dsfaccini b5c0910
Merge remote-tracking branch 'origin/main' into fix-split-thinking-ta…
dsfaccini 3439159
rename _tag_buffer to _thinking_tag_buffer
dsfaccini 876ebb2
remove pragmas
dsfaccini adc51e6
adds a finalize method to prevent lost content from buffered chunks t…
dsfaccini 0818191
fix: handle thinking tags with trailing content and vendor_part_id=…
dsfaccini f50d4b4
fix coverage
dsfaccini 551d035
remove pragmas
dsfaccini 0998a63
Merge main to stay updated with latest changes
dsfaccini 9b598dd
models
dsfaccini 41a38e2
- include incomplete closing tags in thinking part
dsfaccini dcac211
wip: improve coverage
dsfaccini b9bdd78
- reduce complexity in parts manager
dsfaccini 4b7f0c1
Merge branch 'main' into handle-streamed-thinking-over-multiple-chunks
dsfaccini ac03e38
add tests for coverage
dsfaccini 5fae762
- fix coverage
dsfaccini 28578bf
- fix case multiple_thinking_parts_with_text_between
dsfaccini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| from __future__ import annotations as _annotations | ||
|
|
||
| import base64 | ||
| import copy | ||
| import warnings | ||
| from abc import ABC, abstractmethod | ||
| from collections.abc import AsyncIterator, Iterator | ||
|
|
@@ -521,7 +522,7 @@ class StreamedResponse(ABC): | |
| _event_iterator: AsyncIterator[ModelResponseStreamEvent] | None = field(default=None, init=False) | ||
| _usage: RequestUsage = field(default_factory=RequestUsage, init=False) | ||
|
|
||
| def __aiter__(self) -> AsyncIterator[ModelResponseStreamEvent]: | ||
| def __aiter__(self) -> AsyncIterator[ModelResponseStreamEvent]: # noqa: C901 | ||
| """Stream the response as an async iterable of [`ModelResponseStreamEvent`][pydantic_ai.messages.ModelResponseStreamEvent]s. | ||
|
|
||
| This proxies the `_event_iterator()` and emits all events, while also checking for matches | ||
|
|
@@ -580,6 +581,16 @@ def part_end_event(next_part: ModelResponsePart | None = None) -> PartEndEvent | | |
|
|
||
| yield event | ||
|
|
||
| # Flush any buffered content and stream finalize events | ||
| for finalize_event in self._parts_manager.finalize(): # pragma: no cover | ||
| if isinstance(finalize_event, PartStartEvent): | ||
| if last_start_event: | ||
| end_event = part_end_event(finalize_event.part) | ||
| if end_event: | ||
| yield end_event | ||
| last_start_event = finalize_event | ||
| yield finalize_event | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we set |
||
|
|
||
| end_event = part_end_event() | ||
| if end_event: | ||
| yield end_event | ||
|
|
@@ -602,8 +613,14 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]: | |
|
|
||
| def get(self) -> ModelResponse: | ||
| """Build a [`ModelResponse`][pydantic_ai.messages.ModelResponse] from the data received from the stream so far.""" | ||
| # Flush any buffered content before building response | ||
| # clone parts manager to avoid modifying the ongoing stream state | ||
| cloned_manager = copy.deepcopy(self._parts_manager) | ||
| for _ in cloned_manager.finalize(): | ||
| pass | ||
|
|
||
| return ModelResponse( | ||
| parts=self._parts_manager.get_parts(), | ||
| parts=cloned_manager.get_parts(), | ||
| model_name=self.model_name, | ||
| timestamp=self.timestamp, | ||
| usage=self.usage(), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are only two cases when finalize will have an effect: when we're buffering
<th→ emits aPartStartEvent</th→ emits aPartDeltaEventcoverage is complaining that there's no test running through the
PartDeltaEventbranch of this, so I need to figure out how to test it