pydantic_ai.toolsets
AbstractToolset
Bases: ABC
, Generic[AgentDepsT]
A toolset is a collection of tools that can be used by an agent.
It is responsible for:
- Listing the tools it contains
- Validating the arguments of the tools
- Calling the tools
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
tool_name_conflict_hint
property
tool_name_conflict_hint: str
A hint for how to avoid name conflicts with other toolsets for use in error messages.
__aenter__
async
__aenter__() -> Self
Enter the toolset context.
This is where you can set up network connections in a concrete implementation.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
82 83 84 85 86 87 |
|
__aexit__
async
Exit the toolset context.
This is where you can tear down network connections in a concrete implementation.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
89 90 91 92 93 94 |
|
get_tools
abstractmethod
async
get_tools(
ctx: RunContext[AgentDepsT],
) -> dict[str, ToolsetTool[AgentDepsT]]
The tools that are available in this toolset.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
96 97 98 99 |
|
call_tool
abstractmethod
async
call_tool(
name: str,
tool_args: dict[str, Any],
ctx: RunContext[AgentDepsT],
tool: ToolsetTool[AgentDepsT],
) -> Any
Call a tool with the given arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the tool to call. |
required |
tool_args
|
dict[str, Any]
|
The arguments to pass to the tool. |
required |
ctx
|
RunContext[AgentDepsT]
|
The run context. |
required |
tool
|
ToolsetTool[AgentDepsT]
|
The tool definition returned by |
required |
Source code in pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
apply
apply(
visitor: Callable[[AbstractToolset[AgentDepsT]], None],
) -> None
Run a visitor function on all concrete toolsets that are not wrappers (i.e. they implement their own tool listing and calling).
Source code in pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
115 116 117 |
|
visit_and_replace
visit_and_replace(
visitor: Callable[
[AbstractToolset[AgentDepsT]],
AbstractToolset[AgentDepsT] | None,
],
) -> None
The reason we are adding this is to be able to use it to recursively turn toolsets into temporal-compatible toolsets.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
119 120 121 122 123 |
|
filtered
filtered(
filter_func: Callable[
[RunContext[AgentDepsT], ToolDefinition], bool
],
) -> FilteredToolset[AgentDepsT]
Returns a new toolset that filters this toolset's tools using a filter function that takes the agent context and the tool definition.
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
125 126 127 128 129 130 131 132 133 134 |
|
prefixed
prefixed(prefix: str) -> PrefixedToolset[AgentDepsT]
Returns a new toolset that prefixes the names of this toolset's tools.
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
136 137 138 139 140 141 142 143 |
|
prepared
prepared(
prepare_func: ToolsPrepareFunc[AgentDepsT],
) -> PreparedToolset[AgentDepsT]
Returns a new toolset that prepares this toolset's tools using a prepare function that takes the agent context and the original tool definitions.
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
145 146 147 148 149 150 151 152 |
|
renamed
renamed(
name_map: dict[str, str],
) -> RenamedToolset[AgentDepsT]
Returns a new toolset that renames this toolset's tools using a dictionary mapping new names to original names.
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
154 155 156 157 158 159 160 161 |
|
CombinedToolset
dataclass
Bases: AbstractToolset[AgentDepsT]
A toolset that combines multiple toolsets.
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/combined.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
DeferredToolset
dataclass
Bases: AbstractToolset[AgentDepsT]
A toolset that holds deferred tools whose results will be produced outside of the Pydantic AI agent run in which they were called.
See toolset docs, ToolDefinition.kind
, and DeferredToolCalls
for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/deferred.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
FilteredToolset
dataclass
Bases: WrapperToolset[AgentDepsT]
A toolset that filters the tools it contains using a filter function that takes the agent context and the tool definition.
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/filtered.py
12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
FunctionToolset
dataclass
Bases: AbstractToolset[AgentDepsT]
A toolset that lets Python functions be used as tools.
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/function.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
__init__
__init__(
tools: Sequence[
Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]
] = [],
max_retries: int = 1,
)
Build a new function toolset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tools
|
Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]]
|
The tools to add to the toolset. |
[]
|
max_retries
|
int
|
The maximum number of retries for each tool during a run. |
1
|
Source code in pydantic_ai_slim/pydantic_ai/toolsets/function.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
tool
tool(
func: ToolFuncEither[AgentDepsT, ToolParams],
) -> ToolFuncEither[AgentDepsT, ToolParams]
tool(
*,
name: str | None = None,
retries: int | None = None,
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
docstring_format: DocstringFormat = "auto",
require_parameter_descriptions: bool = False,
schema_generator: type[
GenerateJsonSchema
] = GenerateToolJsonSchema,
strict: bool | None = None
) -> Callable[
[ToolFuncEither[AgentDepsT, ToolParams]],
ToolFuncEither[AgentDepsT, ToolParams],
]
tool(
func: (
ToolFuncEither[AgentDepsT, ToolParams] | None
) = None,
/,
*,
name: str | None = None,
retries: int | None = None,
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
docstring_format: DocstringFormat = "auto",
require_parameter_descriptions: bool = False,
schema_generator: type[
GenerateJsonSchema
] = GenerateToolJsonSchema,
strict: bool | None = None,
) -> Any
Decorator to register a tool function which takes RunContext
as its first argument.
Can decorate a sync or async functions.
The docstring is inspected to extract both the tool description and description of each parameter, learn more.
We can't add overloads for every possible signature of tool, since the return type is a recursive union
so the signature of functions decorated with @toolset.tool
is obscured.
Example:
from pydantic_ai import Agent, RunContext
from pydantic_ai.toolsets.function import FunctionToolset
toolset = FunctionToolset()
@toolset.tool
def foobar(ctx: RunContext[int], x: int) -> int:
return ctx.deps + x
@toolset.tool(retries=2)
async def spam(ctx: RunContext[str], y: float) -> float:
return ctx.deps + y
agent = Agent('test', toolsets=[toolset], deps_type=int)
result = agent.run_sync('foobar', deps=1)
print(result.output)
#> {"foobar":1,"spam":1.0}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
ToolFuncEither[AgentDepsT, ToolParams] | None
|
The tool function to register. |
None
|
name
|
str | None
|
The name of the tool, defaults to the function name. |
None
|
retries
|
int | None
|
The number of retries to allow for this tool, defaults to the agent's default retries, which defaults to 1. |
None
|
prepare
|
ToolPrepareFunc[AgentDepsT] | None
|
custom method to prepare the tool definition for each step, return |
None
|
docstring_format
|
DocstringFormat
|
The format of the docstring, see |
'auto'
|
require_parameter_descriptions
|
bool
|
If True, raise an error if a parameter description is missing. Defaults to False. |
False
|
schema_generator
|
type[GenerateJsonSchema]
|
The JSON schema generator class to use for this tool. Defaults to |
GenerateToolJsonSchema
|
strict
|
bool | None
|
Whether to enforce JSON schema compliance (only affects OpenAI).
See |
None
|
Source code in pydantic_ai_slim/pydantic_ai/toolsets/function.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
add_function
add_function(
func: ToolFuncEither[AgentDepsT, ToolParams],
takes_ctx: bool | None = None,
name: str | None = None,
retries: int | None = None,
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
docstring_format: DocstringFormat = "auto",
require_parameter_descriptions: bool = False,
schema_generator: type[
GenerateJsonSchema
] = GenerateToolJsonSchema,
strict: bool | None = None,
) -> None
Add a function as a tool to the toolset.
Can take a sync or async function.
The docstring is inspected to extract both the tool description and description of each parameter, learn more.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
ToolFuncEither[AgentDepsT, ToolParams]
|
The tool function to register. |
required |
takes_ctx
|
bool | None
|
Whether the function takes a |
None
|
name
|
str | None
|
The name of the tool, defaults to the function name. |
None
|
retries
|
int | None
|
The number of retries to allow for this tool, defaults to the agent's default retries, which defaults to 1. |
None
|
prepare
|
ToolPrepareFunc[AgentDepsT] | None
|
custom method to prepare the tool definition for each step, return |
None
|
docstring_format
|
DocstringFormat
|
The format of the docstring, see |
'auto'
|
require_parameter_descriptions
|
bool
|
If True, raise an error if a parameter description is missing. Defaults to False. |
False
|
schema_generator
|
type[GenerateJsonSchema]
|
The JSON schema generator class to use for this tool. Defaults to |
GenerateToolJsonSchema
|
strict
|
bool | None
|
Whether to enforce JSON schema compliance (only affects OpenAI).
See |
None
|
Source code in pydantic_ai_slim/pydantic_ai/toolsets/function.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
add_tool
add_tool(tool: Tool[AgentDepsT]) -> None
Add a tool to the toolset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool
|
Tool[AgentDepsT]
|
The tool to add. |
required |
Source code in pydantic_ai_slim/pydantic_ai/toolsets/function.py
198 199 200 201 202 203 204 205 206 207 208 |
|
PrefixedToolset
dataclass
Bases: WrapperToolset[AgentDepsT]
A toolset that prefixes the names of the tools it contains.
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/prefixed.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
RenamedToolset
dataclass
Bases: WrapperToolset[AgentDepsT]
A toolset that renames the tools it contains using a dictionary mapping new names to original names.
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/renamed.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
PreparedToolset
dataclass
Bases: WrapperToolset[AgentDepsT]
A toolset that prepares the tools it contains using a prepare function that takes the agent context and the original tool definitions.
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/prepared.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
WrapperToolset
dataclass
Bases: AbstractToolset[AgentDepsT]
A toolset that wraps another toolset and delegates to it.
See toolset docs for more information.
Source code in pydantic_ai_slim/pydantic_ai/toolsets/wrapper.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|