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

Fixes #15529 .

### Problem

`async_ask()` accessed `kbs[0]` without verifying that
`KnowledgebaseService.get_by_ids()` returned any knowledge bases. Empty
or stale `kb_ids` raised `IndexError`, which surfaced as HTTP 500 on
search/bot SSE endpoints.

### Fix

- Add an early guard when `kbs` is empty, yielding a final SSE error
event (consistent with `gen_mindmap()` in the same module).
- Add regression tests for empty `kb_ids` and deleted/invalid KB IDs.

### Test plan

- [ ] `pytest
test/unit_test/api/db/services/test_dialog_service_final_answer.py -k
"async_ask_empty or async_ask_stale"`
- [ ] Manual: `POST /api/v1/searchbots/ask` with invalid `kb_ids`
returns SSE error, not HTTP 500

---------

Co-authored-by: Wang Qi <wangq8@outlook.com>
This commit is contained in:
bohdansolovie
2026-06-11 15:52:59 +08:00
committed by GitHub
parent de18313f97
commit 381091df71
2 changed files with 55 additions and 0 deletions

View File

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