{"record":{"id":"120a27cd3dc729da","repo":"microsoft/semantic-kernel","slug":"image-content-mimetype-is-empty","errorCode":null,"errorMessage":"Image content MimeType is empty.","messagePattern":"Image content MimeType is empty\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs","lineNumber":319,"sourceCode":"        if (imageContent.Uri is not null)\n        {\n            return new GeminiPart\n            {\n                FileData = new GeminiPart.FileDataPart\n                {\n                    MimeType = GetMimeTypeFromImageContent(imageContent),\n                    FileUri = imageContent.Uri ?? throw new InvalidOperationException(\"Image content URI is empty.\")\n                }\n            };\n        }\n\n        throw new InvalidOperationException(\"Image content does not contain any data or uri.\");\n    }\n\n    private static string GetMimeTypeFromImageContent(ImageContent imageContent)\n    {\n        return imageContent.MimeType\n               ?? throw new InvalidOperationException(\"Image content MimeType is empty.\");\n    }\n\n    /// <summary>\n    /// Creates a GeminiPart with FunctionResponse containing multimodal image data (Gemini 3+ only).\n    /// </summary>\n    private static GeminiPart CreateImageFunctionResponsePart(string functionName, ImageContent imageContent)\n    {\n        if (imageContent.Data is not { IsEmpty: false })\n        {\n            throw new InvalidOperationException(\"ImageContent in function result must contain binary data.\");\n        }\n\n        return new GeminiPart\n        {\n            FunctionResponse = new GeminiPart.FunctionResponsePart\n            {\n                FunctionName = functionName,\n                Response = new(s_imageFunctionResponseEnvelope),","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs#L301-L337","documentation":"Thrown by GeminiRequest.GetMimeTypeFromImageContent when ImageContent.MimeType is null. The Gemini API requires a MIME type for both inline data (InlineDataPart.MimeType) and file data (FileDataPart.MimeType) parts. Without a MIME type, the model cannot determine the media format of the image.","triggerScenarios":"Constructing an ImageContent with data or a URI but without setting the MimeType property. The method is called from CreateGeminiPartFromImage in both the inline-data path and the file-data path, so it fires whenever either path needs a MIME type and none is set.","commonSituations":"Loading raw bytes from a source that does not expose content type (e.g. a byte array from memory). Using new ImageContent(bytes) without the mimeType parameter. Deserializing an ImageContent from a message where the content-type header was stripped. Passing a URI from a CDN where the extension is ambiguous.","solutions":["Always set MimeType when constructing ImageContent: new ImageContent(data) { MimeType = \"image/png\" } or new ImageContent(uri) { MimeType = \"image/jpeg\" }.","Infer the MIME type from the file extension if not explicitly available: var mime = Path.GetExtension(path).ToLowerInvariant() switch { \".png\" => \"image/png\", \".jpg\" or \".jpeg\" => \"image/jpeg\", _ => \"application/octet-stream\" };","Validate before the call: if (string.IsNullOrEmpty(image.MimeType)) throw new InvalidOperationException(\"MimeType required for Gemini.\");"],"exampleFix":"// before — no MIME type\nvar content = new ImageContent(await File.ReadAllBytesAsync(\"photo.jpg\"));\n\n// after — include MIME type\nvar content = new ImageContent(await File.ReadAllBytesAsync(\"photo.jpg\"))\n{\n    MimeType = \"image/jpeg\"\n};","handlingStrategy":"validation","validationCode":"static void ValidateImageMimeType(ImageContent image)\n{\n    if (string.IsNullOrWhiteSpace(image.MimeType))\n        throw new ArgumentException(\n            \"ImageContent.MimeType is required for Gemini. \" +\n            \"Set it explicitly (e.g. 'image/png', 'image/jpeg').\");\n}","typeGuard":"static bool HasMimeType(ImageContent img) =>\n    !string.IsNullOrWhiteSpace(img.MimeType);","tryCatchPattern":"try { await client.GetChatMessageContentsAsync(history, settings, ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"MimeType is empty\"))\n{\n    // Infer from file extension if available and retry\n    var mimeType = InferMimeType(imageUri);\n    imageContent.MimeType = mimeType;\n    await client.GetChatMessageContentsAsync(history, settings, ct);\n}","preventionTips":["Always set MimeType when constructing ImageContent, especially from raw bytes.","Infer MIME type from file extension or content-type headers when the source does not provide it.","Common Gemini image MIME types: image/png, image/jpeg, image/webp, image/heic, image/heif."],"tags":["google","gemini","image-content","mime-type","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}