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
Enables CallDeferred and ApprovalRequired exceptions to carry arbitrary
metadata via an optional `metadata` parameter. The metadata is accessible
in DeferredToolRequests.metadata keyed by tool_call_id.
This allows tools to:
- Provide cost/time estimates for approval decisions
- Include task IDs for external execution tracking
- Store context about why approval is required
- Attach priority or urgency information
Backward compatible - metadata defaults to empty dict if not provided.
Copy file name to clipboardExpand all lines: docs/deferred-tools.md
+105Lines changed: 105 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -320,6 +320,111 @@ async def main():
320
320
321
321
_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_
322
322
323
+
## Attaching Metadata to Deferred Tools
324
+
325
+
Both [`CallDeferred`][pydantic_ai.exceptions.CallDeferred] and [`ApprovalRequired`][pydantic_ai.exceptions.ApprovalRequired] exceptions accept an optional `metadata` parameter that allows you to attach arbitrary context information to deferred tool calls. This metadata is then available in the [`DeferredToolRequests.metadata`][pydantic_ai.tools.DeferredToolRequests.metadata] dictionary, keyed by the tool call ID.
326
+
327
+
Common use cases for metadata include:
328
+
329
+
- Providing cost estimates or time estimates for approval decisions
330
+
- Including task IDs or tracking information for external execution
331
+
- Storing context about why approval is required
332
+
- Attaching priority or urgency information
333
+
334
+
Here's an example showing how to use metadata with both approval-required and external tools:
results.calls[call.tool_call_id] =f'Result from {task_id}: success'
416
+
417
+
result = agent.run_sync(message_history=messages, deferred_tool_results=results)
418
+
print(result.output)
419
+
"""
420
+
I completed task-123 and retrieved data from the /data endpoint.
421
+
"""
422
+
```
423
+
424
+
_(This example is complete, it can be run "as is")_
425
+
426
+
The metadata dictionary can contain any JSON-serializable values and is entirely application-defined. If no metadata is provided when raising the exception, the tool call ID will still be present in the `metadata` dictionary with an empty dict as the value for backward compatibility.
427
+
323
428
## See Also
324
429
325
430
-[Function Tools](tools.md) - Basic tool concepts and registration
0 commit comments