[PR #21668] [CLOSED] fix: prevent recursive RAG template nesting during multi-turn tool calling #26194

Closed
opened 2026-04-20 06:23:03 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/21668
Author: @Classic298
Created: 2/20/2026
Status: Closed

Base: devHead: rag-fix


📝 Commits (3)

  • e8e9981 Update middleware.py
  • 43aedfc Update middleware.py
  • 61d2795 fix: prevent recursive RAG template nesting during multi-turn tool calling

📊 Changes

1 file changed (+33 additions, -13 deletions)

View changed files

📝 backend/open_webui/utils/middleware.py (+33 -13)

📄 Description

When native tool calling produced multiple sequential tool calls with
citation sources (e.g. repeated search_web or fetch_url), the RAG
template was recursively injected into the user message on each
iteration. This caused the user message to grow exponentially, wasting
context window and potentially confusing models into parroting the RAG
instructions into subsequent tool call arguments.

Fix: save the original user message before the tool-calling loop and
restore it before each re-application. Sources now accumulate across
iterations so the RAG template is applied once with a growing source
list, rather than being recursively nested N times.

Tested locally

Fixes: https://github.com/open-webui/open-webui/issues/21663

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/21668 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 2/20/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `rag-fix` --- ### 📝 Commits (3) - [`e8e9981`](https://github.com/open-webui/open-webui/commit/e8e99811abbdb19cf9a8f3c983067115c15c963c) Update middleware.py - [`43aedfc`](https://github.com/open-webui/open-webui/commit/43aedfc2b912c3e8cf23a8230fb109f292c3b226) Update middleware.py - [`61d2795`](https://github.com/open-webui/open-webui/commit/61d2795086cb776f26655bc85b6115f9e743f31d) fix: prevent recursive RAG template nesting during multi-turn tool calling ### 📊 Changes **1 file changed** (+33 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/utils/middleware.py` (+33 -13) </details> ### 📄 Description When native tool calling produced multiple sequential tool calls with citation sources (e.g. repeated search_web or fetch_url), the RAG template was recursively injected into the user message on each iteration. This caused the user message to grow exponentially, wasting context window and potentially confusing models into parroting the RAG instructions into subsequent tool call arguments. Fix: save the original user message before the tool-calling loop and restore it before each re-application. Sources now accumulate across iterations so the RAG template is applied once with a growing source list, rather than being recursively nested N times. Tested locally Fixes: https://github.com/open-webui/open-webui/issues/21663 ### 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-04-20 06:23:03 -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#26194