spring-projects/spring-ai · error · IllegalArgumentException

Unsupported media data type:

Error message

Unsupported media data type: 

What it means

OllamaChatModel.fromMediaData() converts Media payloads to Base64 strings for the Ollama API. It supports byte[] and String data; any other media data type throws IllegalArgumentException('Unsupported media data type: ...').

Source

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

		}

		List<ToolDefinition> toolDefinitions = this.toolCallingManager.resolveToolDefinitions(requestOptions);
		if (!CollectionUtils.isEmpty(toolDefinitions)) {
			requestBuilder.tools(this.getTools(toolDefinitions));
		}

		return requestBuilder.build();
	}

	private String fromMediaData(Object mediaData) {
		if (mediaData instanceof byte[] bytes) {
			return Base64.getEncoder().encodeToString(bytes);
		}
		else if (mediaData instanceof String text) {
			return text;
		}
		else {
			throw new IllegalArgumentException("Unsupported media data type: " + mediaData.getClass().getSimpleName());
		}

	}

	private List<ChatRequest.Tool> getTools(List<ToolDefinition> toolDefinitions) {
		return toolDefinitions.stream().map(toolDefinition -> {
			var tool = new ChatRequest.Tool.Function(toolDefinition.name(), toolDefinition.description(),
					toolDefinition.inputSchema());
			return new ChatRequest.Tool(tool);
		}).toList();
	}

	/**
	 * @since 2.0.0
	 */
	@Override
	public OllamaChatOptions getOptions() {
		return this.options;

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Convert media data to byte[] (e.g. resource.getContentAsByteArray()) or a Base64 String before building the Media.
  2. Read the file/Resource into a byte array explicitly when constructing the UserMessage media.
  3. Check for a Media utility in the framework (e.g. MediaUtils) to convert resources correctly.

Example fix

// before
new Media(MimeTypeUtils.IMAGE_PNG, new ClassPathResource("cat.png")); // unsupported
// after
new Media(MimeTypeUtils.IMAGE_PNG, new ClassPathResource("cat.png").getContentAsByteArray());
Defensive patterns

Strategy: validation

Validate before calling

Object data = media.getData();
if (!(data instanceof byte[]) && !(data instanceof String)) {
    throw new IllegalArgumentException("Convert media to byte[]/String first: " + data.getClass());
}

Type guard

boolean isSupportedMediaData(Object d) { return d instanceof byte[] || d instanceof String; }

Try / catch

try {
    return ollamaChatModel.call(prompt);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Unsupported media data type")) { /* convert media to byte[] and retry */ }
    throw e;
}

Prevention

When it happens

Trigger: Attaching a UserMessage Media whose getDataAsByteArray()/data object is e.g. a Resource, InputStream, or BufferedImage instead of byte[] or String, and sending it to Ollama.

Common situations: Loading images from classpath resources or files and passing the Resource object directly as media data instead of its byte contents; copying media handling code from other model adapters with different supported types.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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