Implements automatic Chat Context injection that adds recent or pinned chat history to conversation context.
I believe this feature will enhance the user experience by personalizing model context. Find an example in the screenshot below.
Added
Backend config: ENABLE_CHAT_CONTEXT, CHAT_CONTEXT_MODE, CHAT_CONTEXT_TOP_K in config.py
Middleware handler chat_context_handler() in utils/middleware.py supporting:
Recent mode: retrieves last N chats (skip=1 to exclude current)
Pinned mode: retrieves user-pinned chats (early return if none exist)
Testing: Manual testing of enable/disable, mode switching, top K validation, pinned empty/full, recent mode, config persistence, feature flag, JSON structure, multimodal handling.
Screenshots or Videos
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.
## 📋 Pull Request Information
**Original PR:** https://github.com/open-webui/open-webui/pull/18573
**Author:** [@Davixk](https://github.com/Davixk)
**Created:** 10/24/2025
**Status:** ❌ Closed
**Base:** `dev` ← **Head:** `feat/add-chats-context`
---
### 📝 Commits (3)
- [`e0f0351`](https://github.com/open-webui/open-webui/commit/e0f0351bcc9c4ec618a871828da74f66b849d28a) feat(chat-context): add configurable chat context injection
- [`5f212b6`](https://github.com/open-webui/open-webui/commit/5f212b6fd6cd2307edd49b371b671bafa0091af3) refactor(admin-settings): improve chat context UX with tooltips
- [`9f2f9c4`](https://github.com/open-webui/open-webui/commit/9f2f9c49b3f79e6992fb023f0eaff03bc9db7b80) refactor(middleware): centralize message content cleaning
### 📊 Changes
**6 files changed** (+307 additions, -6 deletions)
<details>
<summary>View changed files</summary>
📝 `backend/open_webui/config.py` (+18 -0)
📝 `backend/open_webui/main.py` (+11 -0)
📝 `backend/open_webui/routers/auths.py` (+13 -0)
📝 `backend/open_webui/utils/middleware.py` (+178 -6)
📝 `src/lib/components/admin/Settings/General.svelte` (+83 -0)
📝 `src/lib/components/chat/Chat.svelte` (+4 -0)
</details>
### 📄 Description
# Changelog Entry
### Description
Implements automatic Chat Context injection that adds recent or pinned chat history to conversation context.
I believe this feature will enhance the user experience by personalizing model context. Find an example in the screenshot below.
### Added
- Backend config: `ENABLE_CHAT_CONTEXT`, `CHAT_CONTEXT_MODE`, `CHAT_CONTEXT_TOP_K` in `config.py`
- Middleware handler `chat_context_handler()` in `utils/middleware.py` supporting:
- Recent mode: retrieves last N chats (skip=1 to exclude current)
- Pinned mode: retrieves user-pinned chats (early return if none exist)
- Structured extraction: first_user_message + last_messages{user, assistant}
- Conditional prompts: encouraging for pinned, discouraging for recent
- JSON-in-XML injection format
- Admin UI in `General.svelte`: enable toggle, mode dropdown, top K input
- Feature flag in `Chat.svelte`: checks `$config.chat_context.enable`
- Config exposure via `/api/config` endpoint
### Changed
- `AdminConfig` model now includes chat context fields
- `get_admin_config()` and `update_admin_config()` handle chat context settings
- Middleware pipeline calls `chat_context_handler()` when feature enabled
### Additional Information
**Design decisions:**
- Skip=1 in recent mode prevents self-reference
- No skip in pinned mode
- Early return for empty pinned chats
- Multimodal content support extracts text from arrays
**Chat format injected:**
\`\`\`json
[
{
"title": "Chat Title",
"created_at": "2024-01-15T10:30:00-0500",
"updated_at": "2024-01-15T11:45:00-0500",
"first_user_message": "...",
"last_messages": {
"user": "...",
"assistant": "..."
}
}
]
\`\`\`
**Testing:** Manual testing of enable/disable, mode switching, top K validation, pinned empty/full, recent mode, config persistence, feature flag, JSON structure, multimodal handling.
### Screenshots or Videos
<img width="1014" height="856" alt="image" src="https://github.com/user-attachments/assets/2abf0756-59c7-4728-9914-9581f5d10082" />
<img width="1012" height="856" alt="image" src="https://github.com/user-attachments/assets/eaf55000-51fd-4cf9-8e09-60ebb12ec6fe" />
### Contributor License Agreement
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.
---
<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/18573
Author: @Davixk
Created: 10/24/2025
Status: ❌ Closed
Base:
dev← Head:feat/add-chats-context📝 Commits (3)
e0f0351feat(chat-context): add configurable chat context injection5f212b6refactor(admin-settings): improve chat context UX with tooltips9f2f9c4refactor(middleware): centralize message content cleaning📊 Changes
6 files changed (+307 additions, -6 deletions)
View changed files
📝
backend/open_webui/config.py(+18 -0)📝
backend/open_webui/main.py(+11 -0)📝
backend/open_webui/routers/auths.py(+13 -0)📝
backend/open_webui/utils/middleware.py(+178 -6)📝
src/lib/components/admin/Settings/General.svelte(+83 -0)📝
src/lib/components/chat/Chat.svelte(+4 -0)📄 Description
Changelog Entry
Description
Implements automatic Chat Context injection that adds recent or pinned chat history to conversation context.
I believe this feature will enhance the user experience by personalizing model context. Find an example in the screenshot below.
Added
ENABLE_CHAT_CONTEXT,CHAT_CONTEXT_MODE,CHAT_CONTEXT_TOP_Kinconfig.pychat_context_handler()inutils/middleware.pysupporting:General.svelte: enable toggle, mode dropdown, top K inputChat.svelte: checks$config.chat_context.enable/api/configendpointChanged
AdminConfigmodel now includes chat context fieldsget_admin_config()andupdate_admin_config()handle chat context settingschat_context_handler()when feature enabledAdditional Information
Design decisions:
Chat format injected:
```json
[
{
"title": "Chat Title",
"created_at": "2024-01-15T10:30:00-0500",
"updated_at": "2024-01-15T11:45:00-0500",
"first_user_message": "...",
"last_messages": {
"user": "...",
"assistant": "..."
}
}
]
```
Testing: Manual testing of enable/disable, mode switching, top K validation, pinned empty/full, recent mode, config persistence, feature flag, JSON structure, multimodal handling.
Screenshots or Videos
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.