spring-projects/spring-ai · error · IllegalStateException
Unsupported message type:
Error message
Unsupported message type:
What it means
MongoChatMemoryRepository.mapMessage() maps stored documents to Message objects for USER/ASSISTANT/SYSTEM; TOOL documents map to null and are filtered by the caller. Any other type string logs a warning and throws IllegalStateException to flag unsupported or corrupt data.
Source
Thrown at memory-repositories/spring-ai-model-chat-memory-repository-mongodb/src/main/java/org/springframework/ai/chat/memory/repository/mongo/MongoChatMemoryRepository.java:107
public void deleteByConversationId(String conversationId) {
this.mongoTemplate.remove(Query.query(Criteria.where("conversationId").is(conversationId)), Conversation.class);
}
public static @Nullable Message mapMessage(Conversation conversation) {
final String content = Objects.requireNonNullElse(conversation.message().content(), "");
return switch (conversation.message().type()) {
case "USER" -> UserMessage.builder().text(content).metadata(conversation.message().metadata()).build();
case "ASSISTANT" ->
AssistantMessage.builder().content(content).properties(conversation.message().metadata()).build();
case "SYSTEM" -> SystemMessage.builder().text(content).metadata(conversation.message().metadata()).build();
// this implementation doesn't support tool calls message persistence, so
// TOOL rows are filtered out by the caller
case "TOOL" -> null;
default -> {
if (logger.isWarnEnabled()) {
logger.warn("Unsupported message type: " + conversation.message().type());
}
throw new IllegalStateException("Unsupported message type: " + conversation.message().type());
}
};
}
public static Builder builder() {
return new Builder();
}
public final static class Builder {
private @Nullable MongoTemplate mongoTemplate;
private Builder() {
}
public Builder mongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
return this;View on GitHub (pinned to 98a7beda4f)
Solutions
- Find and fix/remove the document with the unsupported type (find documents where type is not in USER/ASSISTANT/SYSTEM/TOOL)
- Align library versions across services sharing the collection
- Pre-filter unsupported types at query time before mapping
Example fix
// before
db.chat_memory.insertOne({conversationId:'c1', type:'FUNCTION', ...}); // throws on read
// after
db.chat_memory.insertOne({conversationId:'c1', type:'ASSISTANT', ...}); Defensive patterns
Strategy: validation
Validate before calling
Document doc = collection.find(eq("conversationId", id));
// verify doc.getString("type") in [USER, ASSISTANT, SYSTEM, TOOL] before mapping Type guard
boolean isKnownType(String t) { return t != null && Set.of("USER","ASSISTANT","SYSTEM","TOOL").contains(t); } Try / catch
try { messages = repo.findByConversationId(id); }
catch (IllegalStateException e) { messages = List.of(); logger.warn("Unsupported stored message type", e); } Prevention
- Only let the Spring AI repository write to the chat-memory collection
- Keep library versions aligned across services sharing MongoDB
- Validate document types when migrating data into the collection
When it happens
Trigger: Reading a MongoDB chat-memory document whose 'type' field is not one of USER, ASSISTANT, SYSTEM, TOOL — e.g. documents written by another tool or a mismatched schema version.
Common situations: Another application shares the MongoDB collection and writes custom message types; manual document inserts; version skew between writer and reader.
Related errors
- unknown message type %s
- %s messages are not supported
- Dropping existing TTL index, because TTL is different
- MongoChatMemoryRepository does not support tool call message
- Unsupported message type: + conversation.message().type()
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/f375ec6cbbf9f21b.
Report an issue: GitHub.