From 9abf0b9e695ed94d76e256b3189c2a1d8dead92f Mon Sep 17 00:00:00 2001 From: lemorage Date: Sun, 9 Feb 2025 18:12:03 +0800 Subject: [PATCH 1/2] fix: include missing function arguments in error handling --- letta/agent.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/letta/agent.py b/letta/agent.py index bb082fbf7f..ce36173ab6 100644 --- a/letta/agent.py +++ b/letta/agent.py @@ -260,6 +260,7 @@ def _handle_function_error_response( error_msg: str, tool_call_id: str, function_name: str, + function_args: str, function_response: str, messages: List[Message], include_function_failed_message: bool = False, @@ -394,6 +395,7 @@ def _handle_ai_response( messages = [] # append these to the history when done function_name = None + function_args = {} # Step 2: check if LLM wanted to call a function if response_message.function_call or (response_message.tool_calls is not None and len(response_message.tool_calls) > 0): @@ -459,7 +461,7 @@ def _handle_ai_response( if not target_letta_tool: error_msg = f"No function named {function_name}" function_response = "None" # more like "never ran?" - messages = self._handle_function_error_response(error_msg, tool_call_id, function_name, function_response, messages) + messages = self._handle_function_error_response(error_msg, tool_call_id, function_name, function_args, function_response, messages) return messages, False, True # force a heartbeat to allow agent to handle error # Failure case 2: function name is OK, but function args are bad JSON @@ -469,7 +471,7 @@ def _handle_ai_response( except Exception: error_msg = f"Error parsing JSON for function '{function_name}' arguments: {function_call.arguments}" function_response = "None" # more like "never ran?" - messages = self._handle_function_error_response(error_msg, tool_call_id, function_name, function_response, messages) + messages = self._handle_function_error_response(error_msg, tool_call_id, function_name, function_args, function_response, messages) return messages, False, True # force a heartbeat to allow agent to handle error # Check if inner thoughts is in the function call arguments (possible apparently if you are using Azure) @@ -506,7 +508,7 @@ def _handle_ai_response( if sandbox_run_result and sandbox_run_result.status == "error": messages = self._handle_function_error_response( - function_response, tool_call_id, function_name, function_response, messages + function_response, tool_call_id, function_name, function_args, function_response, messages ) return messages, False, True # force a heartbeat to allow agent to handle error @@ -535,7 +537,7 @@ def _handle_ai_response( error_msg_user = f"{error_msg}\n{traceback.format_exc()}" self.logger.error(error_msg_user) messages = self._handle_function_error_response( - error_msg, tool_call_id, function_name, function_response, messages, include_function_failed_message=True + error_msg, tool_call_id, function_name, function_args, function_response, messages, include_function_failed_message=True ) return messages, False, True # force a heartbeat to allow agent to handle error @@ -543,7 +545,7 @@ def _handle_ai_response( if function_response_string.startswith(ERROR_MESSAGE_PREFIX): error_msg = function_response_string messages = self._handle_function_error_response( - error_msg, tool_call_id, function_name, function_response, messages, include_function_failed_message=True + error_msg, tool_call_id, function_name, function_args, function_response, messages, include_function_failed_message=True ) return messages, False, True # force a heartbeat to allow agent to handle error From 059aa3d085c795441fceca662ee3944073bd1434 Mon Sep 17 00:00:00 2001 From: lemorage Date: Tue, 11 Feb 2025 09:22:16 +0800 Subject: [PATCH 2/2] fix: change function_args type from str to dict --- letta/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/letta/agent.py b/letta/agent.py index ce36173ab6..7b4b94b8a0 100644 --- a/letta/agent.py +++ b/letta/agent.py @@ -260,7 +260,7 @@ def _handle_function_error_response( error_msg: str, tool_call_id: str, function_name: str, - function_args: str, + function_args: dict, function_response: str, messages: List[Message], include_function_failed_message: bool = False,