{"record":{"id":"c380f206c8427afe","repo":"spring-projects/spring-ai","slug":"unable-to-detect-the-mime-type-of-s-please-pro","errorCode":null,"errorMessage":"Unable to detect the MIME type of '%s'. Please provide it explicitly.","messagePattern":"Unable to detect the MIME type of '(.+?)'\\. Please provide it explicitly\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/MimeTypeDetector.java","lineNumber":83,"sourceCode":"\n\tpublic static MimeType getMimeType(URI uri) {\n\t\treturn getMimeType(uri.toString());\n\t}\n\n\tpublic static MimeType getMimeType(File file) {\n\t\treturn getMimeType(file.getAbsolutePath());\n\t}\n\n\tpublic static MimeType getMimeType(Path path) {\n\t\treturn getMimeType(path.toUri());\n\t}\n\n\tpublic static MimeType getMimeType(Resource resource) {\n\t\ttry {\n\t\t\treturn getMimeType(resource.getURI());\n\t\t}\n\t\tcatch (IOException e) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\tString.format(\"Unable to detect the MIME type of '%s'. Please provide it explicitly.\",\n\t\t\t\t\t\t\tresource.getFilename()),\n\t\t\t\t\te);\n\t\t}\n\t}\n\n\tpublic static MimeType getMimeType(String path) {\n\n\t\tint dotIndex = path.lastIndexOf('.');\n\n\t\tif (dotIndex != -1 && dotIndex < path.length() - 1) {\n\t\t\tString extension = path.substring(dotIndex + 1);\n\t\t\tMimeType customMimeType = GEMINI_MIME_TYPES.get(extension);\n\t\t\tif (customMimeType != null) {\n\t\t\t\treturn customMimeType;\n\t\t\t}\n\t\t}\n","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/MimeTypeDetector.java#L65-L101","documentation":"MimeTypeDetector.getMimeType(Resource) resolves the resource URI then looks up its MIME type; if resolving the URI throws IOException, it throws IllegalArgumentException saying the MIME type could not be detected and must be provided explicitly. The detector cannot even read the resource location to guess a type.","triggerScenarios":"Passing a Resource whose getURI() fails — e.g. a ClassPathResource that doesn't exist, an unreadable file, a broken URL resource — to Gemini media parts via Media or media APIs.","commonSituations":"Typos in classpath paths (missing leading slash conventions); files deleted or not on the classpath at runtime; resources inside nested jars; remote URLs with connection problems.","solutions":["Explicitly set the MimeType on the Media/MediaPart instead of relying on detection.","Verify the resource exists (resource.exists()) and its path/URL is correct before passing it.","Check that files referenced on the classpath are actually packaged in the jar.","Inspect the cause IOException for the underlying resolution failure."],"exampleFix":"// before\nnew Media(null, resource); // detection fails for unresolvable resource\n// after\nnew Media(MimeTypeUtils.IMAGE_PNG, resource);","handlingStrategy":"validation","validationCode":"if (!resource.exists()) throw new IllegalStateException(\"resource missing: \" + resource);\nURI uri = resource.getURI(); // fail fast if unresolvable","typeGuard":null,"tryCatchPattern":"try { MimeType mt = MimeTypeDetector.getMimeType(resource); } catch (IllegalArgumentException e) { useExplicitMimeType(); }","preventionTips":["Always set MimeType explicitly for media parts","Verify resource.exists() before constructing Media","Avoid extensionless/temp files for uploads"],"tags":["mime-type","resource","multimodal"],"backgroundTag":"file-not-found","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"}