microsoft/semantic-kernel · error · InvalidOperationException

ImageContent in function result must contain binary data.

Error message

ImageContent in function result must contain binary data.

What it means

Thrown by GeminiRequest.CreateImageFunctionResponsePart when an ImageContent passed as a function result does not contain binary Data. The method requires 'imageContent.Data is { IsEmpty: false }' — a non-null, non-empty byte array. Unlike CreateGeminiPartFromImage, this path does NOT fall back to Uri because function responses require inline binary data (the Gemini API does not support file URIs inside FunctionResponse parts).

Source

Thrown at dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs:329

        }

        throw new InvalidOperationException("Image content does not contain any data or uri.");
    }

    private static string GetMimeTypeFromImageContent(ImageContent imageContent)
    {
        return imageContent.MimeType
               ?? throw new InvalidOperationException("Image content MimeType is empty.");
    }

    /// <summary>
    /// Creates a GeminiPart with FunctionResponse containing multimodal image data (Gemini 3+ only).
    /// </summary>
    private static GeminiPart CreateImageFunctionResponsePart(string functionName, ImageContent imageContent)
    {
        if (imageContent.Data is not { IsEmpty: false })
        {
            throw new InvalidOperationException("ImageContent in function result must contain binary data.");
        }

        return new GeminiPart
        {
            FunctionResponse = new GeminiPart.FunctionResponsePart
            {
                FunctionName = functionName,
                Response = new(s_imageFunctionResponseEnvelope),
                Parts =
                [
                    new GeminiPart.FunctionResponsePart.FunctionResponsePartContent
                    {
                        InlineData = new GeminiPart.InlineDataPart
                        {
                            MimeType = GetMimeTypeFromImageContent(imageContent),
                            InlineData = Convert.ToBase64String(imageContent.Data.Value.ToArray())
                        }
                    }

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Ensure the kernel function returns ImageContent with Data populated: read the image bytes and include them in the content.
  2. If the function has a URI, download the bytes before returning: var bytes = await httpClient.GetByteArrayAsync(uri); return new ImageContent(bytes) { MimeType = "image/png" };
  3. Validate function results before returning: if (result is ImageContent ic && ic.Data is null or { IsEmpty: true }) throw new InvalidOperationException("Function image result must contain binary data");

Example fix

// before — function returns image by URI
[KernelFunction]
public ImageContent GetChart() => new(new Uri("https://cdn.example.com/chart.png"));

// after — function returns inline binary data
[KernelFunction]
public async Task<ImageContent> GetChartAsync()
{
    var bytes = await _httpClient.GetByteArrayAsync("https://cdn.example.com/chart.png");
    return new ImageContent(bytes) { MimeType = "image/png" };
}
Defensive patterns

Strategy: validation

Validate before calling

static void ValidateImageFunctionResult(ImageContent image)
{
    if (image.Data is not { IsEmpty: false })
        throw new ArgumentException(
            "ImageContent returned from a function must contain binary Data, not just a Uri.");
}

Type guard

static bool HasBinaryData(ImageContent img) =>
    img.Data is { IsEmpty: false };

Try / catch

null

Prevention

When it happens

Trigger: Returning an ImageContent with only a Uri (no Data) from a kernel function whose result is being converted to a Gemini FunctionResponse. The CreateImageFunctionResponsePart is specifically for Gemini 3+ multimodal function results and mandates binary data.

Common situations: A kernel plugin function returns an image by URL/URI instead of by binary data. An image-loading function returns a content reference without downloading the bytes. Using the same image content builder for both the user-message path (which supports URIs) and the function-response path (which requires bytes).

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/41d1991bd510357e. Report an issue: GitHub.