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

This commit is contained in:
XZH
2026-09-18 16:35:55 -07:00
parent 897fce52f2
commit 7dde859eaf
2 changed files with 46 additions and 0 deletions

View File

@@ -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),

View File

@@ -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"}]
@pytest.mark.asyncio
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
@pytest.mark.asyncio
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