[PR #21592] [CLOSED] perf: Cache chat object in upsert_message to avoid redundant DB load #49202

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

📋 Pull Request Information

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

Base: devHead: perf-upsert-chat


📝 Commits (1)

  • 83da507 Cache chat object in upsert_message to avoid redundant DB load

📊 Changes

1 file changed (+6 additions, -4 deletions)

View changed files

📝 backend/open_webui/models/chats.py (+6 -4)

📄 Description

upsert_message_to_chat_by_id_and_message_id was calling get_chat_by_id
twice: once at the top to load the chat, and again at line 512 just to
get user_id for the dual-write to chat_message table.

This function fires many times during streaming responses, so each redundant load deserializes the full conversation JSON blob unnecessarily.

upsert_message: Look at that call count: 17 call sites across socket/main.py and middleware.py. This is called during streaming. It's also called when the user's message is first saved, when tool calls happen, when the final response lands. So this one fires many times during a single streaming response, and each was doing 2 full chat loads instead of 1.

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/21592 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 2/19/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `perf-upsert-chat` --- ### 📝 Commits (1) - [`83da507`](https://github.com/open-webui/open-webui/commit/83da50763232d435add16f35c006c16afa461ce3) Cache chat object in upsert_message to avoid redundant DB load ### 📊 Changes **1 file changed** (+6 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/models/chats.py` (+6 -4) </details> ### 📄 Description upsert_message_to_chat_by_id_and_message_id was calling get_chat_by_id twice: once at the top to load the chat, and again at line 512 just to get user_id for the dual-write to chat_message table. <ins>**This function fires many times during streaming responses, so each redundant load deserializes the full conversation JSON blob unnecessarily.**</ins> upsert_message: Look at that call count: 17 call sites across socket/main.py and middleware.py. This is called during streaming. It's also called when the user's message is first saved, when tool calls happen, when the final response lands. <ins>**So this one fires many times during a single streaming response, and each was doing 2 full chat loads instead of 1.**</ins> ### 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:31:42 -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#49202