Skip to content

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

fix(agentic_rag): normalize structure answers (#19025)

Loongd7e453e01a27
diff --git a/rag/advanced_rag/harness/structure_qa.py b/rag/advanced_rag/harness/structure_qa.py
index f440464de2..999ba01a71 100644
--- a/rag/advanced_rag/harness/structure_qa.py
+++ b/rag/advanced_rag/harness/structure_qa.py
@@ -88,7 +88,8 @@ async def _ask_structure(tools, topic: str, entities: list[dict], relations: lis
_LOG.exception(f"[{label}] Could not read the outline with the model.")
is_sufficient = bool(verdict.get("is_sufficient"))
- answer = (verdict.get("answer") or "").strip() if is_sufficient else ""
+ raw_answer = verdict.get("answer")
+ answer = str(raw_answer).strip() if is_sufficient and raw_answer is not None else ""
relevant = [n for n in (verdict.get("relevant_entities") or []) if isinstance(n, str)]
_LOG.info(
"[%s] The %s %s the question; %d relevant entity(ies): %s",
diff --git a/test/unit_test/rag/advanced_rag/test_structure_qa.py b/test/unit_test/rag/advanced_rag/test_structure_qa.py
new file mode 100644
index 0000000000..76a947738c
--- /dev/null
+++ b/test/unit_test/rag/advanced_rag/test_structure_qa.py
@@ -0,0 +1,45 @@
+import sys
+from types import ModuleType, SimpleNamespace
+
+import pytest
+
+from rag.advanced_rag.harness.structure_qa import _ask_structure
+
+
+async def test_ask_structure_normalizes_non_string_answer(monkeypatch):
+ generator = ModuleType("rag.prompts.generator")
+ generator.form_message = lambda system, user: [
+ {"role": "system", "content": system},
+ {"role": "user", "content": user},
+ ]
+ generator.message_fit_in = lambda messages, _budget: (0, messages)
+
+ prompts = ModuleType("rag.prompts")
+ prompts.__path__ = []
+ prompts.generator = generator
+ monkeypatch.setitem(sys.modules, "rag.prompts", prompts)
+ monkeypatch.setitem(sys.modules, "rag.prompts.generator", generator)
+
+ class FakeChatModel:
+ max_length = 8192
+
+ async def async_chat(self, system, messages, config):
+ assert system
+ assert messages
+ assert config == {"temperature": 0.2}
+ return '{"is_sufficient": true, "answer": 42, "relevant_entities": ["Revenue", 7]}'
+
+ tools = SimpleNamespace(chat_mdl=FakeChatModel())
+
+ answer, relevant = await _ask_structure(
+ tools,
+ "What is the revenue?",
+ [{"name": "Revenue", "type": "metric", "description": "Annual revenue is 42."}],
+ [],
+ "outline",
+ "Structure test",
+ )
+
+ assert answer == "42"
+ assert relevant == ["Revenue"]