microsoft/semantic-kernel · error · NotSupportedException

AudioContent cannot be converted to ResponseContentPart. Onl

Error message

AudioContent cannot be converted to ResponseContentPart. Only AudioContent with binary data is supported.

What it means

The BinaryContent overload of ToResponseContentPart requires content.Data to build an input-file part; if Data is null it throws. Note: the message text says 'AudioContent' but the method handles BinaryContent (a copy-paste mismatch), so this covers audio/binary content without a data payload.

Source

Thrown at dotnet/src/Agents/OpenAI/Extensions/KernelContentExtensions.cs:50

        if (content.Uri is not null)
        {
            return ResponseContentPart.CreateInputImagePart(content.Uri);
        }

        if (content.Data is not null)
        {
            var dataUri = new Uri($"data:{content.MimeType};base64,{Convert.ToBase64String(content.Data.Value.ToArray())}");
            return ResponseContentPart.CreateInputImagePart(dataUri);
        }

        throw new NotSupportedException("ImageContent cannot be converted to ResponseContentPart. Only ImageContent with a uri or binary data is supported.");
    }

    internal static ResponseContentPart ToResponseContentPart(this BinaryContent content)
    {
        return content.Data is not null
            ? ResponseContentPart.CreateInputFilePart(new BinaryData(content.Data), content.MimeType, Guid.NewGuid().ToString())
            : throw new NotSupportedException("AudioContent cannot be converted to ResponseContentPart. Only AudioContent with binary data is supported.");
    }

    internal static ResponseContentPart ToResponseContentPart(this FileReferenceContent content)
    {
        return content.FileId is not null
            ? ResponseContentPart.CreateInputFilePart(content.FileId)
            : throw new NotSupportedException("FileReferenceContent cannot be converted to ResponseContentPart. Only FileReferenceContent with a file id is supported.");
    }
}

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Populate BinaryContent.Data from a valid byte stream before adding to the message.
  2. Verify the underlying stream/file is non-empty before constructing the content.
  3. Drop the item if no binary payload is available.

Example fix

// before
var audio = new BinaryContent(mimeType: "audio/wav");
message.Items.Add(audio);
// after
var audio = BinaryContent.Create(dataBytes, "audio/wav");
message.Items.Add(audio);
Defensive patterns

Strategy: validation

Validate before calling

if (binaryContent.Data is null)
    throw new ArgumentException("BinaryContent (audio) must have Data.");

Type guard

static bool HasBinaryData(BinaryContent c) => c.Data is not null;

Try / catch

try { var part = binaryContent.ToResponseContentPart(); }
catch (NotSupportedException ex) when (ex.Message.Contains("AudioContent cannot be converted")) {
    // load the byte stream before retrying, or skip the item
}

Prevention

When it happens

Trigger: Adding a BinaryContent (used for audio and other binary) to a message without setting its Data.

Common situations: Creating audio/binary content from a stream that was empty or never read; deserialized content that lost its Data.

Related errors


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