From 1c813902ec02c8bf2f86dd1d045ecc1ddc57981f Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Sun, 27 Sep 2026 21:21:21 +0200 Subject: [PATCH] fix: keep relevance scores on knowledge tool citations (#31307) With native function calling, citations produced by query_knowledge_files and query_chat_files never showed the relevance percentage badge, while the same knowledge base queried through classic RAG did. The tools already return a distance per chunk, but the step that groups tool results into citation sources dropped it. Each grouped source now carries a distances list aligned with its documents, the same shape the classic RAG path emits, so the existing citation UI shows the badge without frontend changes. Chunks without a score (notes) leave the list empty, which the UI already treats as no score. Fixes #29776 --- backend/open_webui/utils/middleware.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index be4fa816ec..47f7617554 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -510,6 +510,7 @@ def get_citation_source_from_tool_result( }, 'document': [], 'metadata': [], + 'distances': [], } sources_by_file[key]['document'].append(content) @@ -521,6 +522,8 @@ def get_citation_source_from_tool_result( **({'note_id': note_id} if note_id else {}), } ) + if 'distance' in chunk: + sources_by_file[key]['distances'].append(chunk['distance']) # Return all grouped sources as a list if sources_by_file: