Skip to content

MongoDB Chat Memory Repository Application - APPLICATION FAILED TO START when **create-indices = true** #4884

@WilliamBertolasi

Description

@WilliamBertolasi

APPLICATION FAILED TO START when create-indices = true

Bug description

The property spring.ai.chat.memory.repository.mongo.create-indices = true causes the following error:

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.springframework.ai.model.chat.memory.repository.mongo.autoconfigure.MongoChatMemoryIndexCreatorAutoConfiguration.initIndicesAfterStartup(MongoChatMemoryIndexCreatorAutoConfiguration.java:61)

The following method did not exist:

    'java.lang.String org.springframework.data.mongodb.core.index.IndexOperations.createIndex(org.springframework.data.mongodb.core.index.IndexDefinition)'

The calling method's class, org.springframework.ai.model.chat.memory.repository.mongo.autoconfigure.MongoChatMemoryIndexCreatorAutoConfiguration, was loaded from the following location:

    jar:file:/C:/Users/wbertolasi/.m2/repository/org/springframework/ai/spring-ai-autoconfigure-model-chat-memory-repository-mongodb/1.1.0/spring-ai-autoconfigure-model-chat-memory-repository-mongodb-1.1.0.jar!/org/springframework/ai/model/chat/memory/repository/mongo/autoconfigure/MongoChatMemoryIndexCreatorAutoConfiguration.class

The called method's class, org.springframework.data.mongodb.core.index.IndexOperations, is available from the following locations:

    jar:file:/C:/Users/wbertolasi/.m2/repository/org/springframework/data/spring-data-mongodb/4.2.4/spring-data-mongodb-4.2.4.jar!/org/springframework/data/mongodb/core/index/IndexOperations.class

The called method's class hierarchy was loaded from the following locations:

    org.springframework.data.mongodb.core.index.IndexOperations: file:/C:/Users/wbertolasi/.m2/repository/org/springframework/data/spring-data-mongodb/4.2.4/spring-data-mongodb-4.2.4.jar

Action:

Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.ai.model.chat.memory.repository.mongo.autoconfigure.MongoChatMemoryIndexCreatorAutoConfiguration and org.springframework.data.mongodb.core.index.IndexOperations

Environment

  • Spring AI version: 1.1.0
  • Java version: 17
  • SpringBoot version: 3.2.4
  • Database: MongoDB v7.0
  • Schema/driver: Spring Data MongoDB + MongoTemplate

Relevant configuration:

spring:
  ai:
    chat:
      memory:
        repository:
          mongo:
            create-indices: true
            ttl: 20000
<!-- MongoDB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

        <!-- ############### BEGIN: SPRING AI ############### -->

        <!-- Spring AI (Chat, OpenAI Model, Chat Memory, AI Agent) -->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-openai</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-client-chat</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-chat-memory</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-chat-memory-repository-mongodb</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>

        <!-- MCP CLIENT -->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-mcp-client</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>

        <!-- ############### END: SPRING AI ############### -->

where: spring-ai.version=1.1.0

I have also tried to use newer version of spring-data-mongodb (4.3.x or more) and the error becomes: the following:

