microsoft/autogen · error · InvalidOperationException
ImageMessage must have Url or DataUri
Error message
ImageMessage must have Url or DataUri
What it means
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'.
Source
Thrown at dotnet/src/AutoGen.SemanticKernel/Middleware/SemanticKernelChatMessageContentConnector.cs:194
{
return [new ChatMessageContent(AuthorRole.User, message.Content)];
}
}
private IEnumerable<ChatMessageContent> ProcessMessageForOthers(ImageMessage message)
{
var collectionItems = new ChatMessageContentItemCollection();
if (message.Url is not null)
{
collectionItems.Add(new ImageContent(new Uri(message.Url)));
}
else if (message.BuildDataUri() is string dataUri)
{
collectionItems.Add(new ImageContent(dataUri));
}
else
{
throw new InvalidOperationException("ImageMessage must have Url or DataUri");
}
return [new ChatMessageContent(AuthorRole.User, collectionItems)];
}
private IEnumerable<ChatMessageContent> ProcessMessageForSelf(MultiModalMessage message)
{
throw new System.InvalidOperationException("MultiModalMessage is not supported in the semantic kernel if it's from self.");
}
private IEnumerable<ChatMessageContent> ProcessMessageForOthers(MultiModalMessage message)
{
var collections = new ChatMessageContentItemCollection();
foreach (var item in message.Content)
{
if (item is TextMessage textContent)
{
collections.Add(new TextContent(textContent.Content));View on GitHub (pinned to 027ecf0a37)
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.
Example fix
// before var img = new ImageMessage(Role.User, url: null, from: "user"); // nothing set // after var img = new ImageMessage(Role.User, url: "https://example.com/cat.png", from: "user");
Defensive patterns
Strategy: validation
Validate before calling
static void ValidateImageMessage(ImageMessage img)
{
if (img.Url is null && img.BuildDataUri() is null)
throw new ArgumentException("ImageMessage must carry Url or binary Data before sending to an SK agent.");
} Type guard
static bool HasUsableImageSource(ImageMessage img) =>
img.Url is not null || img.BuildDataUri() is not null; Try / catch
catch (InvalidOperationException ex) when (ex.Message == "ImageMessage must have Url or DataUri")
{
logger.LogError("Image upload incomplete: neither Url nor Data present");
throw;
} Prevention
- Validate image inputs at the API boundary where files are uploaded.
- Treat empty-string Url as missing: normalize to null before constructing ImageMessage.
When it happens
Trigger: Constructing ImageMessage with neither Url nor Data (or with data that BuildDataUri cannot render), then sending it to an SK-backed agent.
Common situations: 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.
Related errors
- Invalid DataUri format, expected data:[<mediatype>][;base64]
- Data cannot be empty
- Missing parameter type for {parameterName}
- Missing required parameter: {parameterName}
- Unsupported content type
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/bcb3f4ed2d20079b.
Report an issue: GitHub.