-
Couldn't load subscription status.
- Fork 1.3k
Add Agent.run_stream_sync method and sync convenience methods on StreamedRunResult
#3146
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
ajac-zero
wants to merge
12
commits into
pydantic:main
Choose a base branch
from
ajac-zero:sync_stream
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.
+392
−3
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ccaf87f
Add run_stream_sync
ajac-zero 64586b4
Merge branch 'pydantic:main' into sync_stream
ajac-zero 8623cb9
add lazy implementation
ajac-zero bb5c7fe
Merge branch 'main' into sync_stream
ajac-zero 6497f63
Merge branch 'pydantic:main' into sync_stream
ajac-zero 6e74a2a
add _sync methods to StreamedRunResult
ajac-zero db860ed
fix doctest
ajac-zero 1f1952c
Merge branch 'main' into sync_stream
ajac-zero e523987
Merge branch 'main' into sync_stream
ajac-zero 5ec9b49
add disclaimers
ajac-zero e1765d3
add fixture override
ajac-zero 4f0d2ad
update docs
ajac-zero 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
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||
| from __future__ import annotations as _annotations | ||||||
|
|
||||||
| from collections.abc import AsyncIterator, Awaitable, Callable, Iterable | ||||||
| from collections.abc import AsyncIterator, Awaitable, Callable, Iterable, Iterator | ||||||
| from copy import deepcopy | ||||||
| from dataclasses import dataclass, field | ||||||
| from datetime import datetime | ||||||
|
|
@@ -9,6 +9,8 @@ | |||||
| from pydantic import ValidationError | ||||||
| from typing_extensions import TypeVar, deprecated | ||||||
|
|
||||||
| from pydantic_graph._utils import get_event_loop | ||||||
|
|
||||||
| from . import _utils, exceptions, messages as _messages, models | ||||||
| from ._output import ( | ||||||
| OutputDataT_inv, | ||||||
|
|
@@ -408,6 +410,27 @@ async def stream_output(self, *, debounce_by: float | None = 0.1) -> AsyncIterat | |||||
| else: | ||||||
| raise ValueError('No stream response or run result provided') # pragma: no cover | ||||||
|
|
||||||
| def stream_output_sync(self, *, debounce_by: float | None = 0.1) -> Iterator[OutputDataT]: | ||||||
ajac-zero marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| """Stream the output as an iterable. | ||||||
|
|
||||||
| This is a convenience method that wraps [`self.stream_output`][pydantic_ai.result.StreamedRunResult.stream_output] with `loop.run_until_complete(...)`. | ||||||
| You therefore can't use this method inside async code or if there's an active event loop. | ||||||
|
|
||||||
| The pydantic validator for structured data will be called in | ||||||
| [partial mode](https://docs.pydantic.dev/dev/concepts/experimental/#partial-validation) | ||||||
| on each iteration. | ||||||
|
|
||||||
| Args: | ||||||
| debounce_by: by how much (if at all) to debounce/group the output chunks by. `None` means no debouncing. | ||||||
| Debouncing is particularly important for long structured outputs to reduce the overhead of | ||||||
| performing validation as each token is received. | ||||||
|
|
||||||
| Returns: | ||||||
| An iterable of the response data. | ||||||
| """ | ||||||
| async_stream = self.stream_output(debounce_by=debounce_by) | ||||||
| yield from _blocking_async_iterator(async_stream) | ||||||
|
|
||||||
| async def stream_text(self, *, delta: bool = False, debounce_by: float | None = 0.1) -> AsyncIterator[str]: | ||||||
| """Stream the text result as an async iterable. | ||||||
|
|
||||||
|
|
@@ -436,6 +459,25 @@ async def stream_text(self, *, delta: bool = False, debounce_by: float | None = | |||||
| else: | ||||||
| raise ValueError('No stream response or run result provided') # pragma: no cover | ||||||
|
|
||||||
| def stream_text_sync(self, *, delta: bool = False, debounce_by: float | None = 0.1) -> Iterator[str]: | ||||||
| """Stream the text result as a sync iterable. | ||||||
|
|
||||||
| This is a convenience method that wraps [`self.stream_text`][pydantic_ai.result.StreamedRunResult.stream_text] with `loop.run_until_complete(...)`. | ||||||
|
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. Let's change all of them like this:
Suggested change
|
||||||
| You therefore can't use this method inside async code or if there's an active event loop. | ||||||
|
|
||||||
| !!! note | ||||||
| Result validators will NOT be called on the text result if `delta=True`. | ||||||
|
|
||||||
| Args: | ||||||
| delta: if `True`, yield each chunk of text as it is received, if `False` (default), yield the full text | ||||||
| up to the current point. | ||||||
| debounce_by: by how much (if at all) to debounce/group the response chunks by. `None` means no debouncing. | ||||||
| Debouncing is particularly important for long structured responses to reduce the overhead of | ||||||
| performing validation as each token is received. | ||||||
| """ | ||||||
| async_stream = self.stream_text(delta=delta, debounce_by=debounce_by) | ||||||
| yield from _blocking_async_iterator(async_stream) | ||||||
|
|
||||||
| @deprecated('`StreamedRunResult.stream_structured` is deprecated, use `stream_responses` instead.') | ||||||
| async def stream_structured( | ||||||
| self, *, debounce_by: float | None = 0.1 | ||||||
|
|
@@ -471,6 +513,25 @@ async def stream_responses( | |||||
| else: | ||||||
| raise ValueError('No stream response or run result provided') # pragma: no cover | ||||||
|
|
||||||
| def stream_responses_sync( | ||||||
| self, *, debounce_by: float | None = 0.1 | ||||||
| ) -> Iterator[tuple[_messages.ModelResponse, bool]]: | ||||||
| """Stream the response as an iterable of Structured LLM Messages. | ||||||
|
|
||||||
| This is a convenience method that wraps [`self.stream_responses`][pydantic_ai.result.StreamedRunResult.stream_responses] with `loop.run_until_complete(...)`. | ||||||
| You therefore can't use this method inside async code or if there's an active event loop. | ||||||
|
|
||||||
| Args: | ||||||
| debounce_by: by how much (if at all) to debounce/group the response chunks by. `None` means no debouncing. | ||||||
| Debouncing is particularly important for long structured responses to reduce the overhead of | ||||||
| performing validation as each token is received. | ||||||
|
|
||||||
| Returns: | ||||||
| An iterable of the structured response message and whether that is the last message. | ||||||
| """ | ||||||
| async_stream = self.stream_responses(debounce_by=debounce_by) | ||||||
| yield from _blocking_async_iterator(async_stream) | ||||||
|
|
||||||
| async def get_output(self) -> OutputDataT: | ||||||
| """Stream the whole response, validate and return it.""" | ||||||
| if self._run_result is not None: | ||||||
|
|
@@ -484,6 +545,14 @@ async def get_output(self) -> OutputDataT: | |||||
| else: | ||||||
| raise ValueError('No stream response or run result provided') # pragma: no cover | ||||||
|
|
||||||
| def get_output_sync(self) -> OutputDataT: | ||||||
| """Stream the whole response, validate and return it. | ||||||
|
|
||||||
| This is a convenience method that wraps [`self.get_output`][pydantic_ai.result.StreamedRunResult.get_output] with `loop.run_until_complete(...)`. | ||||||
| You therefore can't use this method inside async code or if there's an active event loop. | ||||||
| """ | ||||||
| return get_event_loop().run_until_complete(self.get_output()) | ||||||
|
|
||||||
| @property | ||||||
| def response(self) -> _messages.ModelResponse: | ||||||
| """Return the current state of the response.""" | ||||||
|
|
@@ -559,6 +628,17 @@ class FinalResult(Generic[OutputDataT]): | |||||
| __repr__ = _utils.dataclasses_no_defaults_repr | ||||||
|
|
||||||
|
|
||||||
| def _blocking_async_iterator(async_iter: AsyncIterator[T]) -> Iterator[T]: | ||||||
| loop = get_event_loop() | ||||||
|
|
||||||
| while True: | ||||||
| try: | ||||||
| item = loop.run_until_complete(async_iter.__anext__()) | ||||||
| yield item | ||||||
| except StopAsyncIteration: | ||||||
| break | ||||||
|
|
||||||
|
|
||||||
| def _get_usage_checking_stream_response( | ||||||
| stream_response: models.StreamedResponse, | ||||||
| limits: UsageLimits | None, | ||||||
|
|
||||||
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.
Uh oh!
There was an error while loading. Please reload this page.