Skip to content

Fix message ordering in MongoChatMemoryRepository#5339

Closed
chanani wants to merge 2 commits intospring-projects:mainfrom
chanani:issue-5330
Closed

Fix message ordering in MongoChatMemoryRepository#5339
chanani wants to merge 2 commits intospring-projects:mainfrom
chanani:issue-5330

Conversation

@chanani
Copy link
Copy Markdown
Contributor

@chanani chanani commented Jan 27, 2026

Description

Fixed message ordering bug where MongoChatMemoryRepository.findByConversationId() returned messages in reverse chronological order, causing incorrect conversation flow for LLMs.

Changes

  • Changed sort order from descending() to ascending() in findByConversationId()

Why

Before (Bug):

.with(Sort.by("timestamp").descending())  // Returns: [newest...oldest]

After (Fixed):

.with(Sort.by("timestamp").ascending())   // Returns: [oldest...newest]

This aligns with PostgresChatMemoryRepositoryDialect:

ORDER BY "timestamp"  -- ascending by default

Testing

  • Existing test findByConversationId() validates message order with assertThat(results).isEqualTo(messages)
  • Unable to run locally (Docker constraints), but CI will validate

Fixes #5330

Changed sort order from descending to ascending in findByConversationId()
to maintain chronological conversation flow.

Fixes spring-projects#5330

Signed-off-by: CHANHAN <130114269+chanani@users.noreply.github.com>
Copy link
Copy Markdown

@tinomthomas tinomthomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chanani Can you please add/update a test case also to cover this?

Added messagesAreReturnedInChronologicalOrder test to verify
that messages are returned in the same order they were saved.

Signed-off-by: CHANHAN <130114269+chanani@users.noreply.github.com>
@chanani
Copy link
Copy Markdown
Contributor Author

chanani commented Jan 27, 2026

@tinomthomas, Added test case messagesAreReturnedInChronologicalOrder() to explicitly verify chronological ordering.

The test validates that messages are returned in the same order they were saved, ensuring the fix maintains proper conversation flow.

Let me know if you need any changes !

@ilayaperumalg
Copy link
Copy Markdown
Member

@chanani Thanks for the fix!

@deejay1
Copy link
Copy Markdown
Contributor

deejay1 commented Jan 28, 2026

@ilayaperumalg LGTM, except the javaformat. Do we have a way to mark this change in release notes as it may break someones behaviour?

@ilayaperumalg
Copy link
Copy Markdown
Member

@deejay1 Thanks for the review. Good point on the notes related to the breaking change.

Rebased and merged via c65cb21 along with the update to docs to clarify the breaking change and the message ordering via dbde175.

Backported the changes to 1.1.x as well.

@chanani chanani deleted the issue-5330 branch January 28, 2026 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MongoChatMemoryRepository is changing the order of the messages to descending during fetch and consequently the LLM is behaving unexpectedly

4 participants