-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Update docs about choosing final_result in sync and stream modes
#3725
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -315,6 +315,12 @@ When the model calls other tools in parallel with an output tool, you can contro | |||||||
|
|
||||||||
| The `'exhaustive'` strategy is useful when tools have important side effects (like logging, sending notifications, or updating metrics) that should always execute. | ||||||||
|
|
||||||||
| !!! warning "Streaming vs Sync Behavior Difference" | ||||||||
| `run_stream()` behaves differently from `run()` and `run_sync()` when choosing the final result: | ||||||||
|
Collaborator
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.
|
||||||||
|
|
||||||||
| - **`run_stream()`**: The first called tool that **can** produce a final result (output or deferred) becomes the final result | ||||||||
|
Collaborator
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 link to the deferred tool docs |
||||||||
| - **`run()` / `run_sync()`**: The first **output** tool becomes the final result. If none are called, all **deferred** tools become the final result as `DeferredToolRequests` | ||||||||
|
Contributor
Author
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.
The wording is like this, since we get an
Contributor
Author
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. If I understand correctly, the deferred tools args are not validated I guess it means we could check that if any deferred tools calls are present and pydantic-ai/pydantic_ai_slim/pydantic_ai/_agent_graph.py Lines 913 to 915 in ccefddc
But I'm not sure about it...
Collaborator
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.
That's true for external tools, but not for function tools that raise
I don't quite understand what you mean
Contributor
Author
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.
I mean we could try to guess if we have any valid differed tools calls in the same model response. And if we have any, do not raise
Collaborator
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. @Danipulok I think the behavior of all-invalid output tool calls + deferred tool calls (valid or otherwise) should be identical to the behavior of all-invalid output tool calls + function tool calls. That means that the With deferred tool calls, the consistent behavior would be to never return
Contributor
Author
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. Got it, thank you! |
||||||||
|
|
||||||||
| #### Native Output | ||||||||
|
|
||||||||
| Native Output mode uses a model's native "Structured Outputs" feature (aka "JSON Schema response format"), where the model is forced to only output text matching the provided JSON schema. Note that this is not supported by all models, and sometimes comes with restrictions. For example, Gemini cannot use tools at the same time as structured output, and attempting to do so will result in an error. | ||||||||
|
|
||||||||
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.
It's not exactly streaming vs sync; it's just
run_streamthat's odd. So maybe it makes sense to mention this under https://ai.pydantic.dev/agents/#streaming-events-and-final-output, either as well, or instead. I say "instead" because we already give context there onrun_streamalways preferring the first final result it sees, which is in line with what we're explaining here. So maybe that place should have the primary explanation, and then here we can link to there