{"record":{"id":"a50262ab2e1d2e86","repo":"microsoft/autogen","slug":"mediatype-is-needed-for-datauri-images","errorCode":null,"errorMessage":"MediaType is needed for DataUri Images","messagePattern":"MediaType is needed for DataUri Images","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.Core/Message/ImageMessage.cs","lineNumber":75,"sourceCode":"            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        }\n\n        if (data.MediaType is null)\n        {\n            throw new ArgumentException(\"MediaType is needed for DataUri Images\", nameof(data));\n        }\n\n        this.Role = role;\n        this.From = from;\n        this.Data = data;\n        this.MimeType = data.MediaType;\n    }\n\n    public Role Role { get; }\n\n    public string? Url { get; }\n\n    public string? From { get; set; }\n\n    public BinaryData? Data { get; }\n\n    public string MimeType { get; }\n","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.Core/Message/ImageMessage.cs#L57-L93","documentation":"The BinaryData overload of ImageMessage requires the BinaryData to carry a media type because it copies data.MediaType into the message's MimeType. If data.MediaType is null it throws ArgumentException('MediaType is needed for DataUri Images', paramName: data). BinaryData created from a raw string or byte array without a media type argument has a null MediaType.","triggerScenarios":"new ImageMessage(role, new BinaryData(bytes)) or new BinaryData(someString) — none of these set MediaType, so the constructor throws; BinaryData.FromStream(stream) also leaves MediaType null.","commonSituations":"Deserializing messages from JSON where BinaryData was reconstructed without its media type; using BinaryData.ToString()/string round-trips that drop the MediaType; generic pipeline code that wraps payloads without knowing their type.","solutions":["Always construct BinaryData with an explicit media type: new BinaryData(bytes, \"image/png\").","When persisting messages, store MimeType alongside the payload and restore both.","Pre-check data.MediaType is not null before building the ImageMessage and surface a clearer error."],"exampleFix":"// before\nvar msg = new ImageMessage(Role.User, new BinaryData(bytes)); // MediaType null\n\n// after\nvar msg = new ImageMessage(Role.User, new BinaryData(bytes, \"image/png\"));","handlingStrategy":"validation","validationCode":"if (data.MediaType is null)\n    data = new BinaryData(data.ToArray(), inferredMimeType); // re-wrap with media type\nvar msg = new ImageMessage(Role.User, data);","typeGuard":"static bool HasMediaType(BinaryData data) => data.MediaType is not null;","tryCatchPattern":"try { var msg = new ImageMessage(Role.User, data); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"MediaType is needed\"))\n{\n    var msg = new ImageMessage(Role.User, new BinaryData(data.ToArray(), \"image/png\"));\n}","preventionTips":["Construct BinaryData with the media-type overload everywhere: new BinaryData(bytes, \"image/png\").","Persist MimeType alongside payloads when serializing messages.","Avoid BinaryData string round-trips that drop MediaType."],"tags":["autogen","dotnet","message","image-message","binary-data","media-type","argumentexception"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}