spring-projects/spring-ai · error · java.lang.IllegalArgumentException

Unsupported message type:

Error message

Unsupported message type: 

What it means

GoogleGenAiChatModel.toGeminiMessageType() maps Spring AI MessageType values to Gemini roles: SYSTEM, USER, and TOOL map to USER; ASSISTANT maps to MODEL. Any other/unknown MessageType has no Gemini equivalent, so the switch default throws this IllegalArgumentException.

Source

Thrown at models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/GoogleGenAiChatModel.java:237

		this.cachedContentService = (genAiClient != null && genAiClient.caches != null && genAiClient.async != null
				&& genAiClient.async.caches != null) ? new GoogleGenAiCachedContentService(genAiClient) : null;

		if (toolCallingManager instanceof GoogleGenAiToolCallingManager) {
			this.toolCallingManager = toolCallingManager;
		}
		else {
			this.toolCallingManager = new GoogleGenAiToolCallingManager(toolCallingManager);
		}
	}

	private static GeminiMessageType toGeminiMessageType(MessageType type) {

		Assert.notNull(type, "Message type must not be null");

		return switch (type) {
			case SYSTEM, USER, TOOL -> GeminiMessageType.USER;
			case ASSISTANT -> GeminiMessageType.MODEL;
			default -> throw new IllegalArgumentException("Unsupported message type: " + type);
		};
	}

	List<Part> messageToGeminiParts(Message message) {

		if (message instanceof SystemMessage systemMessage) {

			List<Part> parts = new ArrayList<>();

			if (systemMessage.getText() != null) {
				parts.add(Part.fromText(systemMessage.getText()));
			}

			return parts;
		}
		else if (message instanceof UserMessage userMessage) {
			List<Part> parts = new ArrayList<>();
			if (userMessage.getText() != null) {

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Use only standard message types: SystemMessage, UserMessage, AssistantMessage, or ToolResponseMessage.
  2. Fix custom Message implementations to return one of the supported MessageType values.
  3. Align Spring AI versions so the chat model supports all MessageType values present in your classpath.

Example fix

// before
Message msg = new MyCustomMessage("hi", MessageType.FUNCTION);

// after
Message msg = new UserMessage("hi");
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(type == MessageType.SYSTEM || type == MessageType.USER || type == MessageType.ASSISTANT || type == MessageType.TOOL)) {
    throw new IllegalArgumentException("MessageType not supported by Gemini: " + type);
}

Prevention

When it happens

Trigger: Passing a Message whose getMessageType() returns a custom or unrecognized MessageType (not SYSTEM, USER, ASSISTANT, or TOOL) into the chat model's contents() conversion path — typically via a custom Message implementation.

Common situations: Custom Message subclasses returning a bespoke MessageType enum value; version drift where a new MessageType was added in Spring AI but this model's switch wasn't updated; routing a function-result message type through the wrong channel.

Related errors


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