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
- Always construct BinaryData with an explicit media type: new BinaryData(bytes, "image/png").
- When persisting messages, store MimeType alongside the payload and restore both.
- 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
- Construct BinaryData with the media-type overload everywhere: new BinaryData(bytes, "image/png").
- Persist MimeType alongside payloads when serializing messages.
- Avoid BinaryData string round-trips that drop MediaType.
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
- Data cannot be empty
- Invalid DataUri format, expected data:[<mediatype>][;base64]
- MimeType is required for ImageMessage
- The from property of the message {message} is different from
- Invalid aggregate message {reason}
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/a50262ab2e1d2e86.
Report an issue: GitHub.