{"record":{"id":"4291e85282edf9bb","repo":"microsoft/autogen","slug":"mimetype-is-required-for-imagemessage","errorCode":null,"errorMessage":"MimeType is required for ImageMessage","messagePattern":"MimeType is required for ImageMessage","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.Core/Message/ImageMessage.cs","lineNumber":53,"sourceCode":"\n            this.MimeType = match.Groups[\"mediatype\"].Value;\n        }\n        else\n        {\n            this.Url = url;\n            // try infer mimeType from uri extension if not provided\n            if (mimeType is null)\n            {\n                mimeType = url switch\n                {\n                    _ when url.EndsWith(\".png\", StringComparison.OrdinalIgnoreCase) => \"image/png\",\n                    _ when url.EndsWith(\".jpg\", StringComparison.OrdinalIgnoreCase) => \"image/jpeg\",\n                    _ when url.EndsWith(\".jpeg\", StringComparison.OrdinalIgnoreCase) => \"image/jpeg\",\n                    _ when url.EndsWith(\".gif\", StringComparison.OrdinalIgnoreCase) => \"image/gif\",\n                    _ when url.EndsWith(\".bmp\", StringComparison.OrdinalIgnoreCase) => \"image/bmp\",\n                    _ when url.EndsWith(\".webp\", StringComparison.OrdinalIgnoreCase) => \"image/webp\",\n                    _ when url.EndsWith(\".svg\", StringComparison.OrdinalIgnoreCase) => \"image/svg+xml\",\n                    _ => throw new ArgumentException(\"MimeType is required for ImageMessage\", nameof(mimeType))\n                };\n            }\n\n            this.MimeType = mimeType;\n        }\n    }\n\n    public ImageMessage(Role role, Uri uri, string? from = null, string? mimeType = null)\n        : this(role, uri.ToString(), from, mimeType)\n    {\n    }\n\n    public ImageMessage(Role role, BinaryData data, string? from = null)\n    {\n        if (data.IsEmpty)\n        {\n            throw new ArgumentException(\"Data cannot be empty\", nameof(data));\n        }","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.Core/Message/ImageMessage.cs#L35-L71","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the mimeType explicitly when the URL lacks a recognized extension.","Normalize URLs before construction so they end with a supported extension (or extract the extension from a query parameter).","For unsupported formats, convert the image to png/jpg first, then use the inferred path."],"exampleFix":"// before\nvar msg = new ImageMessage(Role.User, \"https://cdn.example.com/avatar/123\");\n\n// after\nvar msg = new ImageMessage(Role.User, \"https://cdn.example.com/avatar/123\", mimeType: \"image/png\");","handlingStrategy":"validation","validationCode":"static readonly string[] KnownExtensions = { \".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\", \".webp\", \".svg\" };\n\nbool NeedsExplicitMime(string url) =>\n    !KnownExtensions.Any(ext => url.EndsWith(ext, StringComparison.OrdinalIgnoreCase));","typeGuard":null,"tryCatchPattern":"try { var msg = new ImageMessage(Role.User, url, mimeType: mimeOrNull); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"MimeType is required\"))\n{\n    var msg = new ImageMessage(Role.User, url, mimeType: \"application/octet-stream\"); // or probe the bytes\n}","preventionTips":["Always pass mimeType for CDN/pre-signed/extension-less URLs.","Keep a mapping from your storage's content-type header to the mimeType argument.","Convert exotic formats to png/jpg before constructing the message."],"tags":["autogen","dotnet","message","image-message","mime-type","argumentexception"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}