99from typing import TYPE_CHECKING , Any , Callable , ClassVar , Generic , cast , final , overload
1010
1111from opentelemetry .trace import NoOpTracer , use_span
12+ from pydantic .json_schema import GenerateJsonSchema
1213from typing_extensions import TypeGuard , TypeVar , deprecated
1314
1415from pydantic_graph import End , Graph , GraphRun , GraphRunContext
3132from .tools import (
3233 AgentDepsT ,
3334 DocstringFormat ,
35+ GenerateToolJsonSchema ,
3436 RunContext ,
3537 Tool ,
3638 ToolFuncContext ,
@@ -936,6 +938,7 @@ def tool(
936938 prepare : ToolPrepareFunc [AgentDepsT ] | None = None ,
937939 docstring_format : DocstringFormat = 'auto' ,
938940 require_parameter_descriptions : bool = False ,
941+ schema_generator : type [GenerateJsonSchema ] = GenerateToolJsonSchema ,
939942 ) -> Callable [[ToolFuncContext [AgentDepsT , ToolParams ]], ToolFuncContext [AgentDepsT , ToolParams ]]: ...
940943
941944 def tool (
@@ -948,6 +951,7 @@ def tool(
948951 prepare : ToolPrepareFunc [AgentDepsT ] | None = None ,
949952 docstring_format : DocstringFormat = 'auto' ,
950953 require_parameter_descriptions : bool = False ,
954+ schema_generator : type [GenerateJsonSchema ] = GenerateToolJsonSchema ,
951955 ) -> Any :
952956 """Decorator to register a tool function which takes [`RunContext`][pydantic_ai.tools.RunContext] as its first argument.
953957
@@ -989,6 +993,7 @@ async def spam(ctx: RunContext[str], y: float) -> float:
989993 docstring_format: The format of the docstring, see [`DocstringFormat`][pydantic_ai.tools.DocstringFormat].
990994 Defaults to `'auto'`, such that the format is inferred from the structure of the docstring.
991995 require_parameter_descriptions: If True, raise an error if a parameter description is missing. Defaults to False.
996+ schema_generator: The JSON schema generator class to use for this tool. Defaults to `GenerateToolJsonSchema`.
992997 """
993998 if func is None :
994999
@@ -997,15 +1002,22 @@ def tool_decorator(
9971002 ) -> ToolFuncContext [AgentDepsT , ToolParams ]:
9981003 # noinspection PyTypeChecker
9991004 self ._register_function (
1000- func_ , True , name , retries , prepare , docstring_format , require_parameter_descriptions
1005+ func_ ,
1006+ True ,
1007+ name ,
1008+ retries ,
1009+ prepare ,
1010+ docstring_format ,
1011+ require_parameter_descriptions ,
1012+ schema_generator ,
10011013 )
10021014 return func_
10031015
10041016 return tool_decorator
10051017 else :
10061018 # noinspection PyTypeChecker
10071019 self ._register_function (
1008- func , True , name , retries , prepare , docstring_format , require_parameter_descriptions
1020+ func , True , name , retries , prepare , docstring_format , require_parameter_descriptions , schema_generator
10091021 )
10101022 return func
10111023
@@ -1022,6 +1034,7 @@ def tool_plain(
10221034 prepare : ToolPrepareFunc [AgentDepsT ] | None = None ,
10231035 docstring_format : DocstringFormat = 'auto' ,
10241036 require_parameter_descriptions : bool = False ,
1037+ schema_generator : type [GenerateJsonSchema ] = GenerateToolJsonSchema ,
10251038 ) -> Callable [[ToolFuncPlain [ToolParams ]], ToolFuncPlain [ToolParams ]]: ...
10261039
10271040 def tool_plain (
@@ -1034,6 +1047,7 @@ def tool_plain(
10341047 prepare : ToolPrepareFunc [AgentDepsT ] | None = None ,
10351048 docstring_format : DocstringFormat = 'auto' ,
10361049 require_parameter_descriptions : bool = False ,
1050+ schema_generator : type [GenerateJsonSchema ] = GenerateToolJsonSchema ,
10371051 ) -> Any :
10381052 """Decorator to register a tool function which DOES NOT take `RunContext` as an argument.
10391053
@@ -1075,20 +1089,28 @@ async def spam(ctx: RunContext[str]) -> float:
10751089 docstring_format: The format of the docstring, see [`DocstringFormat`][pydantic_ai.tools.DocstringFormat].
10761090 Defaults to `'auto'`, such that the format is inferred from the structure of the docstring.
10771091 require_parameter_descriptions: If True, raise an error if a parameter description is missing. Defaults to False.
1092+ schema_generator: The JSON schema generator class to use for this tool. Defaults to `GenerateToolJsonSchema`.
10781093 """
10791094 if func is None :
10801095
10811096 def tool_decorator (func_ : ToolFuncPlain [ToolParams ]) -> ToolFuncPlain [ToolParams ]:
10821097 # noinspection PyTypeChecker
10831098 self ._register_function (
1084- func_ , False , name , retries , prepare , docstring_format , require_parameter_descriptions
1099+ func_ ,
1100+ False ,
1101+ name ,
1102+ retries ,
1103+ prepare ,
1104+ docstring_format ,
1105+ require_parameter_descriptions ,
1106+ schema_generator ,
10851107 )
10861108 return func_
10871109
10881110 return tool_decorator
10891111 else :
10901112 self ._register_function (
1091- func , False , name , retries , prepare , docstring_format , require_parameter_descriptions
1113+ func , False , name , retries , prepare , docstring_format , require_parameter_descriptions , schema_generator
10921114 )
10931115 return func
10941116
@@ -1101,6 +1123,7 @@ def _register_function(
11011123 prepare : ToolPrepareFunc [AgentDepsT ] | None ,
11021124 docstring_format : DocstringFormat ,
11031125 require_parameter_descriptions : bool ,
1126+ schema_generator : type [GenerateJsonSchema ],
11041127 ) -> None :
11051128 """Private utility to register a function as a tool."""
11061129 retries_ = retries if retries is not None else self ._default_retries
@@ -1112,6 +1135,7 @@ def _register_function(
11121135 prepare = prepare ,
11131136 docstring_format = docstring_format ,
11141137 require_parameter_descriptions = require_parameter_descriptions ,
1138+ schema_generator = schema_generator ,
11151139 )
11161140 self ._register_tool (tool )
11171141
0 commit comments