This version is still in development and is not considered stable yet. For the latest stable version, please use Spring AI 2.1.0!

Migrating to Session Memory

Session memory replaces the ChatMemory API. A session keeps the conversation as an append-only log of events, and the prompt is built from a view of it, so a window or a compaction never loses what was said and never splits a turn.

Chat memory Session memory

MessageChatMemoryAdvisor.builder(chatMemory)

SessionAdvisors.builder(sessionService), from spring-ai-session-advisor

ChatMemory.CONVERSATION_ID

MemoryAdvisor.CONVERSATION_ID, the same key, or SessionAdvisors.session(conversationId)

MessageWindowChatMemory, maxMessages

A window on the memory advisor, EventFilter.lastN, or a compaction

chatMemory.get(id), add(id, messages), clear(id)

sessionService.getActiveMessages(id), appendMessage(id, message), delete(id); create the session first, findOrCreate

ChatMemoryRepository, InMemoryChatMemoryRepository

SessionRepository, InMemorySessionRepository

JdbcChatMemoryRepository, spring-ai-starter-model-chat-memory-repository-jdbc

JdbcSessionRepository, spring-ai-starter-model-session-jdbc: PostgreSQL, MySQL, MariaDB, Microsoft SQL Server and H2

The Cassandra, Neo4j, MongoDB and Redis repositories; the HSQLDB, Oracle and SQLite dialects

No session counterpart yet; they are planned before 3.0.0, when these modules are removed. Until then, a store implements SessionRepository and checks itself with AbstractSessionRepositoryContractTests from spring-ai-test, and a dialect implements JdbcSessionRepositoryDialect.

The context key stays the same, so a request that sets ChatMemory.CONVERSATION_ID reaches the session advisors unchanged:

// Before
ChatClient chatClient = ChatClient.builder(chatModel)
    .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())
    .build();

// After
ChatClient chatClient = ChatClient.builder(chatModel)
    .defaultAdvisors(SessionAdvisors.builder(sessionService).build())
    .build();

chatClient.prompt()
    .user("Hello")
    .advisors(SessionAdvisors.session(conversationId))
    .call()
    .content();

With spring-ai-starter-model-session-jdbc on the classpath, the SessionService is auto-configured on the application’s DataSource.

The two stores do not share their tables: conversations kept in SPRING_AI_CHAT_MEMORY are not read by the session memory.