Service API and web app message history pagination skips messages that share the same created_at
Self Checks
- I have read the Contributing Guide and Language Policy.
- This is only for bug report, if you would like to ask a question, please head to Discussions.
- I have searched for existing issues search for existing issues, including closed ones.
- I confirm that I am using English to submit this report, otherwise it will be closed.
- 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
- Please do not modify this template :) and fill in all the required fields.
Dify version
main at 431c185706 (2026-10-01)
Cloud or Self Hosted
Self Hosted (Source)
Steps to reproduce
Steps to reproduce
- Create a Chatflow app with only a Start node and an Answer node. Publish it and create an API key.
- Send several messages to one conversation quickly through the Service API, e.g.
hi, thenmsg-1…msg-6in a loop (POST /v1/chat-messageswith the sameconversation_id). On PostgreSQLmessages.created_atdefaults toCURRENT_TIMESTAMP(0), so messages sent within the same second get identical timestamps. - Page through the history with a small page size:
GET /v1/messages?conversation_id={conversation_id}&user=u1&limit=2
GET /v1/messages?conversation_id={conversation_id}&user=u1&limit=2&first_id={oldest id of the previous page}Messages that share created_at with the cursor never appear on the next page, and has_more turns false early. The same happens on the web app endpoint GET /api/messages, which uses the same service method, and on MessageService.pagination_by_last_id.
A source-only reproduction: the two new tests in the linked PR (test_first_id_keeps_messages_that_share_created_at, test_last_id_keeps_messages_that_share_created_at) persist five messages with one created_at and page through them with limit=2. On current main both tests fail: the first page returns two messages and the next page is empty.
Root cause
MessageService.pagination_by_first_id and pagination_by_last_id (api/services/message_service.py) use
Message.created_at < cursor.created_at, Message.id != cursor.idas the cursor and order only by created_at. created_at < drops every row that ties with the cursor, and id != cursor.id only excludes the cursor itself. Ordering by created_at alone also makes the order of tied rows unstable between requests.
This is the same class of bug as #43084 (conversation variables, fixed in #43087), #43134 (console chat messages, #43146) and #43266 (workflow runs). It was previously reported as #35996; the fix in #37896 was closed during PR queue cleanup because of merge conflicts, with a note that a fresh submission is welcome.
✔️ Expected Behavior
Paging with first_id / last_id returns every message exactly once in a stable (created_at, id) order, and has_more is only false on the final page.
❌ Actual Behavior
Messages created in the same second as the cursor are skipped. Clients consuming the history (Service API integrations, the web app) silently lose messages.
Source: langgenius/dify