From 89922cc9d585e10b026693681b44afdc4b874588 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 10 Aug 2026 22:53:37 -0600 Subject: [PATCH] refac --- backend/open_webui/routers/knowledge.py | 2 ++ backend/open_webui/routers/retrieval.py | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/backend/open_webui/routers/knowledge.py b/backend/open_webui/routers/knowledge.py index 4cdcee678d..625095e872 100644 --- a/backend/open_webui/routers/knowledge.py +++ b/backend/open_webui/routers/knowledge.py @@ -381,6 +381,8 @@ async def reindex_knowledge_files( ) try: + # Force the KB add path to use stored SQL content instead of stale file-{id} chunks. + # process_file recreates file-{id} only when that stored content exists. file_collection = f'file-{file.id}' if await ASYNC_VECTOR_DB_CLIENT.has_collection(collection_name=file_collection): await ASYNC_VECTOR_DB_CLIENT.delete_collection(collection_name=file_collection) diff --git a/backend/open_webui/routers/retrieval.py b/backend/open_webui/routers/retrieval.py index 21ab578700..f92f30d4f2 100644 --- a/backend/open_webui/routers/retrieval.py +++ b/backend/open_webui/routers/retrieval.py @@ -1907,14 +1907,18 @@ async def process_file( text_content = form_data.content elif form_data.collection_name: - # Check if the file has already been processed and save the content + # Add this file to a knowledge collection. # Usage: /knowledge/{id}/file/add, /knowledge/{id}/file/update + # Reuse file-{id} chunks when they exist; otherwise restore file-{id} + # from stored file content while adding the file to the knowledge collection. file_result = await ASYNC_VECTOR_DB_CLIENT.query( collection_name=file_collection_name, filter={'file_id': file.id} ) + stored_content = (file.data or {}).get('content') if has_vector_results(file_result): + # Normal path: reuse the already-processed per-file chunks. docs = [ Document( page_content=file_result.documents[0][idx], @@ -1922,10 +1926,11 @@ async def process_file( ) for idx, id in enumerate(file_result.ids[0]) ] - else: + elif stored_content is not None: + # Repair path: vector chunks are missing, but SQL still has the file text. docs = [ Document( - page_content=file.data.get('content', ''), + page_content=stored_content, metadata={ **file.meta, 'name': file.filename, @@ -1936,8 +1941,10 @@ async def process_file( ) ] collection_names.append(file_collection_name) + else: + raise ValueError(ERROR_MESSAGES.EMPTY_CONTENT) - text_content = file.data.get('content', '') + text_content = stored_content or '' else: # Process the file and save the content # Usage: /files/