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

Unsupported media format:

Error message

Unsupported media format: 

What it means

Terminal guard in mapMediaToContentBlock: after the document and image branches, if the Media's MIME type matches no supported document or image format, this IllegalArgumentException is thrown. Bedrock only accepts a fixed set of media formats and the library maps MimeType via BedrockMediaFormat.

Source

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

			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);
	}

	/**
	 * Sanitizes a document name to conform to Amazon Bedrock's naming restrictions. The
	 * name can only contain alphanumeric characters, whitespace characters (no more than
	 * one in a row), hyphens, parentheses, and square brackets.
	 * @param name the document name to sanitize
	 * @return the sanitized document name
	 * @see <a href=
	 * "https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_DocumentBlock.html">DocumentBlock
	 * API Reference</a>
	 */
	static String sanitizeDocumentName(String name) {
		return name.replaceAll("[^a-zA-Z0-9\\s\\-()\\[\\]]", "-");
	}

	/**
	 * Convert {@link ConverseResponse} to {@link ChatResponse} includes model output,

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Convert the media to a supported format (png, jpeg, gif, webp per Bedrock docs) before sending.
  2. Use 'image/jpeg' not 'image/jpg'; verify the MimeType constant matches the actual bytes.
  3. Check BedrockMediaFormat's supported format maps to confirm the type before calling.
  4. For documents use supported formats (pdf, csv, doc, docx, xls, xlsx, html, txt, md).

Example fix

// before
Media media = new Media(new MimeType("image", "svg+xml"), svgBytes);
// after
BufferedImage img = ImageIO.read(/* svg rendered to raster */);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(img, "png", out);
Media media = new Media(MimeTypeUtils.IMAGE_PNG, out.toByteArray());
Defensive patterns

Strategy: validation

Validate before calling

MimeType mt = media.getMimeType();
if (!BedrockMediaFormat.isSupportedImageFormat(mt) && !BedrockMediaFormat.isSupportedDocumentFormat(mt)) {
    throw new IllegalArgumentException("Unsupported media for Bedrock: " + mt);
}

Type guard

boolean isBedrockSupportedMedia(MimeType mt) {
    return Boolean.TRUE.equals(BedrockMediaFormat.isSupportedImageFormat(mt))
        || Boolean.TRUE.equals(BedrockMediaFormat.isSupportedDocumentFormat(mt));
}

Try / catch

try {
    model.call(prompt);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Unsupported media format")) {
        // convert to PNG/JPEG/PDF and retry
    } else throw e;
}

Prevention

When it happens

Trigger: Passing a Media with a MimeType not in BedrockMediaFormat's image/document maps — e.g. image/svg+xml, image/webp (if unsupported), audio formats, application/pdf以外的 unsupported types — so neither getImageFormat nor getDocumentFormat matches.

Common situations: Sending WebP or SVG images to Bedrock (not supported by the Converse API); sending audio files expecting transcription through chat; typos in MimeType like 'image/jpg' instead of 'image/jpeg'.

Related errors


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