{"record":{"id":"72ceb6267847f419","repo":"spring-projects/spring-ai","slug":"unsupported-media-data-type-72ceb6","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/src/main/java/org/springframework/ai/google/genai/GoogleGenAiChatModel.java","lineNumber":342,"sourceCode":"\t}\n\n\tprivate static List<Part> mediaToParts(Collection<Media> media) {\n\t\tList<Part> parts = new ArrayList<>();\n\n\t\tList<Part> mediaParts = media.stream().map(mediaData -> {\n\t\t\tObject data = mediaData.getData();\n\t\t\tString 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\telse if (data instanceof URI || data instanceof String) {\n\t\t\t\t// Handle URI or String URLs\n\t\t\t\tString uri = data.toString();\n\t\t\t\treturn Part.fromUri(uri, mimeType);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthrow new IllegalArgumentException(\"Unsupported media data type: \" + data.getClass());\n\t\t\t}\n\t\t}).toList();\n\n\t\tif (!CollectionUtils.isEmpty(mediaParts)) {\n\t\t\tparts.addAll(mediaParts);\n\t\t}\n\n\t\treturn parts;\n\t}\n\n\t// Helper methods for JSON/Map conversion\n\tprivate Map<String, Object> parseJsonToMap(String json) {\n\t\ttry {\n\t\t\t// First, try to parse as an array\n\t\t\tObject parsed = this.jsonMapper.readValue(json, Object.class);\n\t\t\tif (parsed instanceof List) {\n\t\t\t\t// It's an array, wrap it in a map with \"result\" key\n\t\t\t\tMap<String, Object> wrapper = new HashMap<>();","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/GoogleGenAiChatModel.java#L324-L360","documentation":"In the chat model's mediaToParts(), media data must be byte[] (via fromBytes) or URI/String (via fromUri). Other data types cannot be represented as a Gemini Part, so an IllegalArgumentException including the data's class is thrown. This is the chat-model counterpart of the image model's equivalent check.","triggerScenarios":"Attaching a Media to a UserMessage where data is an InputStream, Resource, File, Path, or other non-byte[]/non-URI object, then sending the prompt through GoogleGenAiChatModel.call()/stream().","commonSituations":"Multimodal prompts built from ClassPathResource/UrlResource objects passed directly as Media data; passing java.io.File or Path from a file picker; code ported from an OpenAI adapter that accepted different media types.","solutions":["Convert resource/file input to byte[] (Resource.getContentAsByteArray(), Files.readAllBytes) before constructing the Media.","For URL-based media, pass a java.net.URI or String URL so it maps to Part.fromUri.","Centralize media creation in a helper that only emits byte[] or URI data and fails early with a clear message."],"exampleFix":"// before\nMedia media = new Media(MimeTypeUtils.IMAGE_JPEG, resource); // Resource\n\n// after\nMedia media = new Media(MimeTypeUtils.IMAGE_JPEG, resource.getContentAsByteArray());","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean isSupportedChatMediaData(Object data) {\n    return data instanceof byte[] || data instanceof URI || data instanceof String;\n}","tryCatchPattern":null,"preventionTips":["Convert Resource/File/InputStream media to byte[] when building multimodal UserMessages.","Use URI or String URLs for remote media.","Share one media-normalization utility across chat and image models."],"tags":["chat","multimodal","media","type-mismatch"],"backgroundTag":"type-mismatch","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"}