iOfficeAI/OfficeCLI · error · ArgumentException

Invalid data URI: missing comma separator

Error message

Invalid data URI: missing comma separator

What it means

Thrown by ImageSource.ResolveDataUri when a data: URI string has no comma character anywhere. The data URI format is 'data:[<mediatype>][;base64],<data>' — the comma separates the metadata header from the payload. A missing comma means the URI is malformed at the syntax level and cannot be parsed regardless of encoding.

Source

Thrown at src/officecli/Core/ImageSource.cs:122

        return false;
    }

    private static string ContentTypeName(PartTypeInfo type)
    {
        if (type == ImagePartType.Png) return "PNG";
        if (type == ImagePartType.Jpeg) return "JPEG";
        if (type == ImagePartType.Gif) return "GIF";
        if (type == ImagePartType.Bmp) return "BMP";
        if (type == ImagePartType.Tiff) return "TIFF";
        return type.ContentType ?? "unknown";
    }

    private static (Stream, PartTypeInfo) ResolveDataUri(string dataUri)
    {
        // Format: data:[<mediatype>][;base64],<data>
        var commaIdx = dataUri.IndexOf(',');
        if (commaIdx < 0)
            throw new ArgumentException("Invalid data URI: missing comma separator");

        var header = dataUri[..commaIdx]; // e.g. "data:image/png;base64"
        var data = dataUri[(commaIdx + 1)..];

        if (!header.Contains("base64", StringComparison.OrdinalIgnoreCase))
            throw new ArgumentException("Only base64-encoded data URIs are supported");

        // Extract MIME type
        var mimeStart = header.IndexOf(':') + 1;
        var mimeEnd = header.IndexOf(';');
        var mime = mimeEnd > mimeStart ? header[mimeStart..mimeEnd] : header[mimeStart..];

        var contentType = MimeToContentType(mime);
        var bytes = Convert.FromBase64String(data);
        // An empty payload decodes "successfully" to zero bytes and would
        // flow all the way into a 0-byte media part — schema-valid, but the
        // picture renders broken in Word/PowerPoint. Reject up front, like
        // the extpart carrier does for empty data.

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Ensure the data URI follows the format 'data:<mediatype>;base64,<base64payload>' with a comma before the encoded data.
  2. Use a standard library to construct data URIs rather than string concatenation.
  3. Validate the data URI string has a comma before passing it to ImageSource.Resolve.

Example fix

// before — missing comma
add image src='data:image/png;base64' path='/body'

// after — complete data URI with comma and payload
add image src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...' path='/body'
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check data URI has a comma
if (source.StartsWith("data:", StringComparison.OrdinalIgnoreCase) && !source.Contains(','))
{
    Console.Error.WriteLine("Invalid data URI: missing comma separator.");
    return;
}
var (stream, contentType) = ImageSource.Resolve(source);

Try / catch

try
{
    var (stream, contentType) = ImageSource.Resolve(source);
}
catch (ArgumentException ex) when (ex.Message.Contains("missing comma separator"))
{
    // Reconstruct the data URI with the proper format
}

Prevention

When it happens

Trigger: Passing a data URI like 'data:image/png;base64' (missing the comma and payload) or 'data:image/png' or any string starting with 'data:' that has no comma. The check is dataUri.IndexOf(',') < 0, so even 'data:' with no further content triggers it.

Common situations: A data URI that was truncated during copy-paste or serialization. A template string that was not fully interpolated (the payload variable was empty). A malformed data URI generated by a library bug that omits the comma separator. An agent constructing a data URI from parts but forgetting the comma.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/f7365d83365d3b6c. Report an issue: GitHub.