Skip to content

Commit dbde175

Browse files
committed
Docs: MongoChatmemoryRepository message ordering
- Update upgrade notes - Fix MongoChatMemoryRepositoryIT to include DataMongoAutoConfiguration - Fix the chat memory docs to explicitly specify the message ordering Signed-off-by: Ilayaperumal Gopinathan <ilayaperumal.gopinathan@broadcom.com>
1 parent c65cb21 commit dbde175

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

memory/repository/spring-ai-model-chat-memory-repository-mongodb/src/test/java/org/springframework/ai/chat/memory/repository/mongo/MongoChatMemoryRepositoryIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.beans.factory.annotation.Autowired;
3535
import org.springframework.boot.SpringBootConfiguration;
3636
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
37+
import org.springframework.boot.data.mongodb.autoconfigure.DataMongoAutoConfiguration;
3738
import org.springframework.boot.mongodb.autoconfigure.MongoAutoConfiguration;
3839
import org.springframework.boot.test.context.SpringBootTest;
3940
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
@@ -155,7 +156,7 @@ void deleteMessagesByConversationId() {
155156
}
156157

157158
@SpringBootConfiguration
158-
@ImportAutoConfiguration({ MongoAutoConfiguration.class })
159+
@ImportAutoConfiguration({ MongoAutoConfiguration.class, DataMongoAutoConfiguration.class })
159160
static class TestConfiguration {
160161

161162
@Bean

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat-memory.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ ChatMemoryRepository repository = new InMemoryChatMemoryRepository();
6868

6969
`JdbcChatMemoryRepository` is a built-in implementation that uses JDBC to store messages in a relational database. It supports multiple databases out-of-the-box and is suitable for applications that require persistent storage of chat memory.
7070

71+
Messages are retrieved in ascending timestamp order (oldest-to-newest), which is the expected format for LLM conversation history.
72+
7173
First, add the following dependency to your project:
7274

7375
[tabs]
@@ -181,6 +183,8 @@ ChatMemoryRepository chatMemoryRepository = JdbcChatMemoryRepository.builder()
181183

182184
`CassandraChatMemoryRepository` has a time-series schema, keeping record of all past chat windows, valuable for governance and auditing. Setting time-to-live to some value, for example three years, is recommended.
183185

186+
Messages are retrieved in ascending timestamp order (oldest-to-newest), which is the expected format for LLM conversation history.
187+
184188
To use `CassandraChatMemoryRepository` first, add the dependency to your project:
185189

186190
[tabs]
@@ -256,6 +260,8 @@ You can disable the schema initialization by setting the property `spring.ai.cha
256260

257261
`Neo4jChatMemoryRepository` is a built-in implementation that uses Neo4j to store chat messages as nodes and relationships in a property graph database. It is suitable for applications that want to leverage Neo4j's graph capabilities for chat memory persistence.
258262

263+
Messages are retrieved in ascending message index order (oldest-to-newest), which is the expected format for LLM conversation history.
264+
259265
First, add the following dependency to your project:
260266

261267
[tabs]
@@ -328,6 +334,8 @@ The Neo4j repository will automatically ensure that indexes are created for conv
328334

329335
`CosmosDBChatMemoryRepository` is a built-in implementation that uses Azure Cosmos DB NoSQL API to store messages. It is suitable for applications that require a globally distributed, highly scalable document database for chat memory persistence. The repository uses the conversation ID as the partition key to ensure efficient data distribution and fast retrieval.
330336

337+
Messages are retrieved in ascending timestamp order (oldest-to-newest), which is the expected format for LLM conversation history.
338+
331339
First, add the following dependency to your project:
332340

333341
[tabs]
@@ -412,6 +420,8 @@ You can customize the database and container names using the configuration prope
412420

413421
`MongoChatMemoryRepository` is a built-in implementation that uses MongoDB to store messages. It is suitable for applications that require a flexible, document-oriented database for chat memory persistence.
414422

423+
Messages are retrieved in ascending timestamp order (oldest-to-newest), which is the expected format for LLM conversation history. This ordering is consistent across all chat memory repository implementations.
424+
415425
First, add the following dependency to your project:
416426

417427
[tabs]
@@ -484,6 +494,8 @@ It is suitable for applications that require high-performance, low-latency chat
484494
The repository stores messages as JSON documents and creates a search index for efficient querying.
485495
It also provides extended query capabilities through the `AdvancedRedisChatMemoryRepository` interface for searching messages by content, type, time range, and metadata.
486496

497+
Messages are retrieved in ascending timestamp order (oldest-to-newest), which is the expected format for LLM conversation history.
498+
487499
First, add the following dependency to your project:
488500

489501
[tabs]

spring-ai-docs/src/main/antora/modules/ROOT/pages/upgrade-notes.adoc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@
44
[[upgrading-to-2-0-0-M2]]
55
== Upgrading to 2.0.0-M2
66

7+
=== Breaking Changes
8+
9+
==== MongoDB Chat Memory Message Ordering Fixed
10+
11+
The `MongoChatMemoryRepository` has been fixed to return messages in the order they were sent (oldest-to-newest), matching all other chat memory repository implementations. Previously, it incorrectly returned messages in reverse order (newest-to-oldest), which broke conversation flow for LLMs.
12+
13+
===== Impact
14+
15+
If your application was using `MongoChatMemoryRepository` and working around the incorrect ordering (e.g., by reversing messages after retrieval), you will need to remove that workaround.
16+
17+
===== Migration
18+
19+
Remove any code that reverses the message order after retrieving from MongoDB chat memory:
20+
21+
[source,java]
22+
----
23+
// BEFORE (with workaround for bug):
24+
List<Message> messages = chatMemoryRepository.findByConversationId(conversationId);
25+
Collections.reverse(messages); // Remove this workaround
26+
27+
// AFTER (correct ordering):
28+
List<Message> messages = chatMemoryRepository.findByConversationId(conversationId);
29+
// Messages are now correctly ordered chronologically
30+
----
31+
32+
All chat memory repositories now consistently return messages in the order they were sent (oldest-to-newest), which is the expected format for LLM conversation history.
33+
734
=== Development-time Services
835

936
* Docker Compose and Testcontainers support for MongoDB Atlas is now provided natively by the Spring Boot MongoDB module. The migration should be transparent and not require any code change. Regarding dependencies, you don't need to import `org.springframework.ai:spring-ai-spring-boot-testcontainers` anymore. A dependency on `org.springframework.boot:spring-boot-testcontainers` is sufficient.

0 commit comments

Comments
 (0)