spring-projects/spring-ai · error · IllegalStateException
Failed to invoke createIndex() method
Error message
Failed to invoke createIndex() method
What it means
This is the first branch of the same reflective index creation: when createIndex(IndexDefinition) is looked up successfully on IndexOperations but invoking it throws a ReflectiveOperationException, the Mongo chat memory auto-configuration wraps it in an IllegalStateException with this message. The real failure is in the chained 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:135
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
- Read the wrapped cause exception to find the actual MongoDB error.
- Confirm the index definition (field, direction, TTL options) is valid for your server version.
- Ensure the database user can create indexes on the chat memory collection.
- Align spring-data-mongodb and mongodb-driver versions with the Spring AI BOM.
Example fix
// before
catch (ReflectiveOperationException ex) { throw new IllegalStateException("Failed to invoke createIndex() method", ex); } // cause hidden
// after
catch (ReflectiveOperationException ex) { logger.error("createIndex failed: {}", ex.getCause()); throw new IllegalStateException("Failed to invoke createIndex() method", ex); } Defensive patterns
Strategy: try-catch
Validate before calling
// pre-check: collection is writable and driver/server compatible
mongoTemplate.collectionExists("ai_chat_memory"); Prevention
- Validate index key names and options before deploy.
- Ensure DB user has index-creation permissions.
- Keep mongodb driver and spring-data-mongodb versions aligned via the BOM.
When it happens
Trigger: createIndexSafely resolves Method createIndex(IndexDefinition) but method.invoke(indexOps, index) throws IllegalAccessException or InvocationTargetException while called from createMainIndex or createOrUpdateTtlIndex at startup.
Common situations: MongoDB rejecting the index specification (bad keys, unsupported options); insufficient write permissions on the collection; driver/server version mismatch on index options like partialFilterExpression or expireAfterSeconds.
Related errors
- Failed to invoke ensureIndex() 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/3fe784243fcb67c8.
Report an issue: GitHub.