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

Unsupported media data type:

Error message

Unsupported media data type: 

What it means

In the chat model's mediaToParts(), media data must be byte[] (via fromBytes) or URI/String (via fromUri). Other data types cannot be represented as a Gemini Part, so an IllegalArgumentException including the data's class is thrown. This is the chat-model counterpart of the image model's equivalent check.

Source

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

	}

	private static List<Part> mediaToParts(Collection<Media> media) {
		List<Part> parts = new ArrayList<>();

		List<Part> mediaParts = media.stream().map(mediaData -> {
			Object data = mediaData.getData();
			String mimeType = mediaData.getMimeType().toString();

			if (data instanceof byte[]) {
				return Part.fromBytes((byte[]) data, mimeType);
			}
			else if (data instanceof URI || data instanceof String) {
				// Handle URI or String URLs
				String uri = data.toString();
				return Part.fromUri(uri, mimeType);
			}
			else {
				throw new IllegalArgumentException("Unsupported media data type: " + data.getClass());
			}
		}).toList();

		if (!CollectionUtils.isEmpty(mediaParts)) {
			parts.addAll(mediaParts);
		}

		return parts;
	}

	// Helper methods for JSON/Map conversion
	private Map<String, Object> parseJsonToMap(String json) {
		try {
			// First, try to parse as an array
			Object parsed = this.jsonMapper.readValue(json, Object.class);
			if (parsed instanceof List) {
				// It's an array, wrap it in a map with "result" key
				Map<String, Object> wrapper = new HashMap<>();

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Convert resource/file input to byte[] (Resource.getContentAsByteArray(), Files.readAllBytes) before constructing the Media.
  2. For URL-based media, pass a java.net.URI or String URL so it maps to Part.fromUri.
  3. Centralize media creation in a helper that only emits byte[] or URI data and fails early with a clear message.

Example fix

// before
Media media = new Media(MimeTypeUtils.IMAGE_JPEG, resource); // Resource

// after
Media media = new Media(MimeTypeUtils.IMAGE_JPEG, resource.getContentAsByteArray());
Defensive patterns

Strategy: type-guard

Type guard

static boolean isSupportedChatMediaData(Object data) {
    return data instanceof byte[] || data instanceof URI || data instanceof String;
}

Prevention

When it happens

Trigger: Attaching a Media to a UserMessage where data is an InputStream, Resource, File, Path, or other non-byte[]/non-URI object, then sending the prompt through GoogleGenAiChatModel.call()/stream().

Common situations: Multimodal prompts built from ClassPathResource/UrlResource objects passed directly as Media data; passing java.io.File or Path from a file picker; code ported from an OpenAI adapter that accepted different media 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/72ceb6267847f419. Report an issue: GitHub.