Skip to content

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

fix(chat): keep reasoning chats grounded when LLM lacks tool calling (#18983)

georgelichenf300b154b0b0
diff --git a/api/db/services/dialog_service.py b/api/db/services/dialog_service.py
index 189bad8924..17c77f4b58 100644
--- a/api/db/services/dialog_service.py
+++ b/api/db/services/dialog_service.py
@@ -1959,6 +1959,21 @@ async def rag_agent(dialog, messages, stream=True, **kwargs):
yield ans
return
kbs, embd_mdl, rerank_mdl, chat_mdl, tts_mdl = get_models(dialog)
+
+ # Agentic RAG depends on the outer model being able to call the bound
+ # ``rag`` tool. Models without tool-calling support would otherwise receive
+ # only the router prompt and could answer from their own knowledge without
+ # ever running retrieval. Reuse the regular RAG path for those models so
+ # the configured knowledge base and citation flow remain authoritative.
+ if not getattr(chat_mdl, "is_tools", False):
+ logging.info("LLM does not support tool calls; falling back to regular RAG chat")
+ fallback_kwargs = dict(kwargs)
+ if isinstance(fallback_kwargs.get("doc_ids"), list):
+ fallback_kwargs["doc_ids"] = ",".join(str(doc_id) for doc_id in fallback_kwargs["doc_ids"] if doc_id)
+ async for ans in async_chat(dialog, messages, stream, **fallback_kwargs):
+ yield ans
+ return
+
model_type = chat_mdl.model_config["model_type"]
factory = chat_mdl.model_config.get("llm_factory", "") if chat_mdl.model_config else ""
text_attachments_content, image_attachments, image_files = get_files_content(messages[-1], model_type)
diff --git a/test/unit_test/api/db/services/test_dialog_service_rag_agent_messages.py b/test/unit_test/api/db/services/test_dialog_service_rag_agent_messages.py
index 38d50d78da..86a7c18dfb 100644
--- a/test/unit_test/api/db/services/test_dialog_service_rag_agent_messages.py
+++ b/test/unit_test/api/db/services/test_dialog_service_rag_agent_messages.py
@@ -88,6 +88,7 @@ class _RecordingChatModel:
"""Records the message list rag_agent() hands to the provider."""
def __init__(self):
+ self.is_tools = True
self.model_config = {"model_type": "chat", "llm_factory": "OpenAI"}
self.mdl = None
self.sent_messages = None
@@ -123,6 +124,43 @@ def _drive_rag_agent(monkeypatch, messages):
return chat_mdl
+def test_rag_agent_falls_back_to_retrieval_when_model_has_no_tools(monkeypatch):
+ """Reasoning must not bypass KB retrieval for models without tool support."""
+
+ class _NoToolsChatModel(_RecordingChatModel):
+ def __init__(self):
+ super().__init__()
+ self.is_tools = False
+
+ chat_mdl = _NoToolsChatModel()
+ fallback_calls = []
+
+ async def _fallback(_dialog, _messages, stream=True, **_kwargs):
+ fallback_calls.append((stream, _kwargs))
+ yield {"answer": "retrieved answer", "reference": {"chunks": [{"doc_id": "doc-1"}]}}
+
+ monkeypatch.setattr(dialog_service, "get_models", lambda _dialog, **_kw: ([_KB], None, None, chat_mdl, None))
+ monkeypatch.setattr(dialog_service, "async_chat", _fallback)
+
+ async def _run():
+ return [
+ event
+ async for event in dialog_service.rag_agent(
+ _DIALOG,
+ [{"role": "user", "content": "What is RAGFlow?"}],
+ False,
+ reasoning="2",
+ doc_ids=["doc-1"],
+ )
+ ]
+
+ events = asyncio.run(_run())
+
+ assert fallback_calls == [(False, {"reasoning": "2", "doc_ids": "doc-1"})]
+ assert events[0]["answer"] == "retrieved answer"
+
+
@pytest.mark.p2
def test_rag_agent_drops_bookkeeping_keys_before_the_provider_call(monkeypatch):
"""Groq rejects any message property the chat schema does not define."""