From 85752791ed7ca15eb0afff6e178e72b20c3e7186 Mon Sep 17 00:00:00 2001 From: Ko Date: Thu, 16 Apr 2026 01:37:00 +0900 Subject: [PATCH] fix: resolve UnboundLocalError in post-tool empty response nudge path When a model returns an empty response after tool calls with no new tool_calls in the follow-up turn, the code enters the "nudge" recovery path which referenced `assistant_msg` before it was assigned. This variable is only set in the tool-calls branch (line 10098), but the nudge code lives in the no-tool-calls branch (line 10263+). The fix builds a fresh assistant message dict via `_build_assistant_message()` instead of reusing the unbound variable, consistent with the exhausted- retries path at line 10457. --- run_agent.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/run_agent.py b/run_agent.py index bb1c8b89..d2612c34 100644 --- a/run_agent.py +++ b/run_agent.py @@ -10945,8 +10945,9 @@ class AIAgent: # tool(result) → assistant("(empty)") → user(nudge) # Without this, we'd have tool → user which most # APIs reject as an invalid sequence. - assistant_msg["content"] = "(empty)" - messages.append(assistant_msg) + _nudge_msg = self._build_assistant_message(assistant_message, finish_reason) + _nudge_msg["content"] = "(empty)" + messages.append(_nudge_msg) messages.append({ "role": "user", "content": (