MongoChatMemoryRepository is changing the order of the messages to descending during fetch and consequently the LLM is behaving unexpectedly
Bug description
org.springframework.ai.chat.memory.repository.mongo.MongoChatMemoryRepository is changing the order of the messages to descending during fetch and consequently the LLM is behaving unexpectedly.
I believe this happening because of the descending sort in the below code:
@Override
public List<Message> findByConversationId(String conversationId) {
var messages = this.mongoTemplate.query(Conversation.class)
.matching(Query.query(Criteria.where("conversationId").is(conversationId))
.with(Sort.by("timestamp").descending()));
return messages.stream().map(MongoChatMemoryRepository::mapMessage).collect(Collectors.toList());
}
If we compare the sort with org.springframework.ai.chat.memory.repository.jdbc.PostgresChatMemoryRepositoryDialect, it uses an ascending sort rather than a descending one.
@Override
public String getSelectMessagesSql() {
return "SELECT content, type FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ? ORDER BY \"timestamp\"";
}
Environment Spring AI 1.1.2 Azure CosmosDB with Mongo API
Expected behavior The order of conversation history should maintain the natural flow of the conversation (USER, ASSISTANT, USER, ASSISTANT.....)
Hi @tinomthomas,
I'd like to work on this issue if it's still available.
Problem Analysis
The bug is in MongoChatMemoryRepository.findByConversationId() where messages are sorted in descending order (Sort.by("timestamp").descending()), while the PostgreSQL implementation uses ascending order (implicit in ORDER BY "timestamp").
This causes the conversation history to be reversed (ASSISTANT, USER, ASSISTANT, USER...), which confuses the LLM as it expects chronological order.
Proposed Solution
Change the sort order from descending to ascending:
@Override
public List<Message> findByConversationId(String conversationId) {
var messages = this.mongoTemplate.query(Conversation.class)
.matching(Query.query(Criteria.where("conversationId").is(conversationId))
.with(Sort.by("timestamp").ascending())); // Change to ascending
return messages.stream()
.map(MongoChatMemoryRepository::mapMessage)
.collect(Collectors.toList());
}
Testing Plan
- Add/update integration tests to verify message ordering
- Ensure compatibility with existing MongoDB-based chat memory functionality
- Verify that messages are returned in chronological order (USER → ASSISTANT → USER → ASSISTANT...)
I'll follow the DCO process and sign off my commits. Please let me know if I can proceed with this fix.
Thanks!
@chanani Sure.Please go ahead.
Also, I would love to know the opinion of @deejay1 (original author of this implementation). He might had some other rationale in mind during the implementation.
Submitted PR #5339 to fix this - changed descending() to ascending() for chronological order.
@tinomthomas Please verify when merged. @deejay1 Could you review?
Sorry for the late reply, unfortunately I'm on a longer sick leave right now. The original reasoning for the order was for user facing UI which are mostly interested in the latest replies but I see how it could affect LLMs themselves :/ so +1 from my side for the change