mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-07 03:18:23 -05:00
[PR #21855] [CLOSED] enh.: Move citation-only RAG prompt to system prompt to prevent tool call hallucination #65163
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/21855
Author: @Classic298
Created: 2/25/2026
Status: ❌ Closed
Base:
dev← Head:claude/move-rag-system-prompt-etLrl📝 Commits (4)
33d27adfix: move RAG template to system prompt to prevent model hallucination in tool callsd21cef3fix: always append RAG template to system prompt instead of user messageca84dbafix: default RAG template to system prompt, preserve conditionald6497d3fix: 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.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.