spring-projects/spring-ai · error · IllegalArgumentException

Unsupported image data type:

Error message

Unsupported image data type: 

What it means

VertexAiEmbeddingUtils.ImageBuilder.imageData(Object) accepts only byte[] (raw image bytes) or String (a GCS URI, gs://...). Any other type falls through to IllegalArgumentException("Unsupported image data type: " + imageData.getClass()), telling you which class was supplied so you can convert it.

Source

Thrown at models/spring-ai-vertex-ai-embedding/src/main/java/org/springframework/ai/vertexai/embedding/VertexAiEmbeddingUtils.java:271

		public MimeType mimeType;

		public static ImageBuilder of(MimeType mimeType) {
			Assert.notNull(mimeType, "MimeType must not be null");
			var builder = new ImageBuilder();
			builder.mimeType = mimeType;
			return builder;
		}

		public ImageBuilder imageData(Object imageData) {
			Assert.notNull(imageData, "Image data must not be null");
			if (imageData instanceof byte[] bytes) {
				return imageBytes(bytes);
			}
			else if (imageData instanceof String uri) {
				return gcsUri(uri);
			}
			else {
				throw new IllegalArgumentException("Unsupported image data type: " + imageData.getClass());
			}
		}

		public ImageBuilder imageBytes(byte[] imageBytes) {
			Assert.notNull(imageBytes, "Image bytes must not be null");
			this.imageBytes = imageBytes;
			return this;
		}

		public ImageBuilder gcsUri(String gcsUri) {
			Assert.hasText(gcsUri, "GCS URI must not be empty");
			this.gcsUri = gcsUri;
			return this;
		}

		public Struct build() {

			Struct.Builder imageBuilder = Struct.newBuilder();

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Convert the object to byte[] (read file bytes or encode the BufferedImage) and pass imageBytes(...).
  2. If the image lives in Google Cloud Storage, pass its gs:// URI as a String.
  3. Check the reported class name in the message to identify exactly which type slipped through.

Example fix

// before
image.imageData(new File("cat.jpg"));
// after
image.imageData(Files.readAllBytes(Path.of("cat.jpg")));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(data instanceof byte[]) && !(data instanceof String)) {
    throw new IllegalArgumentException("imageData must be byte[] or String GCS URI, got " + data.getClass());
}

Type guard

static boolean isSupportedImageData(Object o) {
    return o instanceof byte[] || (o instanceof String s && s.startsWith("gs://"));
}

Prevention

When it happens

Trigger: Calling image.imageData(...) with a type other than byte[] or String — e.g. java.io.File, BufferedImage, InputStream, Path, or a byte[]-like wrapper.

Common situations: Passing a java.io.File or BufferedImage directly from image-processing code; passing a URI/URL object instead of the gs:// string; loading images with ImageIO and forgetting to encode to bytes.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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