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

  1. Find and fix/remove the document with the unsupported type (find documents where type is not in USER/ASSISTANT/SYSTEM/TOOL)
  2. Align library versions across services sharing the collection
  3. 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

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


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