microsoft/autogen · error · ArgumentException

MimeType is required for ImageMessage

Error message

MimeType is required for ImageMessage

What it means

In ImageMessage's constructor for non-data-URI urls, when no mimeType argument is supplied the code tries to infer the MIME type from the file extension (.png/.jpg/.jpeg/.gif/.bmp/.webp/.svg, case-insensitive). Any URL not ending in one of these extensions throws ArgumentException('MimeType is required for ImageMessage', paramName: mimeType). Note the switch also runs for http(s) URLs, so even remote images need either a known extension or an explicit mime type.

Source

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

            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",
                    _ when url.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) => "image/bmp",
                    _ when url.EndsWith(".webp", StringComparison.OrdinalIgnoreCase) => "image/webp",
                    _ when url.EndsWith(".svg", StringComparison.OrdinalIgnoreCase) => "image/svg+xml",
                    _ => throw new ArgumentException("MimeType is required for ImageMessage", nameof(mimeType))
                };
            }

            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));
        }

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Pass the mimeType explicitly when the URL lacks a recognized extension.
  2. Normalize URLs before construction so they end with a supported extension (or extract the extension from a query parameter).
  3. For unsupported formats, convert the image to png/jpg first, then use the inferred path.

Example fix

// before
var msg = new ImageMessage(Role.User, "https://cdn.example.com/avatar/123");

// after
var msg = new ImageMessage(Role.User, "https://cdn.example.com/avatar/123", mimeType: "image/png");
Defensive patterns

Strategy: validation

Validate before calling

static readonly string[] KnownExtensions = { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".svg" };

bool NeedsExplicitMime(string url) =>
    !KnownExtensions.Any(ext => url.EndsWith(ext, StringComparison.OrdinalIgnoreCase));

Try / catch

try { var msg = new ImageMessage(Role.User, url, mimeType: mimeOrNull); }
catch (ArgumentException ex) when (ex.Message.Contains("MimeType is required"))
{
    var msg = new ImageMessage(Role.User, url, mimeType: "application/octet-stream"); // or probe the bytes
}

Prevention

When it happens

Trigger: new ImageMessage(Role.User, "https://cdn.example.com/get-image?id=123") with no mimeType; local paths like "photo" or "img.orig"; URLs with query strings after the extension are fine (EndsWith check), but extension-less SAS/CDN URLs are not.

Common situations: Dynamic image endpoints without file extensions; pre-signed blob/S3 URLs; user-supplied paths where the extension was stripped; .svgz or exotic formats not in the supported list.

Related errors


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