microsoft/semantic-kernel · error · InvalidOperationException
Binary content MimeType is empty.
Error message
Binary content MimeType is empty.
What it means
Thrown by GetMimeTypeFromBinaryContent when binaryContent.MimeType is null. Gemini requires an explicit MIME type for both inline and file-URI binary parts. Fires on both code paths because GetMimeTypeFromBinaryContent is invoked in each branch of CreateGeminiPartFromBinary.
Source
Thrown at dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs:422
if (binaryContent.Uri is not null)
{
return new GeminiPart
{
FileData = new GeminiPart.FileDataPart
{
MimeType = GetMimeTypeFromBinaryContent(binaryContent),
FileUri = binaryContent.Uri ?? throw new InvalidOperationException("Binary content URI is empty.")
}
};
}
throw new InvalidOperationException("Binary content does not contain any data or uri.");
}
private static string GetMimeTypeFromBinaryContent(BinaryContent binaryContent)
{
return binaryContent.MimeType
?? throw new InvalidOperationException("Binary content MimeType is empty.");
}
private static void AddConfiguration(GeminiPromptExecutionSettings executionSettings, GeminiRequest request)
{
request.Configuration = new ConfigurationElement
{
Temperature = executionSettings.Temperature,
TopP = executionSettings.TopP,
TopK = executionSettings.TopK,
MaxOutputTokens = executionSettings.MaxTokens,
StopSequences = executionSettings.StopSequences,
CandidateCount = executionSettings.CandidateCount,
AudioTimestamp = executionSettings.AudioTimestamp,
ResponseMimeType = executionSettings.ResponseMimeType,
ResponseSchema = GetResponseSchemaConfig(executionSettings.ResponseSchema),
};
}
View on GitHub (pinned to c028a0c7dc)
Solutions
- Always set MimeType when constructing BinaryContent (e.g. "application/pdf", "image/png").
- Derive it from the source file extension or HTTP Content-Type header.
- Validate MimeType is non-null before adding the content to ChatHistory.
Example fix
// before var bin = new BinaryContent(bytes); // MimeType null // after var bin = new BinaryContent(bytes, mimeType: "application/pdf");
Defensive patterns
Strategy: validation
Validate before calling
static void EnsureBinaryMime(BinaryContent c)
{
if (string.IsNullOrWhiteSpace(c.MimeType))
throw new InvalidOperationException("BinaryContent.MimeType is required for Gemini.");
} Type guard
static bool HasBinaryMime(BinaryContent c) => !string.IsNullOrWhiteSpace(c.MimeType);
Try / catch
try { await kernel.InvokePromptAsync(prompt); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Binary content MimeType is empty."))
{ logger.LogError(ex, "Set BinaryContent.MimeType before sending."); throw; } Prevention
- Always set a mime type when constructing BinaryContent.
- Infer mime from file extension or HTTP Content-Type.
- Centralize construction in a helper enforcing non-null mime.
When it happens
Trigger: Constructing a BinaryContent with Data or a Uri but without setting MimeType, then passing it to a Gemini chat request.
Common situations: Passing raw bytes without inferring the format; HTTP downloads where the content-type header was dropped; generic byte loaders that leave MimeType unset.
Related errors
- Image content MimeType is empty.
- Audio content MimeType is empty.
- Binary content does not contain any data or uri.
- MaxTokens {maxTokens} is not valid, the value must be greate
- Chat history can't contain only system messages.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/aedf0883b9a6a23d.
Report an issue: GitHub.