diff --git a/backend/open_webui/routers/files.py b/backend/open_webui/routers/files.py index 15eb817703..5268ea657d 100644 --- a/backend/open_webui/routers/files.py +++ b/backend/open_webui/routers/files.py @@ -590,6 +590,29 @@ def update_file_data_content_by_id( log.exception(e) log.error(f"Error processing file: {file.id}") + # Propagate content change to all knowledge collections referencing + # this file. Without this the old embeddings remain in the knowledge + # collection and RAG returns both stale and current data (#20558). + knowledges = Knowledges.get_knowledges_by_file_id(id, db=db) + for knowledge in knowledges: + try: + # Remove old embeddings for this file from the KB collection + VECTOR_DB_CLIENT.delete( + collection_name=knowledge.id, filter={"file_id": id} + ) + # Re-add from the now-updated file-{file_id} collection + process_file( + request, + ProcessFileForm(file_id=id, collection_name=knowledge.id), + user=user, + db=db, + ) + except Exception as e: + log.warning( + f"Failed to update knowledge {knowledge.id} after " + f"content change for file {id}: {e}" + ) + return {"content": file.data.get("content", "")} else: raise HTTPException(