Skip to content

Commit 493f42c

Browse files
zhcn000000DouweM
andauthored
Add description arg to tool function decorators (#3153)
Co-authored-by: Douwe Maan <[email protected]>
1 parent f2b9e9f commit 493f42c

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

pydantic_ai_slim/pydantic_ai/agent/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,7 @@ def tool(
10291029
/,
10301030
*,
10311031
name: str | None = None,
1032+
description: str | None = None,
10321033
retries: int | None = None,
10331034
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
10341035
docstring_format: DocstringFormat = 'auto',
@@ -1046,6 +1047,7 @@ def tool(
10461047
/,
10471048
*,
10481049
name: str | None = None,
1050+
description: str | None = None,
10491051
retries: int | None = None,
10501052
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
10511053
docstring_format: DocstringFormat = 'auto',
@@ -1088,6 +1090,7 @@ async def spam(ctx: RunContext[str], y: float) -> float:
10881090
Args:
10891091
func: The tool function to register.
10901092
name: The name of the tool, defaults to the function name.
1093+
description: The description of the tool, defaults to the function docstring.
10911094
retries: The number of retries to allow for this tool, defaults to the agent's default retries,
10921095
which defaults to 1.
10931096
prepare: custom method to prepare the tool definition for each step, return `None` to omit this
@@ -1113,6 +1116,7 @@ def tool_decorator(
11131116
func_,
11141117
takes_ctx=True,
11151118
name=name,
1119+
description=description,
11161120
retries=retries,
11171121
prepare=prepare,
11181122
docstring_format=docstring_format,
@@ -1136,6 +1140,7 @@ def tool_plain(
11361140
/,
11371141
*,
11381142
name: str | None = None,
1143+
description: str | None = None,
11391144
retries: int | None = None,
11401145
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
11411146
docstring_format: DocstringFormat = 'auto',
@@ -1153,6 +1158,7 @@ def tool_plain(
11531158
/,
11541159
*,
11551160
name: str | None = None,
1161+
description: str | None = None,
11561162
retries: int | None = None,
11571163
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
11581164
docstring_format: DocstringFormat = 'auto',
@@ -1195,6 +1201,7 @@ async def spam(ctx: RunContext[str]) -> float:
11951201
Args:
11961202
func: The tool function to register.
11971203
name: The name of the tool, defaults to the function name.
1204+
description: The description of the tool, defaults to the function docstring.
11981205
retries: The number of retries to allow for this tool, defaults to the agent's default retries,
11991206
which defaults to 1.
12001207
prepare: custom method to prepare the tool definition for each step, return `None` to omit this
@@ -1218,6 +1225,7 @@ def tool_decorator(func_: ToolFuncPlain[ToolParams]) -> ToolFuncPlain[ToolParams
12181225
func_,
12191226
takes_ctx=False,
12201227
name=name,
1228+
description=description,
12211229
retries=retries,
12221230
prepare=prepare,
12231231
docstring_format=docstring_format,

pydantic_ai_slim/pydantic_ai/toolsets/function.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def tool(
109109
/,
110110
*,
111111
name: str | None = None,
112+
description: str | None = None,
112113
retries: int | None = None,
113114
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
114115
docstring_format: DocstringFormat | None = None,
@@ -126,6 +127,7 @@ def tool(
126127
/,
127128
*,
128129
name: str | None = None,
130+
description: str | None = None,
129131
retries: int | None = None,
130132
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
131133
docstring_format: DocstringFormat | None = None,
@@ -169,6 +171,7 @@ async def spam(ctx: RunContext[str], y: float) -> float:
169171
Args:
170172
func: The tool function to register.
171173
name: The name of the tool, defaults to the function name.
174+
description: The description of the tool,defaults to the function docstring.
172175
retries: The number of retries to allow for this tool, defaults to the agent's default retries,
173176
which defaults to 1.
174177
prepare: custom method to prepare the tool definition for each step, return `None` to omit this
@@ -197,18 +200,19 @@ def tool_decorator(
197200
) -> ToolFuncEither[AgentDepsT, ToolParams]:
198201
# noinspection PyTypeChecker
199202
self.add_function(
200-
func_,
201-
None,
202-
name,
203-
retries,
204-
prepare,
205-
docstring_format,
206-
require_parameter_descriptions,
207-
schema_generator,
208-
strict,
209-
sequential,
210-
requires_approval,
211-
metadata,
203+
func=func_,
204+
takes_ctx=None,
205+
name=name,
206+
description=description,
207+
retries=retries,
208+
prepare=prepare,
209+
docstring_format=docstring_format,
210+
require_parameter_descriptions=require_parameter_descriptions,
211+
schema_generator=schema_generator,
212+
strict=strict,
213+
sequential=sequential,
214+
requires_approval=requires_approval,
215+
metadata=metadata,
212216
)
213217
return func_
214218

@@ -219,6 +223,7 @@ def add_function(
219223
func: ToolFuncEither[AgentDepsT, ToolParams],
220224
takes_ctx: bool | None = None,
221225
name: str | None = None,
226+
description: str | None = None,
222227
retries: int | None = None,
223228
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
224229
docstring_format: DocstringFormat | None = None,
@@ -240,6 +245,7 @@ def add_function(
240245
func: The tool function to register.
241246
takes_ctx: Whether the function takes a [`RunContext`][pydantic_ai.tools.RunContext] as its first argument. If `None`, this is inferred from the function signature.
242247
name: The name of the tool, defaults to the function name.
248+
description: The description of the tool, defaults to the function docstring.
243249
retries: The number of retries to allow for this tool, defaults to the agent's default retries,
244250
which defaults to 1.
245251
prepare: custom method to prepare the tool definition for each step, return `None` to omit this
@@ -279,6 +285,7 @@ def add_function(
279285
func,
280286
takes_ctx=takes_ctx,
281287
name=name,
288+
description=description,
282289
max_retries=retries,
283290
prepare=prepare,
284291
docstring_format=docstring_format,

0 commit comments

Comments
 (0)