microsoft/semantic-kernel · error · ArgumentException

AudioContent must have Data bytes.

Error message

AudioContent must have Data bytes.

What it means

Thrown by GetAudioContentItem. Unlike images, OpenAI input-audio parts are byte-only (no audio URI is accepted), so AudioContent MUST carry non-empty Data. Empty/null Data makes the part unbuildable and raises an ArgumentException.

Source

Thrown at dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs:912

            return ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(data), imageContent.MimeType, detailLevel);
        }

        if (imageContent.Uri is not null)
        {
            return ChatMessageContentPart.CreateImagePart(imageContent.Uri, detailLevel);
        }

        throw new ArgumentException($"{nameof(ImageContent)} must have either Data or a Uri.");
    }

    private static ChatMessageContentPart GetAudioContentItem(AudioContent audioContent)
    {
        if (audioContent.Data is { IsEmpty: false } data)
        {
            return ChatMessageContentPart.CreateInputAudioPart(BinaryData.FromBytes(data), GetChatInputAudioFormat(audioContent.MimeType));
        }

        throw new ArgumentException($"{nameof(AudioContent)} must have Data bytes.");
    }

    private static ChatMessageContentPart GetBinaryContentItem(BinaryContent binaryContent)
    {
        if (binaryContent.Data is { IsEmpty: false } data)
        {
            return ChatMessageContentPart.CreateFilePart(BinaryData.FromBytes(data), binaryContent.MimeType, Guid.NewGuid().ToString());
        }

        throw new ArgumentException($"{nameof(BinaryContent)} must have Data bytes.");
    }

    private static ChatInputAudioFormat GetChatInputAudioFormat(string? mimeType)
    {
        if (string.IsNullOrWhiteSpace(mimeType))
        {
            return ChatInputAudioFormat.Mp3;
        }

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Populate AudioContent.Data with a non-empty byte array before adding the content to a message.
  2. Verify the source stream's Length > 0 before reading into Data.
  3. Do not send AudioContent until the bytes are actually available.

Example fix

// before
var audio = new AudioContent(null, "audio/wav");
// after
var audio = new AudioContent(await File.ReadAllBytesAsync(path), "audio/wav");
Defensive patterns

Strategy: validation

Validate before calling

static AudioContent BuildAudio(byte[]? data, string mime) => data is { Length: > 0 } ? new AudioContent(data, mime) : throw new ArgumentException("AudioContent needs non-empty Data");

Type guard

static bool AudioContentIsUsable(AudioContent a) => a.Data is { IsEmpty: false };

Try / catch

try { await client.GetChatCompletionAsync(history); }
catch (ArgumentException ex) when (ex.Message.Contains("Data bytes")) { /* load bytes and retry */ }

Prevention

When it happens

Trigger: Building an AudioContent with empty Data (e.g. an audio stream that yielded zero bytes, or only a MimeType) and including it in a chat request.

Common situations: Microphone capture produced no frames; a file read returned empty; constructing AudioContent as a placeholder before bytes are loaded.

Related errors


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