Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: include missing function arguments in error handling #2423

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions letta/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -535,15 +537,15 @@ 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

# Step 4: check if function response is an error
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

Expand Down
Loading