From 4adef5754e93f127adb5a026574e0108bf778fd7 Mon Sep 17 00:00:00 2001 From: cyclop-bot <178948048+cyclop-bot@users.noreply.github.com> Date: Mon, 2 Jun 2025 15:56:43 -0500 Subject: [PATCH] Fix: Handle dict input for tool arguments in call_tool --- base_telegram_inference_bot.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/base_telegram_inference_bot.py b/base_telegram_inference_bot.py index 23e38ab..6751e67 100644 --- a/base_telegram_inference_bot.py +++ b/base_telegram_inference_bot.py @@ -78,16 +78,30 @@ class BaseTelegramInferenceBot(ABC): def call_tool(self, function_call_name, function_call_arguments): function_name = function_call_name - try: - function_args = json.loads(function_call_arguments if function_call_arguments is not None else "{}") - except json.JSONDecodeError as e: - logging.error(f"Error decoding function call arguments for {function_call_name}: {e}. Arguments: {function_call_arguments}") - return f"Error: Malformed arguments for tool call: {e}" + function_args = None + if isinstance(function_call_arguments, dict): + function_args = function_call_arguments + elif isinstance(function_call_arguments, str): + try: + function_args = json.loads(function_call_arguments) + except json.JSONDecodeError as e: + 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}" + 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 function in tool.get_functions(): if function["function"]["name"] == function_name: 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) except Exception as e: logging.error(f"Error executing tool {function_name} with args {function_args}: {e}")