{"record":{"id":"ae64bd0a228c5c0b","repo":"spring-projects/spring-ai","slug":"unsupported-media-data-type","errorCode":null,"errorMessage":"Unsupported media data type: ","messagePattern":"Unsupported media data type: ","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-google-genai-image/src/main/java/org/springframework/ai/google/genai/image/GoogleGenAiImageModel.java","lineNumber":371,"sourceCode":"\t\t\tparts.addAll(mediaToParts(message.getMedia()));\n\t\t}\n\n\t\treturn parts;\n\t}\n\n\tprivate static List<Part> mediaToParts(Collection<Media> media) {\n\t\treturn media.stream().map(mediaData -> {\n\t\t\tfinal Object data = mediaData.getData();\n\t\t\tfinal String mimeType = mediaData.getMimeType().toString();\n\n\t\t\tif (data instanceof byte[]) {\n\t\t\t\treturn Part.fromBytes((byte[]) data, mimeType);\n\t\t\t}\n\t\t\tif (data instanceof URI || data instanceof String) {\n\t\t\t\treturn Part.fromUri(data.toString(), mimeType);\n\t\t\t}\n\n\t\t\tthrow new IllegalArgumentException(\"Unsupported media data type: \" + data.getClass());\n\t\t}).toList();\n\t}\n\n\tprivate static List<SafetySetting> buildSafetySettings(\n\t\t\tGoogleGenAiImageOptions.SafetyFilterLevel safetyFilterLevel) {\n\t\tfinal String threshold = safetyFilterLevel.name();\n\t\tfinal List<HarmCategory.Known> categories = List.of(HarmCategory.Known.HARM_CATEGORY_HARASSMENT,\n\t\t\t\tHarmCategory.Known.HARM_CATEGORY_HATE_SPEECH, HarmCategory.Known.HARM_CATEGORY_SEXUALLY_EXPLICIT,\n\t\t\t\tHarmCategory.Known.HARM_CATEGORY_DANGEROUS_CONTENT);\n\n\t\tfinal List<SafetySetting> safetySettings = new ArrayList<>();\n\t\tfor (HarmCategory.Known category : categories) {\n\t\t\tsafetySettings.add(SafetySetting.builder().category(category).threshold(threshold).build());\n\t\t}\n\n\t\treturn safetySettings;\n\t}\n","sourceCodeStart":353,"sourceCodeEnd":389,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-google-genai-image/src/main/java/org/springframework/ai/google/genai/image/GoogleGenAiImageModel.java#L353-L389","documentation":"In the image model's mediaToParts(), Media data must be a byte[] (sent via Part.fromBytes) or a URI/String URL (sent via Part.fromUri). Any other data type cannot be converted to a Gemini Part, so an IllegalArgumentException naming the actual class is thrown.","triggerScenarios":"Constructing a Media (e.g. new Media(MimeTypeUtils.IMAGE_PNG, someObject)) where data is an InputStream, File, Path, Resource, or BufferedImage instead of byte[] or URI/String, then passing it through an ImagePrompt message.","commonSituations":"Loading images via ClassPathResource/FileInputStream and passing the Resource or InputStream directly as Media data; using Path objects from java.nio; code migrated from another AI model API that accepted Resources.","solutions":["Convert the data to byte[] first (e.g. resource.getContentAsByteArray() or Files.readAllBytes(path)) and pass that as Media data.","If the image is reachable by URL, pass a java.net.URI or its toString() so it is sent via Part.fromUri.","Wrap the conversion in a helper that normalizes InputStream/File/Resource to byte[] before building the Media."],"exampleFix":"// before\nMedia media = new Media(MimeTypeUtils.IMAGE_PNG, new ClassPathResource(\"cat.png\"));\n\n// after\nbyte[] bytes = new ClassPathResource(\"cat.png\").getContentAsByteArray();\nMedia media = new Media(MimeTypeUtils.IMAGE_PNG, bytes);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean isSupportedMediaData(Object data) {\n    return data instanceof byte[] || data instanceof URI || data instanceof String;\n}","tryCatchPattern":null,"preventionTips":["Normalize InputStream/Resource/File media to byte[] before creating a Media.","Prefer URI-based media for remote images.","Centralize Media construction in one utility method with the type guard."],"tags":["image-generation","media","type-mismatch","illegal-argument"],"backgroundTag":"type-mismatch","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}