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
Copy file name to clipboardExpand all lines: docs/agents.md
+4-3Lines changed: 4 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -125,10 +125,11 @@ It also takes an optional `event_stream_handler` argument that you can use to ga
125
125
The example below shows how to stream events and text output. You can also [stream structured output](output.md#streaming-structured-output).
126
126
127
127
!!! note
128
-
As the `run_stream()` method will consider the first output matching the [output type](output.md#structured-output) to be the final output,
129
-
it will stop running the agent graph and will not execute any tool calls made by the model after this "final" output.
128
+
The `run_stream()` method will consider the first output that matches the [output type](output.md#structured-output) to be the final output of the agent run, even when the model generates tool calls after this "final" output.
130
129
131
-
If you want to always run the agent graph to completion and stream all events from the model's streaming response and the agent's execution of tools,
130
+
These "dangling" tool calls will not be executed unless the agent's [`end_strategy`][pydantic_ai.agent.Agent.end_strategy] is set to `'exhaustive'`, and even then their results will not be sent back to the model as the agent run will already be considered completed.
131
+
132
+
If you want to always keep running the agent when it performs tool calls, and stream all events from the model's streaming response and the agent's execution of tools,
132
133
use [`agent.run_stream_events()`][pydantic_ai.agent.AbstractAgent.run_stream_events] or [`agent.iter()`][pydantic_ai.agent.AbstractAgent.iter] instead, as described in the following sections.
Copy file name to clipboardExpand all lines: docs/output.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -306,6 +306,15 @@ print(repr(result.output))
306
306
307
307
_(This example is complete, it can be run "as is")_
308
308
309
+
##### Parallel Output Tool Calls
310
+
311
+
When the model calls other tools in parallel with an output tool, you can control how tool calls are executed by setting the agent's [`end_strategy`][pydantic_ai.agent.Agent.end_strategy]:
312
+
313
+
-`'early'` (default): Output tools are executed first. Once a valid final result is found, remaining function and output tool calls are skipped
314
+
-`'exhaustive'`: Output tools are executed first, then all function tools are executed. The first valid output tool result becomes the final output
315
+
316
+
The `'exhaustive'` strategy is useful when tools have important side effects (like logging, sending notifications, or updating metrics) that should always execute.
317
+
309
318
#### Native Output
310
319
311
320
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.
Copy file name to clipboardExpand all lines: docs/tools-advanced.md
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -381,6 +381,13 @@ Async functions are run on the event loop, while sync functions are offloaded to
381
381
!!! note "Limiting tool executions"
382
382
You can cap tool executions within a run using [`UsageLimits(tool_calls_limit=...)`](agents.md#usage-limits). The counter increments only after a successful tool invocation. Output tools (used for [structured output](output.md)) are not counted in the `tool_calls` metric.
383
383
384
+
#### Output Tool Calls
385
+
386
+
When a model calls an [output tool](output.md#tool-output) in parallel with other tools, the agent's [`end_strategy`][pydantic_ai.agent.Agent.end_strategy] parameter controls how these tool calls are executed.
387
+
The `'exhaustive'` strategy ensures all tools are executed even after a final result is found, which is useful when tools have side effects (like logging, sending notifications, or updating metrics) that should always execute.
388
+
389
+
For more information of how `end_strategy` works with both function tools and output tools, see the [Output Tool](output.md#parallel-output-tool-calls) docs.
390
+
384
391
## See Also
385
392
386
393
-[Function Tools](tools.md) - Basic tool concepts and registration
0 commit comments