spring-projects/spring-ai · error · IllegalArgumentException

Invalid Image content type:

Error message

Invalid Image content type: 

What it means

Thrown when Media.getData() is neither String nor URL — i.e. the media payload is an unsupported object type. mapMediaToContentBlock only understands base64/URL Strings and java.net.URL instances (plus byte-array-backed data via the document branch); any other class reaches this IllegalArgumentException, which names the actual class.

Source

Thrown at models/spring-ai-bedrock-converse/src/main/java/org/springframework/ai/bedrock/converse/BedrockProxyChatModel.java:573

					sourceBuilder.bytes(SdkBytes.fromByteArray(Base64.getDecoder().decode(text)));
				}
			}
			else if (media.getData() instanceof URL url) {

				try {
					String protocol = url.getProtocol();
					if (!"http".equalsIgnoreCase(protocol) && !"https".equalsIgnoreCase(protocol)) {
						throw new SecurityException("Unsupported URL protocol: " + protocol);
					}
					byte[] bytes = this.mediaFetcher.fetch(url.toURI());
					sourceBuilder.bytes(SdkBytes.fromByteArrayUnsafe(bytes)).build();
				}
				catch (SecurityException | RestClientException | URISyntaxException e) {
					throw new IllegalArgumentException("Failed to read media data from URL: " + url, e);
				}
			}
			else {
				throw new IllegalArgumentException("Invalid Image content type: " + media.getData().getClass());
			}

			return ContentBlock.fromImage(ImageBlock.builder()
				.source(sourceBuilder.build())
				.format(BedrockMediaFormat.getImageFormat(mimeType))
				.build());
		}
		else if (BedrockMediaFormat.isSupportedDocumentFormat(mimeType)) { // Document

			return ContentBlock.fromDocument(DocumentBlock.builder()
				.name(sanitizeDocumentName(media.getName()))
				.format(BedrockMediaFormat.getDocumentFormat(mimeType))
				.source(DocumentSource.builder().bytes(SdkBytes.fromByteArray(media.getDataAsByteArray())).build())
				.build());
		}

		throw new IllegalArgumentException("Unsupported media format: " + mimeType);
	}

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Convert the object to byte[] and pass the bytes (or their base64 String) as media data.
  2. For Spring Resource, call resource.getInputStream().readAllBytes() and pass the bytes.
  3. Only use String (base64 or strict http(s) URL) or java.net.URL for image Media data.
  4. Log media.getData().getClass() when debugging to see what type leaked through.

Example fix

// before
new Media(MimeTypeUtils.IMAGE_PNG, new File("cat.png"));
// after
byte[] bytes = new File("cat.png").toPath().readAllBytes(); // or Files.readAllBytes
new Media(MimeTypeUtils.IMAGE_PNG, bytes);
Defensive patterns

Strategy: type-guard

Validate before calling

Object d = media.getData();
if (!(d instanceof String) && !(d instanceof URL) && !(d instanceof byte[])) {
    throw new IllegalArgumentException("Media data must be String, URL or byte[], got: " + d.getClass());
}

Type guard

boolean hasSupportedMediaData(Media m) {
    Object d = m.getData();
    return d instanceof String || d instanceof URL;
}

Try / catch

try {
    model.call(prompt);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Invalid Image content type")) {
        // convert media.getData() to byte[]/base64 and retry
    } else throw e;
}

Prevention

When it happens

Trigger: Passing Media constructed with an arbitrary object as data, e.g. a java.io.File, Path, InputStream, Resource, or BufferedImage instead of String/URL/byte-array-supported types.

Common situations: Migrating from other Spring AI model implementations whose Media accepted Resource or File; confusion over Media API variants; building Media from an InputStream read from disk.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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