[PR #22272] [CLOSED] fix: prevent duplicate RAG prompt when file upload and tool sources combine #65434

Closed
opened 2026-05-06 11:15:25 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/22272
Author: @Classic298
Created: 3/5/2026
Status: Closed

Base: devHead: fix/rag-prompt-duplication


📝 Commits (1)

  • d858fd9 fix: prevent duplicate RAG prompt when file upload and tool sources combine

📊 Changes

1 file changed (+98 additions, -29 deletions)

View changed files

📝 backend/open_webui/utils/middleware.py (+98 -29)

📄 Description

When a user uploads a file AND the model uses a native tool that returns citation sources (e.g. fetch_url, query_knowledge_bases), the RAG system prompt was injected twice — once for file sources in process_chat_payload and again for tool sources in the streaming handler.

Root cause: the deduplication snapshots (user_message and original_system_content) were captured after the first RAG injection, so restoring to them did not undo the already-injected template.

Fix:

  • Extract build_source_context_string helper from apply_source_context_to_messages for reusable context building.
  • Save the pre-RAG-injection message state in metadata before the first apply_source_context_to_messages call.
  • In the streaming handler, use these pre-RAG originals instead of post-injection snapshots.
  • Merge file sources (with content) and tool sources (without content) into a single RAG template application via a shared citation index.

Contributor License Agreement

By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.

Note

Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/22272 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 3/5/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `fix/rag-prompt-duplication` --- ### 📝 Commits (1) - [`d858fd9`](https://github.com/open-webui/open-webui/commit/d858fd9d8c47ad279ff8dde0be86d8a5bbfb866e) fix: prevent duplicate RAG prompt when file upload and tool sources combine ### 📊 Changes **1 file changed** (+98 additions, -29 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/utils/middleware.py` (+98 -29) </details> ### 📄 Description When a user uploads a file AND the model uses a native tool that returns citation sources (e.g. fetch_url, query_knowledge_bases), the RAG system prompt was injected twice — once for file sources in process_chat_payload and again for tool sources in the streaming handler. Root cause: the deduplication snapshots (user_message and original_system_content) were captured after the first RAG injection, so restoring to them did not undo the already-injected template. Fix: - Extract build_source_context_string helper from apply_source_context_to_messages for reusable context building. - Save the pre-RAG-injection message state in metadata before the first apply_source_context_to_messages call. - In the streaming handler, use these pre-RAG originals instead of post-injection snapshots. - Merge file sources (with content) and tool sources (without content) into a single RAG template application via a shared citation index. ### Contributor License Agreement <!-- 🚨 DO NOT DELETE THE TEXT BELOW 🚨 Keep the "Contributor License Agreement" confirmation text intact. Deleting it will trigger the CLA-Bot to INVALIDATE your PR. --> By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. > [!NOTE] > Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-05-06 11:15:25 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#65434