@@ -45,31 +45,49 @@ def __init__(
45
45
tools : Sequence [Tool [AgentDepsT ] | ToolFuncEither [AgentDepsT , ...]] = [],
46
46
* ,
47
47
max_retries : int = 1 ,
48
- id : str | None = None ,
49
48
docstring_format : DocstringFormat = 'auto' ,
50
49
require_parameter_descriptions : bool = False ,
51
50
schema_generator : type [GenerateJsonSchema ] = GenerateToolJsonSchema ,
51
+ strict : bool | None = None ,
52
+ sequential : bool = False ,
53
+ requires_approval : bool = False ,
54
+ metadata : dict [str , Any ] | None = None ,
55
+ id : str | None = None ,
52
56
):
53
57
"""Build a new function toolset.
54
58
55
59
Args:
56
60
tools: The tools to add to the toolset.
57
61
max_retries: The maximum number of retries for each tool during a run.
58
- id: An optional unique ID for the toolset. A toolset needs to have an ID in order to be used in a durable execution environment like Temporal,
59
- in which case the ID will be used to identify the toolset's activities within the workflow.
62
+ Applies to all tools, unless overridden when adding a tool.
60
63
docstring_format: Format of tool docstring, see [`DocstringFormat`][pydantic_ai.tools.DocstringFormat].
61
64
Defaults to `'auto'`, such that the format is inferred from the structure of the docstring.
62
65
Applies to all tools, unless overridden when adding a tool.
63
66
require_parameter_descriptions: If True, raise an error if a parameter description is missing. Defaults to False.
64
67
Applies to all tools, unless overridden when adding a tool.
65
68
schema_generator: The JSON schema generator class to use for this tool. Defaults to `GenerateToolJsonSchema`.
66
69
Applies to all tools, unless overridden when adding a tool.
70
+ strict: Whether to enforce JSON schema compliance (only affects OpenAI).
71
+ See [`ToolDefinition`][pydantic_ai.tools.ToolDefinition] for more info.
72
+ sequential: Whether the function requires a sequential/serial execution environment. Defaults to False.
73
+ Applies to all tools, unless overridden when adding a tool.
74
+ requires_approval: Whether this tool requires human-in-the-loop approval. Defaults to False.
75
+ See the [tools documentation](../deferred-tools.md#human-in-the-loop-tool-approval) for more info.
76
+ Applies to all tools, unless overridden when adding a tool.
77
+ metadata: Optional metadata for the tool. This is not sent to the model but can be used for filtering and tool behavior customization.
78
+ Applies to all tools, unless overridden when adding a tool, which will be merged with the toolset's metadata.
79
+ id: An optional unique ID for the toolset. A toolset needs to have an ID in order to be used in a durable execution environment like Temporal,
80
+ in which case the ID will be used to identify the toolset's activities within the workflow.
67
81
"""
68
82
self .max_retries = max_retries
69
83
self ._id = id
70
84
self .docstring_format = docstring_format
71
85
self .require_parameter_descriptions = require_parameter_descriptions
72
86
self .schema_generator = schema_generator
87
+ self .strict = strict
88
+ self .sequential = sequential
89
+ self .requires_approval = requires_approval
90
+ self .metadata = metadata
73
91
74
92
self .tools = {}
75
93
for tool in tools :
@@ -97,8 +115,8 @@ def tool(
97
115
require_parameter_descriptions : bool | None = None ,
98
116
schema_generator : type [GenerateJsonSchema ] | None = None ,
99
117
strict : bool | None = None ,
100
- sequential : bool = False ,
101
- requires_approval : bool = False ,
118
+ sequential : bool | None = None ,
119
+ requires_approval : bool | None = None ,
102
120
metadata : dict [str , Any ] | None = None ,
103
121
) -> Callable [[ToolFuncEither [AgentDepsT , ToolParams ]], ToolFuncEither [AgentDepsT , ToolParams ]]: ...
104
122
@@ -114,8 +132,8 @@ def tool(
114
132
require_parameter_descriptions : bool | None = None ,
115
133
schema_generator : type [GenerateJsonSchema ] | None = None ,
116
134
strict : bool | None = None ,
117
- sequential : bool = False ,
118
- requires_approval : bool = False ,
135
+ sequential : bool | None = None ,
136
+ requires_approval : bool | None = None ,
119
137
metadata : dict [str , Any ] | None = None ,
120
138
) -> Any :
121
139
"""Decorator to register a tool function which takes [`RunContext`][pydantic_ai.tools.RunContext] as its first argument.
@@ -165,10 +183,14 @@ async def spam(ctx: RunContext[str], y: float) -> float:
165
183
If `None`, the default value is determined by the toolset.
166
184
strict: Whether to enforce JSON schema compliance (only affects OpenAI).
167
185
See [`ToolDefinition`][pydantic_ai.tools.ToolDefinition] for more info.
186
+ If `None`, the default value is determined by the toolset.
168
187
sequential: Whether the function requires a sequential/serial execution environment. Defaults to False.
188
+ If `None`, the default value is determined by the toolset.
169
189
requires_approval: Whether this tool requires human-in-the-loop approval. Defaults to False.
170
190
See the [tools documentation](../deferred-tools.md#human-in-the-loop-tool-approval) for more info.
191
+ If `None`, the default value is determined by the toolset.
171
192
metadata: Optional metadata for the tool. This is not sent to the model but can be used for filtering and tool behavior customization.
193
+ If `None`, the default value is determined by the toolset. If provided, it will be merged with the toolset's metadata.
172
194
"""
173
195
174
196
def tool_decorator (
@@ -204,8 +226,8 @@ def add_function(
204
226
require_parameter_descriptions : bool | None = None ,
205
227
schema_generator : type [GenerateJsonSchema ] | None = None ,
206
228
strict : bool | None = None ,
207
- sequential : bool = False ,
208
- requires_approval : bool = False ,
229
+ sequential : bool | None = None ,
230
+ requires_approval : bool | None = None ,
209
231
metadata : dict [str , Any ] | None = None ,
210
232
) -> None :
211
233
"""Add a function as a tool to the toolset.
@@ -232,17 +254,27 @@ def add_function(
232
254
If `None`, the default value is determined by the toolset.
233
255
strict: Whether to enforce JSON schema compliance (only affects OpenAI).
234
256
See [`ToolDefinition`][pydantic_ai.tools.ToolDefinition] for more info.
257
+ If `None`, the default value is determined by the toolset.
235
258
sequential: Whether the function requires a sequential/serial execution environment. Defaults to False.
259
+ If `None`, the default value is determined by the toolset.
236
260
requires_approval: Whether this tool requires human-in-the-loop approval. Defaults to False.
237
261
See the [tools documentation](../deferred-tools.md#human-in-the-loop-tool-approval) for more info.
262
+ If `None`, the default value is determined by the toolset.
238
263
metadata: Optional metadata for the tool. This is not sent to the model but can be used for filtering and tool behavior customization.
264
+ If `None`, the default value is determined by the toolset. If provided, it will be merged with the toolset's metadata.
239
265
"""
240
266
if docstring_format is None :
241
267
docstring_format = self .docstring_format
242
268
if require_parameter_descriptions is None :
243
269
require_parameter_descriptions = self .require_parameter_descriptions
244
270
if schema_generator is None :
245
271
schema_generator = self .schema_generator
272
+ if strict is None :
273
+ strict = self .strict
274
+ if sequential is None :
275
+ sequential = self .sequential
276
+ if requires_approval is None :
277
+ requires_approval = self .requires_approval
246
278
247
279
tool = Tool [AgentDepsT ](
248
280
func ,
@@ -270,6 +302,8 @@ def add_tool(self, tool: Tool[AgentDepsT]) -> None:
270
302
raise UserError (f'Tool name conflicts with existing tool: { tool .name !r} ' )
271
303
if tool .max_retries is None :
272
304
tool .max_retries = self .max_retries
305
+ if self .metadata is not None :
306
+ tool .metadata = self .metadata | (tool .metadata or {})
273
307
self .tools [tool .name ] = tool
274
308
275
309
async def get_tools (self , ctx : RunContext [AgentDepsT ]) -> dict [str , ToolsetTool [AgentDepsT ]]:
0 commit comments