microsoft/autogen · error · ArgumentException
Invalid DataUri format, expected data:[<mediatype>][;base64]
Error message
Invalid DataUri format, expected data:[<mediatype>][;base64],<data>
What it means
ImageMessage's string-url constructor detects data URIs by the 'data:' prefix and validates them against s_DataUriRegex for the shape data:[<mediatype>][;base64],<data>. A prefix match that fails the regex throws ArgumentException('Invalid DataUri format...', paramName: url). The parser then base64-decodes the data group, so malformed base64 will subsequently throw FormatException from Convert.FromBase64String.
Source
Thrown at dotnet/src/AutoGen.Core/Message/ImageMessage.cs:31
/// <summary>
/// Create an ImageMessage from a url.
/// The url can be a regular url or a data uri.
/// If the url is a data uri, the scheme must be "data" and the format must be data:[<mediatype>][;base64],<data>
/// </summary>
public ImageMessage(Role role, string url, string? from = null, string? mimeType = null)
{
this.Role = role;
this.From = from;
// url might be a data uri or a regular url
if (url.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{
// the url must be in the format of data:[<mediatype>][;base64],<data>
var match = s_DataUriRegex.Match(url);
if (!match.Success)
{
throw new ArgumentException("Invalid DataUri format, expected data:[<mediatype>][;base64],<data>", nameof(url));
}
this.Data = new BinaryData(Convert.FromBase64String(match.Groups["data"].Value), match.Groups["mediatype"].Value);
this.MimeType = match.Groups["mediatype"].Value;
}
else
{
this.Url = url;
// try infer mimeType from uri extension if not provided
if (mimeType is null)
{
mimeType = url switch
{
_ when url.EndsWith(".png", StringComparison.OrdinalIgnoreCase) => "image/png",
_ when url.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) => "image/jpeg",
_ when url.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) => "image/jpeg",
_ when url.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) => "image/gif",View on GitHub (pinned to 027ecf0a37)
Solutions
- Format data URIs as data:<mediatype>;base64,<payload>, e.g. $"data:image/png;base64,{Convert.ToBase64String(bytes)}".
- Strip whitespace/newlines from the base64 payload before constructing the ImageMessage.
- Pre-validate with your own regex match on the data URI before passing it in.
- If you already have raw bytes plus a media type, use the BinaryData constructor instead, which sidesteps URI formatting entirely.
Example fix
// before
var msg = new ImageMessage(Role.User, $"data:image/png {base64}"); // missing ';base64,'
// after
var msg = new ImageMessage(Role.User, $"data:image/png;base64,{base64}");
// or better: bypass URI formatting
var msg = new ImageMessage(Role.User, new BinaryData(bytes, "image/png")); Defensive patterns
Strategy: validation
Validate before calling
using System.Text.RegularExpressions;
static readonly Regex DataUri = new(@"^data:(?<mediatype>[^;,]+)?(?:;base64)?,(?<data>[A-Za-z0-9+/=]+)$", RegexOptions.Compiled);
bool IsValidDataUri(string url) => !url.StartsWith("data:", StringComparison.OrdinalIgnoreCase) || DataUri.IsMatch(url); Type guard
static bool IsWellFormedDataUri(string url) => url.StartsWith("data:", StringComparison.OrdinalIgnoreCase)
&& url.Contains(',')
&& url.Contains(";base64"); Try / catch
try { var msg = new ImageMessage(Role.User, url); }
catch (ArgumentException ex) when (ex.Message.Contains("DataUri"))
{
// fall back to fetching/downloading the image and using the BinaryData constructor
} Prevention
- Build data URIs with a single helper: $"data:{mime};base64,{Convert.ToBase64String(bytes)}".
- Strip whitespace and newlines from base64 payloads before embedding.
- Prefer the BinaryData overload when you control the raw bytes.
When it happens
Trigger: Passing strings like 'data:image/png' (missing comma and payload), 'data:,hello' with base64 expected, 'data:image/png;base64,' (empty data), or a data URI with whitespace/line breaks in the payload; URL-escaped data URIs that were never decoded.
Common situations: Hand-assembling data URIs from byte arrays or base64 strings and forgetting the ';base64' marker or comma separator; copying data URIs from HTML/CSS that contain newline formatting; truncation of long base64 strings by config files or logs.
Related errors
- Data cannot be empty
- The from property of the message {message} is different from
- MimeType is required for ImageMessage
- MediaType is needed for DataUri Images
- Invalid aggregate message {reason}
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/5697da3be6c0a0b0.
Report an issue: GitHub.