spring-projects/spring-ai · error · RuntimeException

Unknown message type:

Error message

Unknown message type: 

What it means

VectorStoreChatMemoryAdvisor.toDocuments converts the conversation's messages into Documents for the vector store, mapping UserMessage, SystemMessage and AssistantMessage types. If a message of another MessageType appears in the list, it throws this RuntimeException because there is no mapping for it. This typically happens when tool/function-call or ToolResponseMessage instances end up in the memory history.

Source

Thrown at advisors/spring-ai-vector-store-advisor/src/main/java/org/springframework/ai/chat/client/advisor/vectorstore/VectorStoreChatMemoryAdvisor.java:235

			.map(message -> {
				Map<String, Object> metadata = new HashMap<>(
						message.getMetadata() != null ? message.getMetadata() : new HashMap<>());
				metadata.put(DOCUMENT_METADATA_CONVERSATION_ID, conversationId);
				metadata.put(DOCUMENT_METADATA_MESSAGE_TYPE, message.getMessageType().name());
				if (message instanceof UserMessage userMessage) {
					return Document.builder()
						.text(userMessage.getText())
						// userMessage.getMedia().get(0).getId()
						// TODO vector store for memory would not store this into the
						// vector store, could store an 'id' instead
						// .media(userMessage.getMedia())
						.metadata(metadata)
						.build();
				}
				else if (message instanceof AssistantMessage assistantMessage) {
					return Document.builder().text(assistantMessage.getText()).metadata(metadata).build();
				}
				throw new RuntimeException("Unknown message type: " + message.getMessageType());
			})
			.toList();
	}

	/**
	 * Builder for VectorStoreChatMemoryAdvisor.
	 */
	public static final class Builder {

		private PromptTemplate systemPromptTemplate = DEFAULT_SYSTEM_PROMPT_TEMPLATE;

		private Integer defaultTopK = DEFAULT_TOP_K;

		private Scheduler scheduler = BaseAdvisor.DEFAULT_SCHEDULER;

		private int order = Advisor.DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDER;

		private final VectorStore vectorStore;

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Filter the memory to only user/assistant/system messages before it reaches the advisor.
  2. Use a memory implementation or window that excludes ToolResponseMessage entries.
  3. Upgrade spring-ai — newer versions may add handling for additional message types.
  4. As a workaround, wrap the ChatMemory so add() skips tool-response messages.

Example fix

// before
chatMemory.add(conversationId, toolResponseMessage);

// after
if (message instanceof UserMessage || message instanceof AssistantMessage || message instanceof SystemMessage) {
    chatMemory.add(conversationId, message);
}
Defensive patterns

Strategy: type-guard

Validate before calling

boolean memorySafe(List<Message> messages) { return messages.stream().allMatch(m -> m instanceof UserMessage || m instanceof AssistantMessage || m instanceof SystemMessage); }

Type guard

boolean isSupportedMessageType(Message m) {
    return m instanceof UserMessage || m instanceof AssistantMessage || m instanceof SystemMessage;
}

Try / catch

try {
    return advisor.before(body, call);
} catch (RuntimeException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Unknown message type:")) {
        // purge tool-response messages from memory and retry
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling advisor.before/after on a conversation whose Message list contains a message that is not a UserMessage, SystemMessage or AssistantMessage (e.g. ToolResponseMessage from tool-calling results).

Common situations: Using ChatMemory that stores tool execution responses, then re-serving that memory to a conversation augmented with the vector-store memory advisor; mixing memory advisors that record full tool-call transcripts.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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