77
88from prefect import tags as prefect_tags
99from prefect import task
10- from pydantic_ai .tools import Tool
1110
1211T = TypeVar ("T" )
1312
@@ -72,15 +71,12 @@ def prefect_wrapped_function(
7271 @wraps (func )
7372 async def wrapper (* args , ** kwargs ) -> T :
7473 if _progress := _progress_message .get ():
75- tool_name = "Unknown Tool"
76- if args and hasattr (args [0 ], "name" ):
77- tool_name = args [0 ].name
78- elif (
79- args
80- and hasattr (args [0 ], "function" )
81- and hasattr (args [0 ].function , "__name__" )
82- ):
83- tool_name = args [0 ].function .__name__
74+ # For call_tool method: self, name, tool_args, ctx, tool
75+ # The tool name is either in kwargs['name'] or args[1]
76+ tool_name = kwargs .get ("name" , "Unknown Tool" )
77+ if not tool_name or tool_name == "Unknown Tool" :
78+ if len (args ) > 1 :
79+ tool_name = args [1 ]
8480
8581 # Update tool usage counts
8682 counts = _tool_usage_counts .get ()
@@ -129,8 +125,8 @@ class WatchToolCalls(DecorateMethodContext):
129125
130126 def __init__ (
131127 self ,
132- patch_cls : type = Tool ,
133- patch_method_name : str = "run " ,
128+ patch_cls : type | None = None ,
129+ patch_method_name : str = "call_tool " ,
134130 tags : set [str ] | None = None ,
135131 settings : dict [str , Any ] | None = None ,
136132 ):
@@ -139,8 +135,11 @@ def __init__(
139135 tags: Prefect tags to apply to the flow.
140136 flow_kwargs: Keyword arguments to pass to the flow.
141137 """
138+ # Import here to avoid circular imports
139+ from pydantic_ai .toolsets .abstract import AbstractToolset
140+
142141 super ().__init__ (
143- patch_cls = patch_cls ,
142+ patch_cls = patch_cls or AbstractToolset ,
144143 patch_method_name = patch_method_name ,
145144 decorator = prefect_wrapped_function ,
146145 tags = tags ,
0 commit comments