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/tools.md
+64-3Lines changed: 64 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,8 @@ There are a number of ways to register tools with an agent:
15
15
* via the [`@agent.tool_plain`][pydantic_ai.Agent.tool_plain] decorator — for tools that do not need access to the agent [context][pydantic_ai.tools.RunContext]
16
16
* via the [`tools`][pydantic_ai.Agent.__init__] keyword argument to `Agent` which can take either plain functions, or instances of [`Tool`][pydantic_ai.tools.Tool]
17
17
18
+
## Registering Function Tools via Decorator
19
+
18
20
`@agent.tool` is considered the default decorator since in the majority of cases tools will need access to the agent context.
19
21
20
22
Here's an example using both:
@@ -188,7 +190,7 @@ sequenceDiagram
188
190
Note over Agent: Game session complete
189
191
```
190
192
191
-
## Registering Function Tools via kwarg
193
+
## Registering Function Tools via Agent Argument
192
194
193
195
As well as using the decorators, we can register tools via the `tools` argument to the [`Agent` constructor][pydantic_ai.Agent.__init__]. This is useful when you want to reuse tools, and can also give more fine-grained control over the tools.
_(This example is complete, it can be run "as is")_
246
248
249
+
## Function Tool Output
250
+
251
+
Tools can return anything that Pydantic can serialize to JSON, as well as audio, video, image or document content depending on the types of [multi-modal input](input.md) the model supports:
252
+
253
+
```python {title="function_tool_output.py"}
254
+
from datetime import datetime
255
+
256
+
from pydantic import BaseModel
257
+
258
+
from pydantic_ai import Agent, DocumentUrl, ImageUrl
259
+
from pydantic_ai.models.openai import OpenAIResponsesModel
#> The current time is 10:45 PM on April 17, 2025.
293
+
294
+
result = agent.run_sync('What is the user name?')
295
+
print(result.output)
296
+
#> The user's name is John.
297
+
298
+
result = agent.run_sync('What is the company name in the logo?')
299
+
print(result.output)
300
+
#> The company name in the logo is "Pydantic."
301
+
302
+
result = agent.run_sync('What is the main content of the document?')
303
+
print(result.output)
304
+
#> The document contains just the text "Dummy PDF file."
305
+
```
306
+
_(This example is complete, it can be run "as is")_
307
+
308
+
Some models (e.g. Gemini) natively support semi-structured return values, while 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.
309
+
247
310
## Function Tools vs. Structured Outputs
248
311
249
312
As the name suggests, function tools use the model's "tools" or "functions" API to let the model know what is available to call. Tools or functions are also used to define the schema(s) for structured responses, thus a model might have access to many tools, some of which call function tools while others end the run and produce a final output.
_(This example is complete, it can be run "as is")_
309
372
310
-
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.
311
-
312
373
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.
313
374
314
375
Here's an example where we use [`TestModel.last_model_request_parameters`][pydantic_ai.models.test.TestModel.last_model_request_parameters] to inspect the tool schema that would be passed to the model.
0 commit comments