spring-projects/spring-ai · error · IllegalArgumentException

Unsupported message type:

Error message

Unsupported message type: 

What it means

OllamaChatModel.ollamaChatRequest() maps Spring AI message types (UserMessage, AssistantMessage, SystemMessage, ToolResponseMessage) to Ollama API messages. Any other MessageType reaches the final throw of IllegalArgumentException('Unsupported message type: ...').

Source

Thrown at models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/OllamaChatModel.java:412

					toolCalls = assistantMessage.getToolCalls().stream().map(toolCall -> {
						var function = new ToolCallFunction(toolCall.name(),
								jsonHelper.fromJsonToMap(toolCall.arguments()));
						return new ToolCall(toolCall.id(), function);
					}).toList();
				}
				return List.of(OllamaApi.Message.builder(Role.ASSISTANT)
					.content(assistantMessage.getText())
					.toolCalls(toolCalls)
					.build());
			}
			else if (message.getMessageType() == MessageType.TOOL) {
				ToolResponseMessage toolMessage = (ToolResponseMessage) message;
				return toolMessage.getResponses()
					.stream()
					.map(tr -> OllamaApi.Message.builder(Role.TOOL).content(tr.responseData()).build())
					.toList();
			}
			throw new IllegalArgumentException("Unsupported message type: " + message.getMessageType());
		}).flatMap(List::stream).toList();

		OllamaChatOptions requestOptions = (OllamaChatOptions) prompt.getOptions();
		Assert.state(requestOptions != null, "ChatOptions must not be null");

		String model = requestOptions.getModel();
		Assert.state(model != null, "model must not be null");
		OllamaApi.ChatRequest.Builder requestBuilder = OllamaApi.ChatRequest.builder(model)
			.stream(stream)
			.messages(ollamaMessages)
			.options(requestOptions)
			.think(requestOptions.getThinkOption());

		if (requestOptions.getFormat() != null) {
			requestBuilder.format(requestOptions.getFormat());
		}

		if (requestOptions.getKeepAlive() != null) {

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Use only supported message types: UserMessage, AssistantMessage, SystemMessage, ToolResponseMessage.
  2. Filter or convert custom Message implementations before building the Prompt.
  3. Upgrade spring-ai-ollama if a newly introduced framework message type is not yet mapped.

Example fix

// before
messages.add(myCustomMessage); // unsupported type
// after
messages.add(new UserMessage(myCustomMessage.getText()));
Defensive patterns

Strategy: validation

Validate before calling

prompt.getInstructions().forEach(m -> {
    var t = m.getMessageType();
    if (!(t == MessageType.USER || t == MessageType.ASSISTANT || t == MessageType.SYSTEM || t == MessageType.TOOL)) {
        throw new IllegalArgumentException("Unsupported message type: " + t);
    }
});

Type guard

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

Try / catch

try {
    return ollamaChatModel.call(prompt);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Unsupported message type")) { /* drop/convert offending message and retry */ }
    throw e;
}

Prevention

When it happens

Trigger: Adding a message of an unsupported type (e.g. a custom Message implementation or ToolResponseMessage variant not handled) into the Prompt's message list and calling request().

Common situations: Custom chat memory returning bespoke Message implementations; combining messages produced by other model adapters; Spring AI upgrades introducing new message types before the Ollama adapter handles them.

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/fa24004f597d50e3. Report an issue: GitHub.