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
+164-4Lines changed: 164 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -420,7 +420,7 @@ print(dice_result.data)
420
420
```
421
421
422
422
1. The simplest way to register tools via the `Agent` constructor is to pass a list of functions, the function signature is inspected to determine if the tool takes [`RunContext`][pydantic_ai.tools.RunContext].
423
-
2.`agent_a` and `agent_b` are identical — but we can use [`Tool`][pydantic_ai.tools.Tool] to give more fine-grained control over how tools are defined, e.g. setting their name or description.
423
+
2.`agent_a` and `agent_b` are identical — but we can use [`Tool`][pydantic_ai.tools.Tool] to reuse tool definitions and give more fine-grained control over how tools are defined, e.g. setting their name or description, or using a custom [`prepare`](#tool-prepare) method.
424
424
425
425
_(This example is complete, it can be run "as is")_
@@ -491,7 +491,167 @@ _(This example is complete, it can be run "as is")_
491
491
492
492
The return type of tool can be anything which Pydantic can serialize to JSON as some models (e.g. Gemini) support semi-structured return values, some expect text (OpenAI) but seem to be just as good at extracting meaning from the data. If a Python object is returned and the model expects a string, the value will be serialized to JSON.
493
493
494
-
If a tool has a single parameter that can be represented as an object in JSON schema (e.g. dataclass, TypedDict, pydantic model), the schema for the tool is simplified to be just that object. (TODO example)
494
+
If a tool has a single parameter that can be represented as an object in JSON schema (e.g. dataclass, TypedDict, pydantic model), the schema for the tool is simplified to be just that object.
495
+
496
+
Here's an example, we use [`TestModel.agent_model_function_tools`][pydantic_ai.models.test.TestModel.agent_model_function_tools] to inspect the tool schema that would be passed to the model.
497
+
498
+
```py title="single_parameter_tool.py"
499
+
from pydantic import BaseModel
500
+
501
+
from pydantic_ai import Agent
502
+
from pydantic_ai.models.test import TestModel
503
+
504
+
agent = Agent()
505
+
506
+
507
+
classFoobar(BaseModel):
508
+
"""This is a Foobar"""
509
+
510
+
x: int
511
+
y: str
512
+
z: float=3.14
513
+
514
+
515
+
@agent.tool_plain
516
+
deffoobar(f: Foobar) -> str:
517
+
returnstr(f)
518
+
519
+
520
+
test_model = TestModel()
521
+
result = agent.run_sync('hello', model=test_model)
The `prepare` method, should be of type [`ToolPrepareFunc`][pydantic_ai.tools.ToolPrepareFunc], a function which takes [`RunContext`][pydantic_ai.tools.RunContext] and a pre-built [`ToolDefinition`][pydantic_ai.tools.ToolDefinition], and should either return that `ToolDefinition` with or without modifying it, return a new `ToolDefinition`, or return `None` to indicate this tools should not be registered for that step.
561
+
562
+
Here's a simple `prepare` method that only includes the tool if the value of the dependency is `42`.
563
+
564
+
As with the previous example, we use [`TestModel`][pydantic_ai.models.test.TestModel] to demonstrate the behavior without calling a real model.
0 commit comments