From 9a193344a9fd43d4f1d69d90b5ad5994d3a8cdd7 Mon Sep 17 00:00:00 2001 From: snakelu Date: Thu, 5 Dec 2024 16:43:37 +0800 Subject: [PATCH] fix: early return tool_schema_list in convert_tool_into_openai_schema function --- swarms/tools/base_tool.py | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/swarms/tools/base_tool.py b/swarms/tools/base_tool.py index dcb81974..a09c4bdc 100644 --- a/swarms/tools/base_tool.py +++ b/swarms/tools/base_tool.py @@ -387,39 +387,38 @@ class BaseTool(BaseModel): "Converting tools into OpenAI function calling schema" ) + tool_schema_list = [] # Store all tool schemas + for tool in self.tools: - # Transform the tool into a openai function calling schema - if self.check_func_if_have_docs( - tool - ) and self.check_func_if_have_type_hints(tool): + # Check if the tool has both documentation and type hints + if self.check_func_if_have_docs(tool) and self.check_func_if_have_type_hints(tool): name = tool.__name__ description = tool.__doc__ logger.info( - f"Converting tool: {name} into a OpenAI certified function calling schema. Add documentation and type hints." + f"Converting tool: {name} into OpenAI schema." ) - tool_schema_list = ( - get_openai_function_schema_from_func( - tool, name=name, description=description - ) + schema = get_openai_function_schema_from_func( + tool, name=name, description=description ) logger.info( f"Tool {name} converted successfully into OpenAI schema" ) - - # Transform the dictionary to a string - tool_schema_list = json.dumps( - tool_schema_list, indent=4 - ) - - return tool_schema_list + tool_schema_list.append(schema) else: - logger.error( - f"Tool {tool.__name__} does not have documentation or type hints, please add them to make the tool execution reliable." - ) + # Log specific errors for missing documentation or type hints + if not self.check_func_if_have_docs(tool): + logger.error( + f"Tool {tool.__name__} is missing documentation." + ) + if not self.check_func_if_have_type_hints(tool): + logger.error( + f"Tool {tool.__name__} is missing type hints." + ) - return tool_schema_list + # Return the list as a JSON string + return json.dumps(tool_schema_list, indent=4) def check_func_if_have_docs(self, func: callable): if func.__doc__ is not None: