[PR #21855] [CLOSED] enh.: Move citation-only RAG prompt to system prompt to prevent tool call hallucination #49355

Closed
opened 2026-04-30 01:39:42 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

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

Base: devHead: claude/move-rag-system-prompt-etLrl


📝 Commits (4)

  • 33d27ad fix: move RAG template to system prompt to prevent model hallucination in tool calls
  • d21cef3 fix: always append RAG template to system prompt instead of user message
  • ca84dba fix: default RAG template to system prompt, preserve conditional
  • d6497d3 fix: always put citation RAG prompt in system prompt for tool calls

📊 Changes

1 file changed (+22 additions, -16 deletions)

View changed files

📝 backend/open_webui/utils/middleware.py (+22 -16)

📄 Description

apply_source_context_to_messages: When include_content=False (citation-only context from native tool calls like search_web, fetch_url), always append the RAG template to the system prompt instead of the user message. This prevents models from hallucinating the RAG template text into tool call arguments — a problem observed across multiple models (GLM-5, Kimi 2.5, Qwen 3.5, etc.) during native tool calling. Full document context (uploaded files, include_content=True) still respects the RAG_SYSTEM_CONTEXT env var as before.

Tool call loop: Save and restore the original system message content before each re-apply of apply_source_context_to_messages, mirroring the existing user message save/restore. Without this, the RAG template would accumulate in the system message on each tool call iteration — the same class of double-injection bug reported in #21780.

Fixes #21780

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/21855 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 2/25/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `claude/move-rag-system-prompt-etLrl` --- ### 📝 Commits (4) - [`33d27ad`](https://github.com/open-webui/open-webui/commit/33d27ad7e181d1274267469d8976cd5b1c012b93) fix: move RAG template to system prompt to prevent model hallucination in tool calls - [`d21cef3`](https://github.com/open-webui/open-webui/commit/d21cef3977509663e74b22274fe9d18ed910c0f8) fix: always append RAG template to system prompt instead of user message - [`ca84dba`](https://github.com/open-webui/open-webui/commit/ca84dbae119cab73e0d1c30c4a7d77c75e1346f5) fix: default RAG template to system prompt, preserve conditional - [`d6497d3`](https://github.com/open-webui/open-webui/commit/d6497d38fd4ba78b4cd842433f30b11fb8906124) fix: always put citation RAG prompt in system prompt for tool calls ### 📊 Changes **1 file changed** (+22 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/utils/middleware.py` (+22 -16) </details> ### 📄 Description apply_source_context_to_messages: When include_content=False (citation-only context from native tool calls like search_web, fetch_url), always append the RAG template to the system prompt instead of the user message. This prevents models from hallucinating the RAG template text into tool call arguments — a problem observed across multiple models (GLM-5, Kimi 2.5, Qwen 3.5, etc.) during native tool calling. Full document context (uploaded files, include_content=True) still respects the RAG_SYSTEM_CONTEXT env var as before. Tool call loop: Save and restore the original system message content before each re-apply of apply_source_context_to_messages, mirroring the existing user message save/restore. Without this, the RAG template would accumulate in the system message on each tool call iteration — the same class of double-injection bug reported in #21780. Fixes #21780 ### 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-30 01:39:43 -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#49355