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

  1. Always set MimeType when constructing BinaryContent (e.g. "application/pdf", "image/png").
  2. Derive it from the source file extension or HTTP Content-Type header.
  3. 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

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


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/aedf0883b9a6a23d. Report an issue: GitHub.