microsoft/autogen · error · ArgumentException

Data cannot be empty

Error message

Data cannot be empty

What it means

The BinaryData overload of ImageMessage rejects empty payloads: if data.IsEmpty is true it throws ArgumentException('Data cannot be empty', paramName: data). BinaryData.IsEmpty is true for zero-length buffers, so constructing an image message with no bytes (e.g. an empty file read or an empty memory stream) fails immediately.

Source

Thrown at dotnet/src/AutoGen.Core/Message/ImageMessage.cs:70

                    _ when url.EndsWith(".svg", StringComparison.OrdinalIgnoreCase) => "image/svg+xml",
                    _ => throw new ArgumentException("MimeType is required for ImageMessage", nameof(mimeType))
                };
            }

            this.MimeType = mimeType;
        }
    }

    public ImageMessage(Role role, Uri uri, string? from = null, string? mimeType = null)
        : this(role, uri.ToString(), from, mimeType)
    {
    }

    public ImageMessage(Role role, BinaryData data, string? from = null)
    {
        if (data.IsEmpty)
        {
            throw new ArgumentException("Data cannot be empty", nameof(data));
        }

        if (data.MediaType is null)
        {
            throw new ArgumentException("MediaType is needed for DataUri Images", nameof(data));
        }

        this.Role = role;
        this.From = from;
        this.Data = data;
        this.MimeType = data.MediaType;
    }

    public Role Role { get; }

    public string? Url { get; }

    public string? From { get; set; }

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Verify the byte array length is > 0 before constructing the message.
  2. Check file/stream length and log a clear error when the source produced zero bytes.
  3. Fix the upstream producer (download, screenshot capture, file write) that yielded the empty payload.

Example fix

// before
var bytes = await File.ReadAllBytesAsync(path); // zero-byte file
var msg = new ImageMessage(Role.User, new BinaryData(bytes, "image/png")); // throws

// after
var bytes = await File.ReadAllBytesAsync(path);
if (bytes.Length == 0) throw new InvalidDataException($"{path} is empty");
var msg = new ImageMessage(Role.User, new BinaryData(bytes, "image/png"));
Defensive patterns

Strategy: validation

Validate before calling

if (bytes is null || bytes.Length == 0)
    throw new InvalidDataException("Image payload is empty; check the upstream producer");
var msg = new ImageMessage(Role.User, new BinaryData(bytes, "image/png"));

Type guard

static bool HasImagePayload(BinaryData data) => !data.IsEmpty;

Try / catch

try { var msg = new ImageMessage(Role.User, data); }
catch (ArgumentException ex) when (ex.Message.Contains("Data cannot be empty"))
{
    // re-fetch or regenerate the image, then retry
}

Prevention

When it happens

Trigger: new ImageMessage(role, new BinaryData(Array.Empty<byte>(), "image/png")); reading a zero-byte file with File.ReadAllBytesAsync and wrapping it; uploading from a stream that was already consumed or never written.

Common situations: Failed downloads or empty attachments upstream in the pipeline; race conditions where a file is read before being written; placeholder code creating BinaryData from empty arrays during development.

Related errors


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