spring-projects/spring-ai · error · IllegalArgumentException

Unsupported image type: . Supported types: image/png, image/

Error message

Unsupported image type: . Supported types: image/png, image/jpeg, image/gif, image/webp

What it means

Even when the MIME type is image/*, the Anthropic API only accepts four image subtypes: png, jpeg/jpg, gif and webp. AnthropicChatModel maps the subtype to a Base64ImageSource.MediaType enum and throws IllegalArgumentException for any other subtype (e.g. image/svg+xml, image/bmp, image/tiff, image/heic).

Source

Thrown at models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/AnthropicChatModel.java:1456

			source = DocumentBlockParam.Source.ofBase64(Base64PdfSource.builder().data(data).build());
		}
		return ContentBlockParam.ofDocument(DocumentBlockParam.builder().source(source).build());
	}

	/**
	 * Converts a Spring MIME type to the SDK's {@link Base64ImageSource.MediaType}.
	 * @param mimeType the Spring MIME type
	 * @return the SDK media type enum value
	 * @throws IllegalArgumentException if the image type is unsupported
	 */
	private Base64ImageSource.MediaType toSdkImageMediaType(MimeType mimeType) {
		String subtype = mimeType.getSubtype();
		return switch (subtype) {
			case "png" -> Base64ImageSource.MediaType.IMAGE_PNG;
			case "jpeg", "jpg" -> Base64ImageSource.MediaType.IMAGE_JPEG;
			case "gif" -> Base64ImageSource.MediaType.IMAGE_GIF;
			case "webp" -> Base64ImageSource.MediaType.IMAGE_WEBP;
			default -> throw new IllegalArgumentException("Unsupported image type: " + mimeType
					+ ". Supported types: image/png, image/jpeg, image/gif, image/webp");
		};
	}

	/**
	 * Applies {@code disableParallelToolUse} to an existing {@link ToolChoice} by
	 * rebuilding the appropriate subtype with the flag set to {@code true}.
	 */
	private ToolChoice applyDisableParallelToolUse(ToolChoice toolChoice) {
		if (toolChoice.isAuto()) {
			return ToolChoice.ofAuto(toolChoice.asAuto().toBuilder().disableParallelToolUse(true).build());
		}
		else if (toolChoice.isAny()) {
			return ToolChoice.ofAny(toolChoice.asAny().toBuilder().disableParallelToolUse(true).build());
		}
		else if (toolChoice.isTool()) {
			return ToolChoice.ofTool(toolChoice.asTool().toBuilder().disableParallelToolUse(true).build());
		}

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Convert the image to PNG or JPEG before sending (ImageIO.write or a library like Thumbnailator/TwelveMonkeys).
  2. Re-encode SVG by rasterizing it to PNG (e.g. Batik) — Anthropic cannot ingest vector SVG.
  3. Convert HEIC photos to JPEG on the client or server before building the Media.
  4. Double-check the MIME type string for typos (e.g. 'image/jpg' actually works via the 'jpg' case, but 'image/pjpeg' does not).

Example fix

// before
var media = new Media(MimeType.valueOf("image/svg+xml"), svgBytes);
// after
BufferedImage img = rasterize(svgBytes);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(img, "png", out);
var media = new Media(MimeType.valueOf("image/png"), out.toByteArray());
Defensive patterns

Strategy: validation

Validate before calling

Set<String> ok = Set.of("png", "jpeg", "jpg", "gif", "webp");
if (!"image".equals(mimeType.getType()) || !ok.contains(mimeType.getSubtype().toLowerCase())) {
    throw new IllegalArgumentException("Convert image to png/jpeg/gif/webp: " + mimeType);
}

Try / catch

try {
    return client.call(prompt);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Unsupported image type")) {
        byte[] converted = convertToPng(media.getData());
        return client.call(rebuildPromptWith(media, MimeType.valueOf("image/png"), converted));
    }
    throw e;
}

Prevention

When it happens

Trigger: Sending a Media with an image MIME type outside the supported set, such as image/svg+xml, image/bmp, image/tiff, image/x-icon or image/heic (iPhone photos) — the switch falls through to default and throws.

Common situations: Uploading screenshots in BMP or TIFF from legacy tools; passing SVG vector graphics; iOS HEIC photos forwarded directly; icon files (image/x-icon) attached to prompts.

Related errors


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