@@ -1687,7 +1687,6 @@ async def _astep_non_streaming_task(
16871687 return self ._step_terminate (
16881688 e .args [1 ], tool_call_records , "max_tokens_exceeded"
16891689 )
1690-
16911690 response = await self ._aget_model_response (
16921691 openai_messages ,
16931692 num_tokens = num_tokens ,
@@ -3770,7 +3769,7 @@ def clone(self, with_memory: bool = False) -> ChatAgent:
37703769 self .memory .get_context_creator (), "token_limit" , None
37713770 ),
37723771 output_language = self ._output_language ,
3773- tools = cloned_tools ,
3772+ tools = cast ( List [ Union [ FunctionTool , Callable ]], cloned_tools ) ,
37743773 toolkits_to_register_agent = toolkits_to_register ,
37753774 external_tools = [
37763775 schema for schema in self ._external_tool_schemas .values ()
@@ -3799,9 +3798,7 @@ def clone(self, with_memory: bool = False) -> ChatAgent:
37993798
38003799 def _clone_tools (
38013800 self ,
3802- ) -> Tuple [
3803- List [Union [FunctionTool , Callable ]], List [RegisteredAgentToolkit ]
3804- ]:
3801+ ) -> Tuple [List [FunctionTool ], List [RegisteredAgentToolkit ]]:
38053802 r"""Clone tools and return toolkits that need agent registration.
38063803
38073804 This method handles stateful toolkits by cloning them if they have
@@ -3858,13 +3855,45 @@ def _clone_tools(
38583855 method_name = tool .func .__name__
38593856 if hasattr (toolkit , method_name ):
38603857 new_method = getattr (toolkit , method_name )
3861- cloned_tools .append (new_method )
3858+ # Wrap cloned method into a new FunctionTool,
3859+ # preserving schema
3860+ try :
3861+ new_tool = FunctionTool (
3862+ func = new_method ,
3863+ openai_tool_schema = tool .get_openai_tool_schema (),
3864+ )
3865+ cloned_tools .append (new_tool )
3866+ except Exception as e :
3867+ # If wrapping fails, fallback to wrapping the original
3868+ # function with its schema to maintain consistency
3869+ logger .warning (
3870+ f"Failed to wrap cloned toolkit "
3871+ f"method '{ method_name } ' "
3872+ f"with FunctionTool: { e } . Using original "
3873+ f"function with preserved schema instead."
3874+ )
3875+ cloned_tools .append (
3876+ FunctionTool (
3877+ func = tool .func ,
3878+ openai_tool_schema = tool .get_openai_tool_schema (),
3879+ )
3880+ )
38623881 else :
3863- # Fallback to original function
3864- cloned_tools .append (tool .func )
3882+ # Fallback to original function wrapped in FunctionTool
3883+ cloned_tools .append (
3884+ FunctionTool (
3885+ func = tool .func ,
3886+ openai_tool_schema = tool .get_openai_tool_schema (),
3887+ )
3888+ )
38653889 else :
3866- # Not a toolkit method, just use the original function
3867- cloned_tools .append (tool .func )
3890+ # Not a toolkit method, preserve FunctionTool schema directly
3891+ cloned_tools .append (
3892+ FunctionTool (
3893+ func = tool .func ,
3894+ openai_tool_schema = tool .get_openai_tool_schema (),
3895+ )
3896+ )
38683897
38693898 return cloned_tools , toolkits_to_register
38703899
0 commit comments