spring-projects/spring-ai · error · IllegalArgumentException
Unsupported media type: . Supported types are: images (image
Error message
Unsupported media type: . Supported types are: images (image/*) and PDF documents (application/pdf)
What it means
AnthropicChatModel.createMediaBlockParam only supports image/* media and PDF documents (application/pdf) when converting a Media entry in a UserMessage into Anthropic content blocks. Any other MIME type (video, audio, text, application/json, or an unknown/blank type) hits the final throw. The library throws IllegalArgumentException because the Anthropic Messages API simply has no content-block representation for that media.
Source
Thrown at models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/AnthropicChatModel.java:1369
/**
* Converts a Spring AI {@link Media} object to an Anthropic SDK
* {@link ContentBlockParam}. Supports images (PNG, JPEG, GIF, WebP) and PDF
* documents. Data can be provided as byte[] (base64 encoded) or HTTPS URL string.
* @param media the media object containing MIME type and data
* @return the appropriate ContentBlockParam (ImageBlockParam or DocumentBlockParam)
* @throws IllegalArgumentException if the media type is unsupported
*/
private ContentBlockParam getContentBlockParamByMedia(Media media) {
MimeType mimeType = media.getMimeType();
String data = fromMediaData(media.getData());
if (isImageMedia(mimeType)) {
return createImageBlockParam(mimeType, data);
}
else if (isPdfMedia(mimeType)) {
return createDocumentBlockParam(data);
}
throw new IllegalArgumentException("Unsupported media type: " + mimeType
+ ". Supported types are: images (image/*) and PDF documents (application/pdf)");
}
/**
* Checks if the given MIME type represents an image.
* @param mimeType the MIME type to check
* @return true if the type is image/*
*/
private boolean isImageMedia(MimeType mimeType) {
return "image".equals(mimeType.getType());
}
/**
* Checks if the given MIME type represents a PDF document.
* @param mimeType the MIME type to check
* @return true if the type is application/pdf
*/
private boolean isPdfMedia(MimeType mimeType) {View on GitHub (pinned to 98a7beda4f)
Solutions
- Change the Media MIME type to an image type (image/png, image/jpeg, image/gif, image/webp) or application/pdf.
- If the file is another format, convert it (e.g. render a document page to PNG, or embed text directly in the prompt text instead of as Media).
- Verify the MIME type is not empty/generic: pass an explicit MimeType rather than relying on detection of application/octet-stream.
- If you need non-image/PDF media, switch to a model module that supports it (e.g. a different Spring AI model integration).
Example fix
// before
var media = new Media(MimeTypeUtils.APPLICATION_OCTET_STREAM, pdfBytes);
// after
var media = new Media(MimeType.valueOf("application/pdf"), pdfBytes); Defensive patterns
Strategy: validation
Validate before calling
MimeType type = media.getMimeType();
boolean supported = (type.getType().equals("image")) || type.toString().equals("application/pdf");
if (!supported) throw new IllegalArgumentException("Anthropic supports only image/* or application/pdf, got: " + type); Try / catch
try {
client.call(new Prompt(new UserMessage(text, List.of(media))));
} catch (IllegalArgumentException e) {
if (e.getMessage().startsWith("Unsupported media type")) {
log.error("Media {} not supported by Anthropic: {}", media.getMimeType(), e.getMessage());
} else throw e;
} Prevention
- Whitelist image/* and application/pdf at the point where Media objects are constructed.
- Never rely on detected MIME types like application/octet-stream; set explicit types.
- Convert text/other documents to prompt text or PDF before sending to Anthropic.
When it happens
Trigger: Passing a Media with a MIME type that is neither image/* nor application/pdf into a UserMessage, e.g. new Media(MimeTypeUtils.APPLICATION_OCTET_STREAM, bytes), MimeType.valueOf("audio/mpeg"), or a Media whose MIME type was not detected (empty string) and therefore does not match isImageMedia/isPdfMedia.
Common situations: Developers attaching audio, video, zip or plain-text files to prompts assuming multimodal support; building Media from a downloaded Resource with generic application/octet-stream content type; upgrading Spring AI and forgetting that only images and PDFs are accepted by the Anthropic model.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Unsupported image type: . Supported types: image/png, image/
- Unsupported media data type: . Expected byte[] or String.
- httpClientBuilderCustomizers cannot be combined with a pre-b
- httpClientBuilderCustomizers cannot be combined with a pre-b
- Anthropic Citations API requires all documents to have consi
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/7bede6f90ac29c02.
Report an issue: GitHub.