microsoft/semantic-kernel · error · NotSupportedException

ImageContent cannot be converted to ResponseContentPart. Onl

Error message

ImageContent cannot be converted to ResponseContentPart. Only ImageContent with a uri or binary data is supported.

What it means

The ImageContent overload of ToResponseContentPart needs either a Uri or a Data payload to build an input-image part. If both are null, there is nothing to send and it throws NotSupportedException.

Source

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

    internal static ResponseContentPart ToResponseContentPart(this TextContent content)
    {
        return ResponseContentPart.CreateInputTextPart(content.Text);
    }

    internal static ResponseContentPart ToResponseContentPart(this ImageContent content)
    {
        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. Set ImageContent.Uri to a publicly reachable image URL, or set ImageContent.Data with a MimeType.
  2. Validate the image source resolved before constructing ImageContent.
  3. Skip the image item entirely when no source is available rather than passing an empty ImageContent.

Example fix

// before
var img = new ImageContent();
message.Items.Add(img);
// after
var img = new ImageContent { Uri = new Uri(imageUrl) };
message.Items.Add(img);
Defensive patterns

Strategy: validation

Validate before calling

if (imageContent.Uri is null && imageContent.Data is null)
    throw new ArgumentException("ImageContent must have a Uri or Data.");

Type guard

static bool HasImageSource(ImageContent c) => c.Uri is not null || c.Data is not null;

Try / catch

try { var part = imageContent.ToResponseContentPart(); }
catch (NotSupportedException ex) when (ex.Message.Contains("ImageContent cannot be converted")) {
    // skip or supply a placeholder uri
}

Prevention

When it happens

Trigger: Constructing ImageContent and adding it to a message without setting Uri or Data.

Common situations: Building ImageContent from metadata that omitted the source; loading an image that failed and left both fields null.

Related errors


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