Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix: guard LLM response against empty choices (fixes #14711) (#14988)

Jonathan Hill111cdc77b55c
diff --git a/rag/llm/chat_model.py b/rag/llm/chat_model.py
index 5edff15c4c..7c1652ddb0 100644
--- a/rag/llm/chat_model.py
+++ b/rag/llm/chat_model.py
@@ -396,7 +396,7 @@ class Base(ABC):
logging.info(f"{self.tools=}")
response = await self.async_client.chat.completions.create(model=self.model_name, messages=history, tools=self.tools, tool_choice="auto", **gen_conf)
tk_count += total_token_count_from_response(response)
- if any([not response.choices, not response.choices[0].message]):
+ if not response.choices or not response.choices[0].message:
raise Exception(f"500 response structure error. Response: {response}")
if not hasattr(response.choices[0].message, "tool_calls") or not response.choices[0].message.tool_calls:
@@ -685,6 +685,8 @@ class BaiChuanChat(Base):
extra_body={"tools": [{"type": "web_search", "web_search": {"enable": True, "search_mode": "performance_first"}}]},
**gen_conf,
)
+ if not response.choices:
+ raise ValueError("LLM returned empty response") # pact: guard empty choices list
ans = response.choices[0].message.content.strip()
if response.choices[0].finish_reason == "length":
if is_chinese([ans]):
@@ -831,6 +833,8 @@ class MistralChat(Base):
gen_conf = dict(gen_conf or {})
gen_conf = self._clean_conf(gen_conf)
response = self.client.chat(model=self.model_name, messages=history, **gen_conf)
+ if not response.choices:
+ raise ValueError("LLM returned empty response") # pact: guard empty choices list
ans = response.choices[0].message.content
if response.choices[0].finish_reason == "length":
if is_chinese(ans):
@@ -1419,7 +1423,7 @@ class LiteLLMBase(ABC):
timeout=self.timeout,
)
- if any([not response.choices, not response.choices[0].message, not response.choices[0].message.content]):
+ if not response.choices or not response.choices[0].message or not response.choices[0].message.content:
return "", 0
ans = response.choices[0].message.content.strip()
if response.choices[0].finish_reason == "length":
diff --git a/rag/llm/cv_model.py b/rag/llm/cv_model.py
index 728f1677d2..b362365276 100644
--- a/rag/llm/cv_model.py
+++ b/rag/llm/cv_model.py
@@ -265,6 +265,8 @@ class GptV4(Base):
messages=self.prompt(b64),
extra_body=self.extra_body
)
+ if not res.choices:
+ raise ValueError("LLM returned empty response") # pact: guard empty choices list
return res.choices[0].message.content.strip(), total_token_count_from_response(res)
def describe_with_prompt(self, image, prompt=None):
@@ -274,6 +276,8 @@ class GptV4(Base):
messages=self.vision_llm_prompt(b64, prompt),
extra_body=self.extra_body,
)
+ if not res.choices:
+ raise ValueError("LLM returned empty response") # pact: guard empty choices list
return res.choices[0].message.content.strip(), total_token_count_from_response(res)
@@ -507,6 +511,8 @@ class Zhipu4V(GptV4):
resp = self.client.chat.completions.create(model=self.model_name, messages=messages, stream=False)
+ if not resp.choices:
+ raise ValueError("LLM returned empty response") # pact: guard empty choices list
content = resp.choices[0].message.content.strip()
cleaned = re.sub(r"<\|(begin_of_box|end_of_box)\|>", "", content).strip()