{"record":{"id":"bcb3f4ed2d20079b","repo":"microsoft/autogen","slug":"imagemessage-must-have-url-or-datauri","errorCode":null,"errorMessage":"ImageMessage must have Url or DataUri","messagePattern":"ImageMessage must have Url or DataUri","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.SemanticKernel/Middleware/SemanticKernelChatMessageContentConnector.cs","lineNumber":194,"sourceCode":"        {\n            return [new ChatMessageContent(AuthorRole.User, message.Content)];\n        }\n    }\n\n    private IEnumerable<ChatMessageContent> ProcessMessageForOthers(ImageMessage message)\n    {\n        var collectionItems = new ChatMessageContentItemCollection();\n        if (message.Url is not null)\n        {\n            collectionItems.Add(new ImageContent(new Uri(message.Url)));\n        }\n        else if (message.BuildDataUri() is string dataUri)\n        {\n            collectionItems.Add(new ImageContent(dataUri));\n        }\n        else\n        {\n            throw new InvalidOperationException(\"ImageMessage must have Url or DataUri\");\n        }\n\n        return [new ChatMessageContent(AuthorRole.User, collectionItems)];\n    }\n\n    private IEnumerable<ChatMessageContent> ProcessMessageForSelf(MultiModalMessage message)\n    {\n        throw new System.InvalidOperationException(\"MultiModalMessage is not supported in the semantic kernel if it's from self.\");\n    }\n\n    private IEnumerable<ChatMessageContent> ProcessMessageForOthers(MultiModalMessage message)\n    {\n        var collections = new ChatMessageContentItemCollection();\n        foreach (var item in message.Content)\n        {\n            if (item is TextMessage textContent)\n            {\n                collections.Add(new TextContent(textContent.Content));","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.SemanticKernel/Middleware/SemanticKernelChatMessageContentConnector.cs#L176-L212","documentation":"When converting an ImageMessage for Semantic Kernel, the connector uses message.Url if present, otherwise message.BuildDataUri(); if both are unavailable (no URL and no binary data to build a data URI from) it throws 'ImageMessage must have Url or DataUri'.","triggerScenarios":"Constructing ImageMessage with neither Url nor Data (or with data that BuildDataUri cannot render), then sending it to an SK-backed agent.","commonSituations":"Creating ImageMessage(Role, from: ...) with an empty string URL; passing a Data of zero length or a format BuildDataUri rejects; multipart upload flows where the image was never attached.","solutions":["Always set either a valid Url or BinaryData payload when creating ImageMessage.","Validate ImageMessage inputs at construction time (see defense section) so bad messages never reach the connector.","Log Url/Data presence before dispatch when images come from untrusted upstream input."],"exampleFix":"// before\nvar img = new ImageMessage(Role.User, url: null, from: \"user\"); // nothing set\n\n// after\nvar img = new ImageMessage(Role.User, url: \"https://example.com/cat.png\", from: \"user\");","handlingStrategy":"validation","validationCode":"static void ValidateImageMessage(ImageMessage img)\n{\n    if (img.Url is null && img.BuildDataUri() is null)\n        throw new ArgumentException(\"ImageMessage must carry Url or binary Data before sending to an SK agent.\");\n}","typeGuard":"static bool HasUsableImageSource(ImageMessage img) =>\n    img.Url is not null || img.BuildDataUri() is not null;","tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message == \"ImageMessage must have Url or DataUri\")\n{\n    logger.LogError(\"Image upload incomplete: neither Url nor Data present\");\n    throw;\n}","preventionTips":["Validate image inputs at the API boundary where files are uploaded.","Treat empty-string Url as missing: normalize to null before constructing ImageMessage."],"tags":["semantic-kernel","image-message","validation","autogen","csharp"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}