Azure Cosmos DB Chat Memory Auto-Configuration
Spring Boot auto-configuration for the Azure Cosmos DB Chat Memory Repository. This module provides zero-config setup for persistent conversation storage when using Spring Boot.
Dependency
<dependency>
<groupId>com.azure.spring.ai</groupId>
<artifactId>spring-ai-autoconfigure-model-chat-memory-repository-cosmos-db</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
This transitively brings in the spring-ai-model-chat-memory-repository-cosmos-db module.
Configuration Properties
Add these to your application.properties or application.yml:
spring.ai.chat.memory.repository.cosmosdb.endpoint=https://your-account.documents.azure.com:443/
spring.ai.chat.memory.repository.cosmosdb.database-name=springai
spring.ai.chat.memory.repository.cosmosdb.container-name=chat_memory
spring.ai.chat.memory.repository.cosmosdb.connection-mode=gateway
Authentication
Key-based (set the key property):
spring.ai.chat.memory.repository.cosmosdb.key=your-cosmos-db-key
Azure Identity (recommended for production — omit the key property):
When no key is provided, the auto-configuration uses DefaultAzureCredential for authentication via managed identity, service principal, or other Azure credential sources.
Usage
Once configured, simply inject ChatMemoryRepository:
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
@Autowired
private ChatMemoryRepository chatMemoryRepository;
ChatMemoryAutoConfiguration so the Cosmos DB repository takes precedence.Auto-Configured Beans
The following beans are auto-configured (all @ConditionalOnMissingBean, so you can override any of them):
| Bean | Description |
|---|---|
CosmosAsyncClient | Cosmos DB async client (created when endpoint property is set) |
CosmosDBChatMemoryRepositoryConfig | Container configuration (database, container, partition key) |
CosmosDBChatMemoryRepository | The ChatMemoryRepository implementation |
Full Property Reference
| Property | Description | Default |
|---|---|---|
spring.ai.chat.memory.repository.cosmosdb.endpoint | Azure Cosmos DB endpoint URI | (required) |
spring.ai.chat.memory.repository.cosmosdb.key | Primary or secondary key | (uses DefaultAzureCredential if absent) |
spring.ai.chat.memory.repository.cosmosdb.connection-mode | Connection mode (gateway or direct) | gateway |
spring.ai.chat.memory.repository.cosmosdb.database-name | Database name | springai |
spring.ai.chat.memory.repository.cosmosdb.container-name | Container name | chat_memory |
spring.ai.chat.memory.repository.cosmosdb.partition-key-path | Partition key path | /conversationId |
Manual Configuration
If you prefer to configure the chat memory repository manually (without auto-configuration), see the Chat Memory page.