{"record":{"id":"c445a29ddd4ff81a","repo":"spring-projects/spring-ai","slug":"invalid-image-content-type","errorCode":null,"errorMessage":"Invalid Image content type: ","messagePattern":"Invalid Image content type: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-bedrock-converse/src/main/java/org/springframework/ai/bedrock/converse/BedrockProxyChatModel.java","lineNumber":573,"sourceCode":"\t\t\t\t\tsourceBuilder.bytes(SdkBytes.fromByteArray(Base64.getDecoder().decode(text)));\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (media.getData() instanceof URL url) {\n\n\t\t\t\ttry {\n\t\t\t\t\tString protocol = url.getProtocol();\n\t\t\t\t\tif (!\"http\".equalsIgnoreCase(protocol) && !\"https\".equalsIgnoreCase(protocol)) {\n\t\t\t\t\t\tthrow new SecurityException(\"Unsupported URL protocol: \" + protocol);\n\t\t\t\t\t}\n\t\t\t\t\tbyte[] bytes = this.mediaFetcher.fetch(url.toURI());\n\t\t\t\t\tsourceBuilder.bytes(SdkBytes.fromByteArrayUnsafe(bytes)).build();\n\t\t\t\t}\n\t\t\t\tcatch (SecurityException | RestClientException | URISyntaxException e) {\n\t\t\t\t\tthrow new IllegalArgumentException(\"Failed to read media data from URL: \" + url, e);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthrow new IllegalArgumentException(\"Invalid Image content type: \" + media.getData().getClass());\n\t\t\t}\n\n\t\t\treturn ContentBlock.fromImage(ImageBlock.builder()\n\t\t\t\t.source(sourceBuilder.build())\n\t\t\t\t.format(BedrockMediaFormat.getImageFormat(mimeType))\n\t\t\t\t.build());\n\t\t}\n\t\telse if (BedrockMediaFormat.isSupportedDocumentFormat(mimeType)) { // Document\n\n\t\t\treturn ContentBlock.fromDocument(DocumentBlock.builder()\n\t\t\t\t.name(sanitizeDocumentName(media.getName()))\n\t\t\t\t.format(BedrockMediaFormat.getDocumentFormat(mimeType))\n\t\t\t\t.source(DocumentSource.builder().bytes(SdkBytes.fromByteArray(media.getDataAsByteArray())).build())\n\t\t\t\t.build());\n\t\t}\n\n\t\tthrow new IllegalArgumentException(\"Unsupported media format: \" + mimeType);\n\t}","sourceCodeStart":555,"sourceCodeEnd":591,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-bedrock-converse/src/main/java/org/springframework/ai/bedrock/converse/BedrockProxyChatModel.java#L555-L591","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nnew Media(MimeTypeUtils.IMAGE_PNG, new File(\"cat.png\"));\n// after\nbyte[] bytes = new File(\"cat.png\").toPath().readAllBytes(); // or Files.readAllBytes\nnew Media(MimeTypeUtils.IMAGE_PNG, bytes);","handlingStrategy":"type-guard","validationCode":"Object d = media.getData();\nif (!(d instanceof String) && !(d instanceof URL) && !(d instanceof byte[])) {\n    throw new IllegalArgumentException(\"Media data must be String, URL or byte[], got: \" + d.getClass());\n}","typeGuard":"boolean hasSupportedMediaData(Media m) {\n    Object d = m.getData();\n    return d instanceof String || d instanceof URL;\n}","tryCatchPattern":"try {\n    model.call(prompt);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Invalid Image content type\")) {\n        // convert media.getData() to byte[]/base64 and retry\n    } else throw e;\n}","preventionTips":["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"],"tags":["type-mismatch","media","bedrock"],"backgroundTag":"incompatible-source-type","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}