From b1ddad0808525bd2ccf1b71cc6cae9376a98ae46 Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Tue, 14 Apr 2026 03:34:36 +0000 Subject: [PATCH] Fix intermittent UnboundLocalError for 'thinking' variable in ConversationalAgent.chat() - initialize thinking=None before conditional assignment to handle API responses missing message field --- src/backend/app/services/ai_agent/agent.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/backend/app/services/ai_agent/agent.py b/src/backend/app/services/ai_agent/agent.py index 5fd5360..a01130d 100644 --- a/src/backend/app/services/ai_agent/agent.py +++ b/src/backend/app/services/ai_agent/agent.py @@ -894,6 +894,10 @@ class ConversationalAgent: result = resp + # Initialize thinking to None to handle cases where API response + # doesn't have the expected message structure (intermittent issue) + thinking = None + if result.get("choices") and len(result.get("choices", [])) > 0: choice = result["choices"][0] if "message" in choice: