From 460bf0e42b47672cb8d0eb7835e2b10994cac4ce Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:45:53 +0100 Subject: [PATCH] fix: database upgrade breaks the reply chain of old chats whose first message has no id (#35012) In a chat from an older version whose saved message list is broken, the upgrade falls back to the older plain list of its messages. When that list's first entry has no id but is already saved, the message after it kept pointing at a first message that does not exist, so the conversation could not be followed back to its start. The next message now points at the saved first message. Fixes #35004 --- ...d4b13_move_chat_history_to_chat_message.py | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/backend/open_webui/migrations/versions/5e7a2c9d4b13_move_chat_history_to_chat_message.py b/backend/open_webui/migrations/versions/5e7a2c9d4b13_move_chat_history_to_chat_message.py index b1799f742b..7a08207aff 100644 --- a/backend/open_webui/migrations/versions/5e7a2c9d4b13_move_chat_history_to_chat_message.py +++ b/backend/open_webui/migrations/versions/5e7a2c9d4b13_move_chat_history_to_chat_message.py @@ -145,6 +145,24 @@ def _without_nul(value: Any) -> Any: return value +def _drop_saved_list_entries(chat_id: str, messages: dict, existing: dict) -> None: + """Old lists give an entry no id of its own although its row exists; its children move to that row.""" + unclaimed = [row for row in existing.values() if row.id.removeprefix(f'{chat_id}-') not in messages] + for message_id, message in list(messages.items()): + if 'id' in message or f'{chat_id}-{message_id}' in existing: + continue + saved = next( + (row for row in unclaimed if row.role == message.get('role') and row.content == message.get('content')), + None, + ) + if saved: + unclaimed.remove(saved) + del messages[message_id] + for child in messages.values(): + if child.get('parentId') == message_id: + child['parentId'] = saved.id.removeprefix(f'{chat_id}-') + + def _move_chat_history( conn: sa.Connection, chat_id: str, owner_id: str, chat: dict, current_message_id: str | None ) -> None: @@ -171,22 +189,13 @@ def _move_chat_history( deleted_ids = existing.keys() - {f'{chat_id}-{message_id}' for message_id in history['messages']} if deleted_ids: conn.execute(sa.delete(chat_message_table).where(columns.id.in_(deleted_ids))) + else: + _drop_saved_list_entries(chat_id, messages, existing) now = int(time.time()) updates, inserts = [], [] for message_id, message in messages.items(): row_id = f'{chat_id}-{message_id}' - # An old list entry without an id of its own is already saved when a row has its role and content. - if ( - row_id not in existing - and not isinstance(history.get('messages'), dict) - and 'id' not in message - and any( - stored.role == message.get('role') and stored.content == message.get('content') - for stored in existing.values() - ) - ): - continue created_at = _recorded_timestamp(message.get('timestamp')) author = message.get('user') if isinstance(message.get('user'), dict) else {} author_id = message.get('user_id') or author.get('id')