microsoft/semantic-kernel · error · InvalidOperationException

Audio content MimeType is empty.

Error message

Audio content MimeType is empty.

What it means

Thrown by GetMimeTypeFromAudioContent when audioContent.MimeType is null. Gemini requires an explicit MIME type for both inline data and file-URI audio parts, so the connector refuses to serialize audio without one. This fires on BOTH the inline-data and file-URI code paths because GetMimeTypeFromAudioContent is called in each branch.

Source

Thrown at dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs:386

        if (audioContent.Uri is not null)
        {
            return new GeminiPart
            {
                FileData = new GeminiPart.FileDataPart
                {
                    MimeType = GetMimeTypeFromAudioContent(audioContent),
                    FileUri = audioContent.Uri ?? throw new InvalidOperationException("Audio content URI is empty.")
                }
            };
        }

        throw new InvalidOperationException("Audio content does not contain any data or uri.");
    }

    private static string GetMimeTypeFromAudioContent(AudioContent audioContent)
    {
        return audioContent.MimeType
               ?? throw new InvalidOperationException("Audio content MimeType is empty.");
    }

    private static GeminiPart CreateGeminiPartFromBinary(BinaryContent binaryContent)
    {
        // Binary data takes precedence over URI.
        if (binaryContent.Data is { IsEmpty: false })
        {
            return new GeminiPart
            {
                InlineData = new GeminiPart.InlineDataPart
                {
                    MimeType = GetMimeTypeFromBinaryContent(binaryContent),
                    InlineData = Convert.ToBase64String(binaryContent.Data.Value.ToArray())
                }
            };
        }

        if (binaryContent.Uri is not null)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Always set MimeType (e.g. "audio/wav", "audio/mp3", "audio/aac") when constructing AudioContent.
  2. If loading from an HTTP response, use response.Content.Headers.ContentType.MediaType.
  3. Map the file extension to a mime type before constructing the content.

Example fix

// before
var audio = new AudioContent(bytes);  // MimeType null

// after
var audio = new AudioContent(bytes, mimeType: "audio/wav");
Defensive patterns

Strategy: validation

Validate before calling

static void EnsureAudioMime(AudioContent c)
{
    if (string.IsNullOrWhiteSpace(c.MimeType))
        throw new InvalidOperationException("AudioContent.MimeType is required for Gemini.");
}

Type guard

static bool HasAudioMime(AudioContent c) => !string.IsNullOrWhiteSpace(c.MimeType);

Try / catch

try { await kernel.InvokePromptAsync(prompt); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Audio content MimeType is empty."))
{ logger.LogError(ex, "Set AudioContent.MimeType before sending."); throw; }

Prevention

When it happens

Trigger: Adding an AudioContent to a Gemini chat history without setting MimeType, regardless of whether Data or Uri is populated. Also fires when constructing audio from a raw byte array without specifying the mime type.

Common situations: Inferring a file's format by extension locally but forgetting to pass the mime type to AudioContent; loading bytes without a content-type header; generic media loaders that leave MimeType null.

Related errors


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