You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"""Run the agent with a user prompt in collected streaming mode.
591
+
592
+
This method builds an internal agent graph (using system prompts, tools and output schemas) and then
593
+
runs the graph until the model produces output matching the `output_type`, for example text or structured data.
594
+
At this point, a streaming run result object is collected and -- once this output has completed streaming -- you can iterate over the complete output, message history, and usage.
595
+
596
+
As this method will consider the first output matching the `output_type` to be the final output,
597
+
it will stop running the agent graph and will not execute any tool calls made by the model after this "final" output.
598
+
If you want to always run the agent graph to completion and stream events and output at the same time,
599
+
use [`agent.run()`][pydantic_ai.agent.AbstractAgent.run] with an `event_stream_handler` or [`agent.iter()`][pydantic_ai.agent.AbstractAgent.iter] instead.
600
+
601
+
Example:
602
+
```python
603
+
from pydantic_ai import Agent
604
+
605
+
agent = Agent('openai:gpt-4o')
606
+
607
+
def main():
608
+
with agent.run_stream_sync('What is the capital of the UK?') as response:
609
+
print(response.get_output())
610
+
#> The capital of the UK is London.
611
+
```
612
+
613
+
Args:
614
+
user_prompt: User input to start/continue the conversation.
615
+
output_type: Custom output type to use for this run, `output_type` may only be used if the agent has no
616
+
output validators since output validators would expect an argument that matches the agent's output type.
617
+
message_history: History of the conversation so far.
618
+
deferred_tool_results: Optional results for deferred tool calls in the message history.
619
+
model: Optional model to use for this run, required if `model` was not set when creating the agent.
620
+
deps: Optional dependencies to use for this run.
621
+
model_settings: Optional settings to use for this model's request.
622
+
usage_limits: Optional limits on model request count or token usage.
623
+
usage: Optional usage to start with, useful for resuming a conversation or agents used in tools.
624
+
infer_name: Whether to try to infer the agent name from the call frame if it's not set.
625
+
toolsets: Optional additional toolsets for this run.
626
+
builtin_tools: Optional additional builtin tools for this run.
627
+
event_stream_handler: Optional handler for events from the model's streaming response and the agent's execution of tools to use for this run.
628
+
It will receive all the events up until the final result is found, which you can then read or stream from inside the context manager.
629
+
Note that it does _not_ receive any events after the final result is found.
0 commit comments