microsoft/semantic-kernel · error · ArgumentException

BinaryContent must have Data bytes.

Error message

BinaryContent must have Data bytes.

What it means

Thrown by GetBinaryContentItem. BinaryContent is sent as a file part and OpenAI requires the actual bytes; empty/null Data cannot form a file, so an ArgumentException is raised. This guards generic binary attachments (e.g. PDF/file uploads) before they reach the wire.

Source

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

    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;
        }

        return mimeType.ToUpperInvariant() switch
        {
            "AUDIO/WAV" => ChatInputAudioFormat.Wav,
            "AUDIO/MP3" => ChatInputAudioFormat.Mp3,
            _ => throw new NotSupportedException($"Unsupported audio format '{mimeType}'. Supported formats are 'audio/wav' and 'audio/mp3'.")
        };
    }

    private static ChatImageDetailLevel? GetChatImageDetailLevel(ImageContent imageContent)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Ensure BinaryContent.Data is a non-empty byte buffer before use.
  2. Check the file size > 0 before reading it into BinaryContent.
  3. Delay adding the content until the bytes are loaded.

Example fix

// before
var file = new BinaryContent(null, "application/pdf");
// after
var file = new BinaryContent(await File.ReadAllBytesAsync(path), "application/pdf");
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

static bool BinaryContentIsUsable(BinaryContent b) => b.Data is { IsEmpty: false };

Try / catch

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

Prevention

When it happens

Trigger: Adding a BinaryContent whose Data is empty or null to a chat message, for example a file that failed to load or a placeholder BinaryContent with only a MimeType.

Common situations: Uploading a file that was deleted/not yet written; zero-byte attachment; constructing BinaryContent from a stream left at EOF.

Related errors


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