spring-projects/spring-ai · error · IllegalStateException
Failed to invoke ensureIndex() method
Error message
Failed to invoke ensureIndex() method
What it means
After locating IndexOperations#ensureIndex via reflection, the Mongo chat memory index creator invokes it. Any ReflectiveOperationException during that invocation (IllegalAccessException, or InvocationTargetException wrapping the real failure) is rethrown as IllegalStateException with this message and the reflective cause attached. The index was found but could not actually be created.
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:131
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
- Inspect the chained cause (InvocationTargetException target) for the real MongoDB/ driver error.
- Verify the MongoDB user has createIndex privileges on the target database/collection.
- Check the configured index definition (field names, TTL seconds) is valid for your MongoDB server version.
- Align Spring Data MongoDB version with Spring AI's expectations so the correct API path is used.
Example fix
// before
logger.info("Creating index with keys: " + index.getIndexKeys()); // opaque
// after
try { indexCreator.createIndexSafely(indexOps, index); } catch (IllegalStateException e) { logger.error("Index creation failed: ", e.getCause()); throw e; } Defensive patterns
Strategy: try-catch
Validate before calling
// pre-check permissions and connectivity
mongoTemplate.getDb().runCommand(new Document("connectionStatus", 1)); Prevention
- Grant the MongoDB user createIndex privileges on the chat memory collection.
- Validate index definitions (keys, TTL) against your MongoDB server version.
- Log the cause chain, not just the wrapper message.
When it happens
Trigger: createIndexSafely (called by createMainIndex/createOrUpdateTtlIndex) finds ensureIndex(IndexDefinition) but method.invoke(indexOps, index) throws — e.g. the underlying MongoDB driver rejects the index definition, the method is inaccessible, or the wrapped invocation fails.
Common situations: MongoDB server rejecting the index keys/options (invalid field names, collation issues); IndexOperations bound to a database the app cannot write to; incompatible IndexDefinition type for the resolved method; security manager or module access restrictions.
Related errors
- Failed to invoke createIndex() method
- Neither createIndex() nor ensureIndex() method found on Inde
- Required no-arg constructor not found in
- instantiation failed
- Method must not be null
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/0f26e64297afdc96.
Report an issue: GitHub.