Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(rag): report empty retrieval results (#1500)

XZH7dde859eaf0b
diff --git a/deeptutor/tools/builtin/__init__.py b/deeptutor/tools/builtin/__init__.py
index e6d6df4ca..84f32562e 100644
--- a/deeptutor/tools/builtin/__init__.py
+++ b/deeptutor/tools/builtin/__init__.py
@@ -124,6 +124,11 @@ class RAGTool(_PromptHintsMixin, BaseTool):
**extra_kwargs,
)
content = result.get("answer") or result.get("content", "")
+ if not content and not result.get("error_type") and not result.get("sources"):
+ content = (
+ f"No matching content was found in knowledge base '{kb_name}'. "
+ "The search completed successfully."
+ )
return ToolResult(
content=content,
sources=_rag_sources(result, query=query, kb_name=kb_name),
diff --git a/tests/core/test_builtin_tools.py b/tests/core/test_builtin_tools.py
index e4c354363..d526b326f 100644
--- a/tests/core/test_builtin_tools.py
+++ b/tests/core/test_builtin_tools.py
@@ -330,6 +330,47 @@ async def test_rag_tool_falls_back_to_query_echo(monkeypatch: pytest.MonkeyPatch
assert result.sources == [{"type": "rag", "query": "what is a tensor", "kb_name": "demo-kb"}]
+async def test_rag_tool_reports_successful_empty_retrieval(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A no-hit search must remain visible instead of becoming empty tool output."""
+
+ async def fake_rag_search(**_kwargs: Any) -> dict[str, Any]:
+ return {"answer": "", "content": "", "sources": [], "provider": "ima"}
+
+ _install_module(monkeypatch, "deeptutor.tools.rag_tool", rag_search=fake_rag_search)
+
+ result = await RAGTool().execute(query="what is a tensor", kb_name="demo-kb")
+
+ assert result.content == (
+ "No matching content was found in knowledge base 'demo-kb'. "
+ "The search completed successfully."
+ )
+ assert "error_type" not in result.metadata
+
+
+async def test_rag_tool_does_not_disguise_errors_as_empty_results(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ async def fake_rag_search(**_kwargs: Any) -> dict[str, Any]:
+ return {
+ "answer": "",
+ "content": "",
+ "sources": [],
+ "provider": "ima",
+ "error_type": "retrieval_error",
+ }
+
+ _install_module(monkeypatch, "deeptutor.tools.rag_tool", rag_search=fake_rag_search)
+
+ result = await RAGTool().execute(query="what is a tensor", kb_name="demo-kb")
+
+ assert result.content == ""
+ assert result.metadata["error_type"] == "retrieval_error"
+
+
@pytest.mark.asyncio
async def test_rag_tool_rejects_empty_query(monkeypatch: pytest.MonkeyPatch) -> None:
called = False