Merge pull request #193 from bucolucas/fix-search-code-type-error

Fix: Type error in tool argument handling for Anthropic
This commit is contained in:
2025-06-02 15:57:28 -05:00
committed by GitHub
+16 -2
View File
@@ -78,16 +78,30 @@ class BaseTelegramInferenceBot(ABC):
def call_tool(self, function_call_name, function_call_arguments): def call_tool(self, function_call_name, function_call_arguments):
function_name = function_call_name function_name = function_call_name
function_args = None
if isinstance(function_call_arguments, dict):
function_args = function_call_arguments
elif isinstance(function_call_arguments, str):
try: try:
function_args = json.loads(function_call_arguments if function_call_arguments is not None else "{}") function_args = json.loads(function_call_arguments)
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
logging.error(f"Error decoding function call arguments for {function_call_name}: {e}. Arguments: {function_call_arguments}") logging.error(f"Error decoding function call arguments (string) for {function_call_name}: {e}. Arguments: {function_call_arguments}")
return f"Error: Malformed arguments for tool call: {e}" return f"Error: Malformed arguments for tool call: {e}"
else: # Handle cases where arguments might be None or other unexpected types
if function_call_arguments is None:
function_args = {} # Default to empty dict if arguments are None
else:
logging.error(f"Unexpected type for function_call_arguments for {function_call_name}: {type(function_call_arguments)}. Arguments: {function_call_arguments}")
return f"Error: Invalid argument type for tool call: {type(function_call_arguments)}"
for tool in self.tools: for tool in self.tools:
for function in tool.get_functions(): for function in tool.get_functions():
if function["function"]["name"] == function_name: if function["function"]["name"] == function_name:
try: try:
# Ensure function_args is a dictionary before unpacking
if not isinstance(function_args, dict):
logging.error(f"Internal error: function_args not a dict for {function_name} before execution. Args: {function_args}")
return f"Internal error preparing arguments for tool {function_name}."
return tool.execute(function_name, **function_args) return tool.execute(function_name, **function_args)
except Exception as e: except Exception as e:
logging.error(f"Error executing tool {function_name} with args {function_args}: {e}") logging.error(f"Error executing tool {function_name} with args {function_args}: {e}")