spring-projects/spring-ai · error · IllegalStateException

Neither createIndex() nor ensureIndex() method found on Inde

Error message

Neither createIndex() nor ensureIndex() method found on IndexOperations. This may indicate an unsupported Spring Data MongoDB version.

What it means

MongoChatMemory's index creator uses reflection on Spring Data MongoDB's IndexOperations, first trying createIndex() and then ensureIndex(). If neither reflective lookup succeeds, it concludes the classpath contains an unsupported Spring Data MongoDB version whose IndexOperations API has changed, and throws this IllegalStateException with the NoSuchMethodException as cause.

Source

Thrown at auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-mongodb/src/main/java/org/springframework/ai/model/chat/memory/repository/mongo/autoconfigure/MongoChatMemoryIndexCreatorAutoConfiguration.java:125

	 * @param index the index definition
	 * @throws IllegalStateException if neither method is available or invocation fails
	 */
	private void createIndexSafely(final IndexOperations indexOps, final IndexDefinition index) {
		try {
			// Try new API (Spring Data MongoDB 4.5.x+)
			Method method = IndexOperations.class.getMethod("createIndex", IndexDefinition.class);
			method.invoke(indexOps, index);
			logger.debug("Created index using createIndex() method");
		}
		catch (NoSuchMethodException createIndexNotFound) {
			// Fall back to old API (Spring Data MongoDB 4.2.x - 4.4.x)
			try {
				Method method = IndexOperations.class.getMethod("ensureIndex", IndexDefinition.class);
				method.invoke(indexOps, index);
				logger.debug("Created index using ensureIndex() method");
			}
			catch (NoSuchMethodException ensureIndexNotFound) {
				throw new IllegalStateException(
						"Neither createIndex() nor ensureIndex() method found on IndexOperations. "
								+ "This may indicate an unsupported Spring Data MongoDB version.",
						ensureIndexNotFound);
			}
			catch (ReflectiveOperationException ex) {
				throw new IllegalStateException("Failed to invoke ensureIndex() method", ex);
			}
		}
		catch (ReflectiveOperationException ex) {
			throw new IllegalStateException("Failed to invoke createIndex() method", ex);
		}
	}

}

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Upgrade/align spring-data-mongodb to a version compatible with your Spring AI release (check spring-ai-starter_mongodb dependencies in the BOM).
  2. Run mvn dependency:tree (or gradle dependencies) and resolve version conflicts so only one spring-data-mongodb version is on the classpath.
  3. Pin the Spring Boot/Spring AI BOM versions together so Spring Data MongoDB matches what the auto-configuration expects.

Example fix

// before (pom.xml)
<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-mongodb</artifactId><version>3.4.0</version></dependency>
// after
<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-mongodb</artifactId></dependency> <!-- version managed by Spring Boot BOM -->
Defensive patterns

Strategy: validation

Validate before calling

try {
    IndexOperations.class.getMethod("ensureIndex", IndexDefinition.class);
} catch (NoSuchMethodException e) {
    throw new IllegalStateException("Incompatible spring-data-mongodb on classpath; align versions with the Spring AI BOM", e);
}

Try / catch

try {
    context.getBean(MongoChatMemoryIndexCreatorAutoConfiguration.class);
} catch (IllegalStateException e) {
    logger.error("IndexOperations API mismatch: {}", e.getCause());
    throw e;
}

Prevention

When it happens

Trigger: createIndexSafely is invoked by createMainIndex/createOrUpdateTtlIndex during bean initialization and both Method lookups (createIndex, then ensureIndex) on IndexOperations.class throw NoSuchMethodException — i.e. no method with signature (IndexDefinition) exists on the resolved IndexOperations class.

Common situations: Mixing Spring Boot versions where spring-data-mongodb on the classpath is older or newer than the version Spring AI was built against; dependency conflicts pulling a stripped/proxied IndexOperations; using a custom IndexOperations implementation.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/fa2e1bb5808a6fe1. Report an issue: GitHub.