java.lang.IllegalStateException: Failed to introspect Class [org.springframework.data.mongodb.core.convert.MappingMongoConverter] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@1d44bcfa] at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:483) ~[spring-core-6.1.5.jar:6.1.5] at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:360) ~[spring-core-6.1.5.jar:6.1.5] at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:342) ~[spring-core-6.1.5.jar:6.1.5] at org.springframework.ai.mcp.annotation.spring.AbstractClientMcpHandlerRegistry.scan(AbstractClientMcpHandlerRegistry.java:128) ~[spring-ai-mcp-annotations-1.1.0.jar:1.1.0] at org.springframework.ai.mcp.annotation.spring.AbstractClientMcpHandlerRegistry.postProcessBeanFactory(AbstractClientMcpHandlerRegistry.java:77) ~[spring-ai-mcp-annotations-1.1.0.jar:1.1.0] at org.springframework.ai.mcp.annotation.spring.ClientMcpSyncHandlersRegistry.postProcessBeanFactory(ClientMcpSyncHandlersRegistry.java:65) ~[spring-ai-mcp-annotations-1.1.0.jar:1.1.0] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:363) ~[spring-context-6.1.5.jar:6.1.5] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:204) ~[spring-context-6.1.5.jar:6.1.5] at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:788) ~[spring-context-6.1.5.jar:6.1.5] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:606) ~[spring-context-6.1.5.jar:6.1.5] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.2.4.jar:3.2.4] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.2.4.jar:3.2.4] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.2.4.jar:3.2.4] at org.springframework.boot.SpringApplication.run(SpringApplication.java:334) ~[spring-boot-3.2.4.jar:3.2.4] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354) ~[spring-boot-3.2.4.jar:3.2.4] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-3.2.4.jar:3.2.4] at com.fw.ai_mcp_client.AiMcpClientApplication.main(AiMcpClientApplication.java:25) ~[classes/:na] Caused by: java.lang.NoClassDefFoundError: org/springframework/data/mapping/model/ValueExpressionEvaluator at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na] at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3402) ~[na:na] at java.base/java.lang.Class.getDeclaredMethods(Class.java:2504) ~[na:na] at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:465) ~[spring-core-6.1.5.jar:6.1.5] ... 16 common frames omitted Caused by: java.lang.ClassNotFoundException: org.springframework.data.mapping.model.ValueExpressionEvaluator at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na] at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na] ... 20 common frames omitted con spring data mongo db 4.2.12 ottengo An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.ai.model.chat.memory.repository.mongo.autoconfigure.MongoChatMemoryIndexCreatorAutoConfiguration.initIndicesAfterStartup(MongoChatMemoryIndexCreatorAutoConfiguration.java:61) The following method did not exist: 'java.lang.String org.springframework.data.mongodb.core.index.IndexOperations.createIndex(org.springframework.data.mongodb.core.index.IndexDefinition)' The calling method's class, org.springframework.ai.model.chat.memory.repository.mongo.autoconfigure.MongoChatMemoryIndexCreatorAutoConfiguration, was loaded from the following location: jar:file:/C:/Users/wbertolasi/.m2/repository/org/springframework/ai/spring-ai-autoconfigure-model-chat-memory-repository-mongodb/1.1.0/spring-ai-autoconfigure-model-chat-memory-repository-mongodb-1.1.0.jar!/org/springframework/ai/model/chat/memory/repository/mongo/autoconfigure/MongoChatMemoryIndexCreatorAutoConfiguration.class The called method's class, org.springframework.data.mongodb.core.index.IndexOperations, is available from the following locations: jar:file:/C:/Users/wbertolasi/.m2/repository/org/springframework/data/spring-data-mongodb/4.0.4/spring-data-mongodb-4.0.4.jar!/org/springframework/data/mongodb/core/index/IndexOperations.class The called method's class hierarchy was loaded from the following locations: org.springframework.data.mongodb.core.index.IndexOperations: file:/C:/Users/wbertolasi/.m2/repository/org/springframework/data/spring-data-mongodb/4.0.4/spring-data-mongodb-4.0.4.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.ai.model.chat.memory.repository.mongo.autoconfigure.MongoChatMemoryIndexCreatorAutoConfiguration and org.springframework.data.mongodb.core.index.IndexOperations

It seems there is no way starting the application with no errors.

Steps to reproduce

1. Configure Spring Boot + Spring AI with the configuration above.
2. Start the application.
3. Observe the error.

**Expected behavior**
When create-indices: true is set and ttl is defined, Spring AI should automatically create:
1. a TTL index (expireAfterSeconds = ttl) on the document creation date field (or equivalent)
2. any other necessary indices for managing the chat memory (e.g., on conversationId, createdAt, etc.)
These indices should be visible in MongoDB via getIndexes().

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions