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
Improve deferred tools metadata documentation example
- Replace contrived task_id parameter with realistic tool signatures
- Add ComputeDeps class demonstrating dependency injection pattern
- Show using ctx.deps to compute metadata from tool arguments
- Remove incorrect statement about backwards compatibility
- Update test_examples.py to match new realistic example
Copy file name to clipboardExpand all lines: docs/deferred-tools.md
+77-51Lines changed: 77 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -326,16 +326,20 @@ _(This example is complete, it can be run "as is" — you'll need to add `asynci
326
326
327
327
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.
328
328
329
+
A common pattern is to use [`RunContext`][pydantic_ai.tools.RunContext] to access application dependencies (databases, APIs, calculators) and compute metadata based on the tool arguments and current context. This allows you to provide rich information for approval decisions or external task tracking.
330
+
329
331
Common use cases for metadata include:
330
332
331
-
-Providing cost estimates or time estimates for approval decisions
332
-
- Including task IDs or tracking information for external execution
333
-
- Storing context about why approval is required
334
-
- Attaching priority or urgency information
333
+
-Computing cost estimates based on tool arguments and dependency services
334
+
- Including job IDs or tracking information for external execution systems
335
+
- Storing approval context like user permissions or resource availability
336
+
- Attaching priority levels computed from current system state
335
337
336
-
Here's an example showing how to use metadata with both approval-required and external tools:
338
+
Here's an example showing how to use metadata with deps to make informed approval decisions:
result = agent.run_sync(message_history=messages, deferred_tool_results=results, deps=deps)
427
453
print(result.output)
428
-
#> I completed task-123 and retrieved data from the /data endpoint.
454
+
"""
455
+
Model gpt-4 trained on large_dataset and dataset processing job job_large_dataset_transform completed
456
+
"""
429
457
```
430
458
431
459
_(This example is complete, it can be run "as is")_
432
460
433
-
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.
434
-
435
461
## See Also
436
462
437
463
-[Function Tools](tools.md) - Basic tool concepts and registration
0 commit comments