From d89ad8b79d8b4375b9f552a1ac4971e3538f2f2c Mon Sep 17 00:00:00 2001 From: tuandang-diag <121849970+tuandang-diag@users.noreply.github.com> Date: Tue, 24 Feb 2026 12:15:09 +0700 Subject: [PATCH] fix: handle null response in LLM and improve JSON parsing in agent (#13187) Fixes AttributeError in _remove_reasoning_content() when LLM returns None, and improves JSON parsing regex for markdown code fences in agent_with_tools.py --- agent/component/agent_with_tools.py | 5 ++++- api/db/services/llm_service.py | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/agent/component/agent_with_tools.py b/agent/component/agent_with_tools.py index fe665ead72..55e865346a 100644 --- a/agent/component/agent_with_tools.py +++ b/agent/component/agent_with_tools.py @@ -383,7 +383,10 @@ class Agent(LLM, ToolBase): token_count += tk or 0 hist.append({"role": "assistant", "content": response}) try: - functions = json_repair.loads(re.sub(r"```.*", "", response)) + # Remove markdown code fences properly + cleaned_response = re.sub(r"^.*```json\s*", "", response, flags=re.DOTALL) + cleaned_response = re.sub(r"```\s*$", "", cleaned_response, flags=re.DOTALL) + functions = json_repair.loads(cleaned_response) if not isinstance(functions, list): raise TypeError(f"List should be returned, but `{functions}`") for f in functions: diff --git a/api/db/services/llm_service.py b/api/db/services/llm_service.py index db65ec8ecb..16d847e7f1 100644 --- a/api/db/services/llm_service.py +++ b/api/db/services/llm_service.py @@ -265,6 +265,8 @@ class LLMBundle(LLM4Tenant): generation.end() def _remove_reasoning_content(self, txt: str) -> str: + if txt is None: + return None first_think_start = txt.find("") if first_think_start == -1: return txt