microsoft/autogen · error · ArgumentException

MediaType is needed for DataUri Images

Error message

MediaType is needed for DataUri Images

What it means

The BinaryData overload of ImageMessage requires the BinaryData to carry a media type because it copies data.MediaType into the message's MimeType. If data.MediaType is null it throws ArgumentException('MediaType is needed for DataUri Images', paramName: data). BinaryData created from a raw string or byte array without a media type argument has a null MediaType.

Source

Thrown at dotnet/src/AutoGen.Core/Message/ImageMessage.cs:75

            this.MimeType = mimeType;
        }
    }

    public ImageMessage(Role role, Uri uri, string? from = null, string? mimeType = null)
        : this(role, uri.ToString(), from, mimeType)
    {
    }

    public ImageMessage(Role role, BinaryData data, string? from = null)
    {
        if (data.IsEmpty)
        {
            throw new ArgumentException("Data cannot be empty", nameof(data));
        }

        if (data.MediaType is null)
        {
            throw new ArgumentException("MediaType is needed for DataUri Images", nameof(data));
        }

        this.Role = role;
        this.From = from;
        this.Data = data;
        this.MimeType = data.MediaType;
    }

    public Role Role { get; }

    public string? Url { get; }

    public string? From { get; set; }

    public BinaryData? Data { get; }

    public string MimeType { get; }

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Always construct BinaryData with an explicit media type: new BinaryData(bytes, "image/png").
  2. When persisting messages, store MimeType alongside the payload and restore both.
  3. Pre-check data.MediaType is not null before building the ImageMessage and surface a clearer error.

Example fix

// before
var msg = new ImageMessage(Role.User, new BinaryData(bytes)); // MediaType null

// after
var msg = new ImageMessage(Role.User, new BinaryData(bytes, "image/png"));
Defensive patterns

Strategy: validation

Validate before calling

if (data.MediaType is null)
    data = new BinaryData(data.ToArray(), inferredMimeType); // re-wrap with media type
var msg = new ImageMessage(Role.User, data);

Type guard

static bool HasMediaType(BinaryData data) => data.MediaType is not null;

Try / catch

try { var msg = new ImageMessage(Role.User, data); }
catch (ArgumentException ex) when (ex.Message.Contains("MediaType is needed"))
{
    var msg = new ImageMessage(Role.User, new BinaryData(data.ToArray(), "image/png"));
}

Prevention

When it happens

Trigger: new ImageMessage(role, new BinaryData(bytes)) or new BinaryData(someString) — none of these set MediaType, so the constructor throws; BinaryData.FromStream(stream) also leaves MediaType null.

Common situations: Deserializing messages from JSON where BinaryData was reconstructed without its media type; using BinaryData.ToString()/string round-trips that drop the MediaType; generic pipeline code that wraps payloads without knowing their type.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/a50262ab2e1d2e86. Report an issue: GitHub.