{"record":{"id":"5697da3be6c0a0b0","repo":"microsoft/autogen","slug":"invalid-datauri-format-expected-data-mediatype","errorCode":null,"errorMessage":"Invalid DataUri format, expected data:[<mediatype>][;base64],<data>","messagePattern":"Invalid DataUri format, expected data:\\[<mediatype>\\]\\[;base64\\],<data>","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.Core/Message/ImageMessage.cs","lineNumber":31,"sourceCode":"    /// <summary>\n    /// Create an ImageMessage from a url.\n    /// The url can be a regular url or a data uri.\n    /// If the url is a data uri, the scheme must be \"data\" and the format must be data:[<mediatype>][;base64],<data>\n    /// </summary>\n    public ImageMessage(Role role, string url, string? from = null, string? mimeType = null)\n    {\n        this.Role = role;\n        this.From = from;\n\n        // url might be a data uri or a regular url\n        if (url.StartsWith(\"data:\", StringComparison.OrdinalIgnoreCase))\n        {\n            // the url must be in the format of data:[<mediatype>][;base64],<data>\n            var match = s_DataUriRegex.Match(url);\n\n            if (!match.Success)\n            {\n                throw new ArgumentException(\"Invalid DataUri format, expected data:[<mediatype>][;base64],<data>\", nameof(url));\n            }\n\n            this.Data = new BinaryData(Convert.FromBase64String(match.Groups[\"data\"].Value), match.Groups[\"mediatype\"].Value);\n\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\",","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.Core/Message/ImageMessage.cs#L13-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar msg = new ImageMessage(Role.User, $\"data:image/png {base64}\"); // missing ';base64,'\n\n// after\nvar msg = new ImageMessage(Role.User, $\"data:image/png;base64,{base64}\");\n// or better: bypass URI formatting\nvar msg = new ImageMessage(Role.User, new BinaryData(bytes, \"image/png\"));","handlingStrategy":"validation","validationCode":"using System.Text.RegularExpressions;\nstatic readonly Regex DataUri = new(@\"^data:(?<mediatype>[^;,]+)?(?:;base64)?,(?<data>[A-Za-z0-9+/=]+)$\", RegexOptions.Compiled);\n\nbool IsValidDataUri(string url) => !url.StartsWith(\"data:\", StringComparison.OrdinalIgnoreCase) || DataUri.IsMatch(url);","typeGuard":"static bool IsWellFormedDataUri(string url) => url.StartsWith(\"data:\", StringComparison.OrdinalIgnoreCase)\n    && url.Contains(',')\n    && url.Contains(\";base64\");","tryCatchPattern":"try { var msg = new ImageMessage(Role.User, url); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"DataUri\"))\n{\n    // fall back to fetching/downloading the image and using the BinaryData constructor\n}","preventionTips":["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."],"tags":["autogen","dotnet","message","image-message","data-uri","validation","argumentexception"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}