Skip to content

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

fix(dialog): guard async_ask() against empty or invalid kb_ids (#15530)

bohdansolovie381091df71c2
diff --git a/api/db/services/dialog_service.py b/api/db/services/dialog_service.py
index b4af21b587..72e8c1cf1d 100644
--- a/api/db/services/dialog_service.py
+++ b/api/db/services/dialog_service.py
@@ -1600,6 +1600,14 @@ async def async_ask(question, kb_ids, tenant_id, chat_llm_name=None, search_conf
include_reference_metadata, metadata_fields = _resolve_reference_metadata(search_config)
kbs = KnowledgebaseService.get_by_ids(kb_ids)
+ if not kbs:
+ if not kb_ids:
+ error = "**ERROR**: No KB selected"
+ else:
+ error = "**ERROR**: The selected KB is not valid"
+ yield {"answer": error, "reference": {}, "final": True}
+ return
+
embedding_list = list(set([kb.embd_id for kb in kbs]))
is_knowledge_graph = all([kb.parser_id == ParserType.KG for kb in kbs])
diff --git a/test/unit_test/api/db/services/test_dialog_service_final_answer.py b/test/unit_test/api/db/services/test_dialog_service_final_answer.py
index 44b2bf570a..7fefb323a9 100644
--- a/test/unit_test/api/db/services/test_dialog_service_final_answer.py
+++ b/test/unit_test/api/db/services/test_dialog_service_final_answer.py
@@ -292,6 +292,53 @@ def test_async_ask_delta_events_carry_incremental_text_only(monkeypatch):
)
+def test_async_ask_empty_kb_ids_yields_error_final_event(monkeypatch):
+ """
+ When kb_ids is empty, async_ask() must not crash with IndexError on kbs[0].
+ """
+ monkeypatch.setattr(
+ dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: []
+ )
+
+ events = _collect(
+ dialog_service.async_ask(
+ question="What is RAGFlow?",
+ kb_ids=[],
+ tenant_id="tenant-1",
+ )
+ )
+
+ assert len(events) == 1
+ final = events[0]
+ assert final.get("final") is True
+ assert "No KB selected" in final["answer"]
+ assert final["reference"] == {}
+
+
+def test_async_ask_stale_kb_ids_yields_error_final_event(monkeypatch):
+ """Provided kb_ids that do not resolve to any KB should report invalid selection."""
+ monkeypatch.setattr(
+ dialog_service.KnowledgebaseService,
+ "get_by_ids",
+ lambda ids: [] if ids == ["deleted-kb"] else [_KB],
+ )
+
+ events = _collect(
+ dialog_service.async_ask(
+ question="What is RAGFlow?",
+ kb_ids=["deleted-kb"],
+ tenant_id="tenant-1",
+ )
+ )
+
+ assert len(events) == 1
+ assert events[0].get("final") is True
+ assert "not valid" in events[0]["answer"]
+ assert events[0]["reference"] == {}
+
+
# ---------------------------------------------------------------------------
# Tests for async_chat (production code path)
# ---------------------------------------------------------------------------