[PR #21583] [CLOSED] fix: skip memory context injection when system prompt is empty #65004

Closed
opened 2026-05-06 10:45:12 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/21583
Author: @lesj0610
Created: 2/19/2026
Status: Closed

Base: mainHead: fix/memory-context-system-prompt-gate


📝 Commits (1)

  • 85a0b2e Fix memory context injection when system prompt is empty

📊 Changes

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

View changed files

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

📄 Description

Summary

  • Skip memory-context system-message injection when no system prompt is configured.
  • Prevent empty `User Context:

` system messages from being appended.

Why

When the system prompt is intentionally blank, middleware still appended a system message via memory handling. This caused unintended prompt injection and unnecessary context artifacts.

Changes

  • In chat_memory_handler (backend/open_webui/utils/middleware.py):
    • Return early if current system prompt is blank.
    • Append User Context only when memory query produced non-empty context.

Testing

  • I personally tested the change locally.
  • Verified no User Context system message is appended when system prompt is empty.
  • Verified User Context is appended when system prompt is set and memory returns results.
  • python3 -m compileall backend/open_webui/utils/middleware.py

contributor license agreement


🔄 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/21583 **Author:** [@lesj0610](https://github.com/lesj0610) **Created:** 2/19/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/memory-context-system-prompt-gate` --- ### 📝 Commits (1) - [`85a0b2e`](https://github.com/open-webui/open-webui/commit/85a0b2eaa050b559cd1b70f34344cc4efaaf5d1c) Fix memory context injection when system prompt is empty ### 📊 Changes **1 file changed** (+13 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/utils/middleware.py` (+13 -3) </details> ### 📄 Description ## Summary - Skip memory-context system-message injection when no system prompt is configured. - Prevent empty `User Context: ` system messages from being appended. ## Why When the system prompt is intentionally blank, middleware still appended a system message via memory handling. This caused unintended prompt injection and unnecessary context artifacts. ## Changes - In `chat_memory_handler` (`backend/open_webui/utils/middleware.py`): - Return early if current system prompt is blank. - Append `User Context` only when memory query produced non-empty context. ## Testing - ✅ I personally tested the change locally. - ✅ Verified no `User Context` system message is appended when system prompt is empty. - ✅ Verified `User Context` is appended when system prompt is set and memory returns results. - ✅ `python3 -m compileall backend/open_webui/utils/middleware.py` contributor license agreement --- <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 10:45:12 -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#65004