From 5cfbbc3d094d7616f90b931cd28f1fc744304a9d Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:39:48 +0100 Subject: [PATCH] fix: saving a chat without a reply's usage, output or error wipes them (#35009) A chat save from an API client or an older tab that leaves out a reply's usage, output or error wiped them from the stored reply, so the admin analytics lost the reply's tokens and its failed status. A save now leaves the reply's fields it does not send as they were, usage, output and error included, the way the analytics kept them before chats were stored message by message, and the reopened reply keeps its tool output as well. Sending a field as empty clears it. Fixes #35007 --- backend/open_webui/models/chat_messages.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/backend/open_webui/models/chat_messages.py b/backend/open_webui/models/chat_messages.py index 86dc37661a..80ddfdcec8 100644 --- a/backend/open_webui/models/chat_messages.py +++ b/backend/open_webui/models/chat_messages.py @@ -244,13 +244,18 @@ class ChatMessageModel(BaseModel): class ChatMessageTable: @staticmethod def _apply_message_data(message: ChatMessage, data: dict, now: int) -> None: - """Replace the stored message with the payload, keeping its author.""" + """Overwrite only the columns the payload carries, keeping the message's author.""" replacement = ChatMessageTable._build_message(message.id, message.chat_id, message.user_id, data, now) kept = {'id', 'chat_id', 'user_id'} if 'timestamp' not in data: kept.add('created_at') for column in ChatMessage.__table__.columns: - if column.key not in kept: + json_key = ChatMessageTable.DB_TO_JSON_KEY_MAP.get(column.key, column.key) + # Usage may come under the legacy `info` key. + is_sent = ( + column.key in data or json_key in data or (column.key == 'usage' and replacement.usage is not None) + ) + if column.key not in kept and (is_sent or column.key in ('data', 'updated_at')): setattr(message, column.key, getattr(replacement, column.key)) @staticmethod @@ -312,7 +317,7 @@ class ChatMessageTable: data: dict, db: Optional[AsyncSession] = None, ) -> None: - """Insert a chat message or replace the stored one.""" + """Insert a chat message or update the stored one.""" async with nullcontext(db) if db is not None else get_async_db_context() as session: now = int(time.time()) # Use composite ID: {chat_id}-{message_id}