microsoft/semantic-kernel · error · InvalidOperationException
Image content MimeType is empty.
Error message
Image content MimeType is empty.
What it means
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.
Source
Thrown at dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs:319
if (imageContent.Uri is not null)
{
return new GeminiPart
{
FileData = new GeminiPart.FileDataPart
{
MimeType = GetMimeTypeFromImageContent(imageContent),
FileUri = imageContent.Uri ?? throw new InvalidOperationException("Image content URI is empty.")
}
};
}
throw new InvalidOperationException("Image content does not contain any data or uri.");
}
private static string GetMimeTypeFromImageContent(ImageContent imageContent)
{
return imageContent.MimeType
?? throw new InvalidOperationException("Image content MimeType is empty.");
}
/// <summary>
/// Creates a GeminiPart with FunctionResponse containing multimodal image data (Gemini 3+ only).
/// </summary>
private static GeminiPart CreateImageFunctionResponsePart(string functionName, ImageContent imageContent)
{
if (imageContent.Data is not { IsEmpty: false })
{
throw new InvalidOperationException("ImageContent in function result must contain binary data.");
}
return new GeminiPart
{
FunctionResponse = new GeminiPart.FunctionResponsePart
{
FunctionName = functionName,
Response = new(s_imageFunctionResponseEnvelope),View on GitHub (pinned to c028a0c7dc)
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.");
Example fix
// before — no MIME type
var content = new ImageContent(await File.ReadAllBytesAsync("photo.jpg"));
// after — include MIME type
var content = new ImageContent(await File.ReadAllBytesAsync("photo.jpg"))
{
MimeType = "image/jpeg"
}; Defensive patterns
Strategy: validation
Validate before calling
static void ValidateImageMimeType(ImageContent image)
{
if (string.IsNullOrWhiteSpace(image.MimeType))
throw new ArgumentException(
"ImageContent.MimeType is required for Gemini. " +
"Set it explicitly (e.g. 'image/png', 'image/jpeg').");
} Type guard
static bool HasMimeType(ImageContent img) =>
!string.IsNullOrWhiteSpace(img.MimeType); Try / catch
try { await client.GetChatMessageContentsAsync(history, settings, ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("MimeType is empty"))
{
// Infer from file extension if available and retry
var mimeType = InferMimeType(imageUri);
imageContent.MimeType = mimeType;
await client.GetChatMessageContentsAsync(history, settings, ct);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Image content does not contain any data or uri.
- ImageContent in function result must contain binary data.
- MaxTokens {maxTokens} is not valid, the value must be greate
- Chat history can't contain only system messages.
- Auto-invocation of tool calls may only be used with a {nameo
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/120a27cd3dc729da.
Report an issue: GitHub.