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
- Convert the object to byte[] and pass the bytes (or their base64 String) as media data.
- For Spring Resource, call resource.getInputStream().readAllBytes() and pass the bytes.
- Only use String (base64 or strict http(s) URL) or java.net.URL for image Media data.
- 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
- Only construct Media with String (base64/URL) or URL data for images
- Convert File/Path/Resource/InputStream to byte[] before wrapping in Media
- Check Media API docs for supported data types per model implementation
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
- Unsupported media data type: . Expected byte[] or String.
- Invalid video content type:
- Unsupported media format:
- Unsupported media format:
- Unsupported value type:
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/c445a29ddd4ff81a.
Report an issue: GitHub.