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
+59-15Lines changed: 59 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ print(result.data)
49
49
```
50
50
51
51
1. Create an agent, which expects an integer dependency and returns a boolean result. This agent will have type `#!python Agent[int, bool]`.
52
-
2. Define a tool that checks if the square is a winner. Here [`RunContext`][pydantic_ai.dependencies.RunContext] is parameterized with the dependency type `int`; if you got the dependency type wrong you'd get a typing error.
52
+
2. Define a tool that checks if the square is a winner. Here [`RunContext`][pydantic_ai.tools.RunContext] is parameterized with the dependency type `int`; if you got the dependency type wrong you'd get a typing error.
53
53
3. In reality, you might want to use a random number here e.g. `random.randint(0, 36)`.
54
54
4.`result.data` will be a boolean indicating if the square is a winner. Pydantic performs the result validation, it'll be typed as a `bool` since its type is derived from the `result_type` generic parameter of the agent.
55
55
@@ -161,7 +161,7 @@ def foobar(x: bytes) -> None:
161
161
pass
162
162
163
163
164
-
result = agent.run_sync('Does their name start with "A"?', deps=User('Adam'))
164
+
result = agent.run_sync('Does their name start with "A"?', deps=User('Anne'))
165
165
foobar(result.data) # (3)!
166
166
```
167
167
@@ -222,7 +222,7 @@ print(result.data)
222
222
223
223
1. The agent expects a string dependency.
224
224
2. Static system prompt defined at agent creation time.
225
-
3. Dynamic system prompt defined via a decorator with [`RunContext`][pydantic_ai.dependencies.RunContext], this is called just after `run_sync`, not when the agent is created, so can benefit from runtime information like the dependencies used on that run.
225
+
3. Dynamic system prompt defined via a decorator with [`RunContext`][pydantic_ai.tools.RunContext], this is called just after `run_sync`, not when the agent is created, so can benefit from runtime information like the dependencies used on that run.
226
226
4. Another dynamic system prompt, system prompts don't have to have the `RunContext` parameter.
227
227
228
228
_(This example is complete, it can be run "as is")_
@@ -238,12 +238,13 @@ They're useful when it is impractical or impossible to put all the context an ag
238
238
239
239
The main semantic difference between PydanticAI Tools and RAG is RAG is synonymous with vector search, while PydanticAI tools are more general-purpose. (Note: we may add support for vector search functionality in the future, particularly an API for generating embeddings. See [#58](https://github.com/pydantic/pydantic-ai/issues/58))
240
240
241
-
There are two different decorator functions to register tools:
241
+
There are a number of ways to register tools with an agent:
242
242
243
-
1.[`@agent.tool`][pydantic_ai.Agent.tool] — for tools that need access to the agent [context][pydantic_ai.dependencies.RunContext]
244
-
2.[`@agent.tool_plain`][pydantic_ai.Agent.tool_plain] — for tools that do not need access to the agent [context][pydantic_ai.dependencies.RunContext]
243
+
* via the [`@agent.tool`][pydantic_ai.Agent.tool] decorator — for tools that need access to the agent [context][pydantic_ai.tools.RunContext]
244
+
* 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]
245
+
* 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]
245
246
246
-
`@agent.tool` is the default since in the majority of cases tools will need access to the agent context.
247
+
`@agent.tool` is considered the default decorator since in the majority of cases tools will need access to the agent context.
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 re-use tools, and can also give more fine-grained control over the tools.
387
+
388
+
```py title="dice_game_tool_kwarg.py"
389
+
import random
390
+
391
+
from pydantic_ai import Agent, RunContext, Tool
392
+
393
+
394
+
defroll_die() -> str:
395
+
"""Roll a six-sided die and return the result."""
396
+
returnstr(random.randint(1, 6))
397
+
398
+
399
+
defget_player_name(ctx: RunContext[str]) -> str:
400
+
"""Get the player's name."""
401
+
return ctx.deps
402
+
403
+
404
+
agent_a = Agent(
405
+
'gemini-1.5-flash',
406
+
deps_type=str,
407
+
tools=[roll_die, get_player_name], # (1)!
408
+
)
409
+
agent_b = Agent(
410
+
'gemini-1.5-flash',
411
+
deps_type=str,
412
+
tools=[ # (2)!
413
+
Tool(roll_die, takes_ctx=False),
414
+
Tool(get_player_name, takes_ctx=True),
415
+
],
416
+
)
417
+
dice_result = agent_b.run_sync('My guess is 4', deps='Anne')
418
+
print(dice_result.data)
419
+
#> Congratulations Anne, you guessed correctly! You're a winner!
420
+
```
421
+
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.
424
+
425
+
_(This example is complete, it can be run "as is")_
426
+
383
427
### Function Tools vs. Structured Results
384
428
385
429
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 return a result.
_(This example is complete, it can be run "as is")_
447
491
448
-
The return type of tool can be any valid JSON object ([`JsonData`][pydantic_ai.dependencies.JsonData]) 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.
492
+
The return type of tool can be any valid JSON object ([`JsonData`][pydantic_ai.tools.JsonData]) 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.
449
493
450
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)
451
495
@@ -456,7 +500,7 @@ Validation errors from both function tool parameter validation and [structured r
456
500
You can also raise [`ModelRetry`][pydantic_ai.exceptions.ModelRetry] from within a [tool](#function-tools) or [result validator function](results.md#result-validators-functions) to tell the model it should retry generating a response.
457
501
458
502
- The default retry count is **1** but can be altered for the [entire agent][pydantic_ai.Agent.__init__], a [specific tool][pydantic_ai.Agent.tool], or a [result validator][pydantic_ai.Agent.__init__].
459
-
- You can access the current retry count from within a tool or result validator via [`ctx.retry`][pydantic_ai.dependencies.RunContext].
503
+
- You can access the current retry count from within a tool or result validator via [`ctx.retry`][pydantic_ai.tools.RunContext].
Copy file name to clipboardExpand all lines: docs/dependencies.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,7 @@ _(This example is complete, it can be run "as is")_
51
51
52
52
## Accessing Dependencies
53
53
54
-
Dependencies are accessed through the [`RunContext`][pydantic_ai.dependencies.RunContext] type, this should be the first parameter of system prompt functions etc.
54
+
Dependencies are accessed through the [`RunContext`][pydantic_ai.tools.RunContext] type, this should be the first parameter of system prompt functions etc.
#> Did you hear about the toothpaste scandal? They called it Colgate.
93
93
```
94
94
95
-
1.[`RunContext`][pydantic_ai.dependencies.RunContext] may optionally be passed to a [`system_prompt`][pydantic_ai.Agent.system_prompt] function as the only argument.
96
-
2.[`RunContext`][pydantic_ai.dependencies.RunContext] is parameterized with the type of the dependencies, if this type is incorrect, static type checkers will raise an error.
97
-
3. Access dependencies through the [`.deps`][pydantic_ai.dependencies.RunContext.deps] attribute.
98
-
4. Access dependencies through the [`.deps`][pydantic_ai.dependencies.RunContext.deps] attribute.
95
+
1.[`RunContext`][pydantic_ai.tools.RunContext] may optionally be passed to a [`system_prompt`][pydantic_ai.Agent.system_prompt] function as the only argument.
96
+
2.[`RunContext`][pydantic_ai.tools.RunContext] is parameterized with the type of the dependencies, if this type is incorrect, static type checkers will raise an error.
97
+
3. Access dependencies through the [`.deps`][pydantic_ai.tools.RunContext.deps] attribute.
98
+
4. Access dependencies through the [`.deps`][pydantic_ai.tools.RunContext.deps] attribute.
99
99
100
100
_(This example is complete, it can be run "as is")_
Copy file name to clipboardExpand all lines: docs/index.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -125,8 +125,8 @@ async def main():
125
125
2. Here we configure the agent to use [OpenAI's GPT-4o model](api/models/openai.md), you can also set the model when running the agent.
126
126
3. The `SupportDependencies` dataclass is used to pass data, connections, and logic into the model that will be needed when running [system prompt](agents.md#system-prompts) and [tool](agents.md#function-tools) functions. PydanticAI's system of dependency injection provides a [type-safe](agents.md#static-type-checking) way to customise the behavior of your agents, and can be especially useful when running [unit tests](testing-evals.md) and evals.
127
127
4. Static [system prompts](agents.md#system-prompts) can be registered with the [`system_prompt` keyword argument][pydantic_ai.Agent.__init__] to the agent.
128
-
5. Dynamic [system prompts](agents.md#system-prompts) can be registered with the [`@agent.system_prompt`][pydantic_ai.Agent.system_prompt] decorator, and can make use of dependency injection. Dependencies are carried via the [`RunContext`][pydantic_ai.dependencies.RunContext] argument, which is parameterized with the `deps_type` from above. If the type annotation here is wrong, static type checkers will catch it.
129
-
6.[`tool`](agents.md#function-tools) let you register functions which the LLM may call while responding to a user. Again, dependencies are carried via [`RunContext`][pydantic_ai.dependencies.RunContext], any other arguments become the tool schema passed to the LLM. Pydantic is used to validate these arguments, and errors are passed back to the LLM so it can retry.
128
+
5. Dynamic [system prompts](agents.md#system-prompts) can be registered with the [`@agent.system_prompt`][pydantic_ai.Agent.system_prompt] decorator, and can make use of dependency injection. Dependencies are carried via the [`RunContext`][pydantic_ai.tools.RunContext] argument, which is parameterized with the `deps_type` from above. If the type annotation here is wrong, static type checkers will catch it.
129
+
6.[`tool`](agents.md#function-tools) let you register functions which the LLM may call while responding to a user. Again, dependencies are carried via [`RunContext`][pydantic_ai.tools.RunContext], any other arguments become the tool schema passed to the LLM. Pydantic is used to validate these arguments, and errors are passed back to the LLM so it can retry.
130
130
7. The docstring of a tool is also passed to the LLM as the description of the tool. Parameter descriptions are [extracted](agents.md#function-tools-and-schema) from the docstring and added to the parameter schema sent to the LLM.
131
131
8.[Run the agent](agents.md#running-agents) asynchronously, conducting a conversation with the LLM until a final response is reached. Even in this fairly simple case, the agent will exchange multiple messages with the LLM as tools are called to retrieve a result.
132
132
9. The response from the agent will, be guaranteed to be a `SupportResult`, if validation fails [reflection](agents.md#reflection-and-self-correction) will mean the agent is prompted to try again.
Copy file name to clipboardExpand all lines: docs/install.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ To run the examples, follow instructions in the [examples docs](examples/index.m
40
40
41
41
## Slim Install
42
42
43
-
If you know which model you're going to use and want to avoid installing superfluous package, you can use the [`pydantic-ai-slim`](https://pypi.org/project/pydantic-ai-slim/) package.
43
+
If you know which model you're going to use and want to avoid installing superfluous packages, you can use the [`pydantic-ai-slim`](https://pypi.org/project/pydantic-ai-slim/) package.
44
44
45
45
If you're using just [`OpenAIModel`][pydantic_ai.models.openai.OpenAIModel], run:
0 commit comments