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

  1. Verify ImageMessage.Data is non-empty before sending, or set a valid Url.
  2. If using Url, confirm it is publicly reachable (200, image content) — the connector fetches it server-side.
  3. 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

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


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/39cb898e7a99062a. Report an issue: GitHub.