From d1975b740b019ba1364eba7fdcf478ec99b19ca7 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Sun, 8 Mar 2026 02:19:44 +0100 Subject: [PATCH] fix: add deterministic ordering to chat_ids pagination query to prevent duplicates (#22383) --- backend/open_webui/models/chat_messages.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/models/chat_messages.py b/backend/open_webui/models/chat_messages.py index e6e932be12..4cd868e4d4 100644 --- a/backend/open_webui/models/chat_messages.py +++ b/backend/open_webui/models/chat_messages.py @@ -292,9 +292,13 @@ class ChatMessageTable: query = query.filter(ChatMessage.created_at <= end_date) # Group by chat_id and order by most recent message in each chat + # Secondary sort on chat_id ensures deterministic pagination + # (prevents duplicates across pages when timestamps tie) chat_ids = ( query.group_by(ChatMessage.chat_id) - .order_by(func.max(ChatMessage.created_at).desc()) + .order_by( + func.max(ChatMessage.created_at).desc(), ChatMessage.chat_id + ) .offset(skip) .limit(limit) .all()