microsoft/autogen · error · InvalidOperationException
Invalid ImageMessage, the data or url must be provided
Error message
Invalid ImageMessage, the data or url must be provided
What it means
GeminiMessageConnector.CreateImagePart builds an inline Gemini Blob from an ImageMessage. It supports raw bytes (message.Data) and downloads from a URL; if the message has neither usable data nor a resolvable URL, it throws this InvalidOperationException because no Part can be produced.
Source
Thrown at dotnet/src/AutoGen.Gemini/Middleware/GeminiMessageConnector.cs:470
FileUri = url,
MimeType = message.MimeType
}
};
}
else if (message.Data is BinaryData data)
{
return new Part
{
InlineData = new Blob
{
MimeType = message.MimeType,
Data = ByteString.CopyFrom(data.ToArray()),
}
};
}
else
{
throw new InvalidOperationException("Invalid ImageMessage, the data or url must be provided");
}
}
private bool ShouldParseAsUser(IMessage message, IAgent agent)
{
return message switch
{
TextMessage textMessage => (textMessage.Role == Role.User && textMessage.From is null)
|| (textMessage.From != agent.Name),
_ => message.From != agent.Name,
};
}
}
View on GitHub (pinned to 027ecf0a37)
Solutions
- Verify ImageMessage.Data is non-empty before sending, or set a valid Url.
- If using Url, confirm it is publicly reachable (200, image content) — the connector fetches it server-side.
- Add a guard in your message-factory: skip/throw early when both fields are empty so the failure points at the loader, not the connector.
Example fix
// before
var img = new ImageMessage(Role.User, mimeType: "image/png"); // no data, no url
// after
var img = new ImageMessage(Role.User, mimeType: "image/png", data: await File.ReadAllBytesAsync("chart.png")); Defensive patterns
Strategy: validation
Validate before calling
if (imageMessage.Data is null || imageMessage.Data.Length == 0 && string.IsNullOrEmpty(imageMessage.Url?.ToString()))
{
throw new ArgumentException("ImageMessage needs Data or Url", nameof(imageMessage));
} Type guard
static bool HasImagePayload(ImageMessage m) => (m.Data is { Length: > 0 }) || !string.IsNullOrEmpty(m.Url?.ToString()); Prevention
- Load image bytes before constructing the message
- Fail loudly in image loaders instead of emitting empty messages
- Verify URLs are fetchable when using Url mode
When it happens
Trigger: Constructing ImageMessage with both Data and Url null/empty (e.g. default constructor or factory that failed to load the image), or a Url that yields an empty/failed download so no byte array was obtained.
Common situations: Image-loading code that swallows exceptions and still creates the message; URL fetch blocked by network/404 leaving data null; refactoring that dropped the Data assignment.
Related errors
- Invalid DataUri format, expected data:[<mediatype>][;base64]
- Data cannot be empty
- Unsupported message type: {m.GetType()}
- The response should contain exactly one candidate.
- Unsupported message type: {message.GetType()}
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/39cb898e7a99062a.
Report an issue: GitHub.