From 9bf10b47618f846a46e535eb91d2e765cfc50507 Mon Sep 17 00:00:00 2001 From: jnjpng Date: Tue, 3 Feb 2026 15:10:24 -0800 Subject: [PATCH] fix: initialize result and run_status before try block in send_message (#9265) Initialize `result` and `run_status` variables before the try block to prevent UnboundLocalError in the finally block when exceptions occur early in execution (e.g., during AgentLoop.load()). Previously, if an exception was raised before `result` was assigned inside the try block, the finally block would fail when trying to access `result`, masking the original error with an UnboundLocalError. This fix ensures the finally block can safely check and use these variables regardless of when/where an exception occurs. --- letta/server/rest_api/routers/v1/agents.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.py index a70a9428..7e33398d 100644 --- a/letta/server/rest_api/routers/v1/agents.py +++ b/letta/server/rest_api/routers/v1/agents.py @@ -1579,6 +1579,8 @@ async def send_message( await redis_client.set(f"{REDIS_RUN_ID_PREFIX}:{agent_id}", run.id if run else None) run_update_metadata = None + result = None + run_status = None try: agent_loop = AgentLoop.load(agent_state=agent, actor=actor) result = await agent_loop.step(