Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix message ordering in MongoChatMemoryRepository#5339

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

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

Conversation

@chanani

@chanani chanani commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

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 <[email protected]>

@tinomthomas tinomthomas left a comment

Copy link
Copy Markdown

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 <[email protected]>
@chanani

chanani commented Jan 27, 2026

Copy link
Copy Markdown
Contributor Author

@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

deejay1 commented Jan 28, 2026

Copy link
Copy Markdown
Contributor

@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