{"record":{"id":"41d1991bd510357e","repo":"microsoft/semantic-kernel","slug":"imagecontent-in-function-result-must-contain-binar","errorCode":null,"errorMessage":"ImageContent in function result must contain binary data.","messagePattern":"ImageContent in function result must contain binary data\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs","lineNumber":329,"sourceCode":"        }\n\n        throw new InvalidOperationException(\"Image content does not contain any data or uri.\");\n    }\n\n    private static string GetMimeTypeFromImageContent(ImageContent imageContent)\n    {\n        return imageContent.MimeType\n               ?? throw new InvalidOperationException(\"Image content MimeType is empty.\");\n    }\n\n    /// <summary>\n    /// Creates a GeminiPart with FunctionResponse containing multimodal image data (Gemini 3+ only).\n    /// </summary>\n    private static GeminiPart CreateImageFunctionResponsePart(string functionName, ImageContent imageContent)\n    {\n        if (imageContent.Data is not { IsEmpty: false })\n        {\n            throw new InvalidOperationException(\"ImageContent in function result must contain binary data.\");\n        }\n\n        return new GeminiPart\n        {\n            FunctionResponse = new GeminiPart.FunctionResponsePart\n            {\n                FunctionName = functionName,\n                Response = new(s_imageFunctionResponseEnvelope),\n                Parts =\n                [\n                    new GeminiPart.FunctionResponsePart.FunctionResponsePartContent\n                    {\n                        InlineData = new GeminiPart.InlineDataPart\n                        {\n                            MimeType = GetMimeTypeFromImageContent(imageContent),\n                            InlineData = Convert.ToBase64String(imageContent.Data.Value.ToArray())\n                        }\n                    }","sourceCodeStart":311,"sourceCodeEnd":347,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs#L311-L347","documentation":"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).","triggerScenarios":"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.","commonSituations":"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).","solutions":["Ensure the kernel function returns ImageContent with Data populated: read the image bytes and include them in the content.","If the function has a URI, download the bytes before returning: var bytes = await httpClient.GetByteArrayAsync(uri); return new ImageContent(bytes) { MimeType = \"image/png\" };","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\");"],"exampleFix":"// before — function returns image by URI\n[KernelFunction]\npublic ImageContent GetChart() => new(new Uri(\"https://cdn.example.com/chart.png\"));\n\n// after — function returns inline binary data\n[KernelFunction]\npublic async Task<ImageContent> GetChartAsync()\n{\n    var bytes = await _httpClient.GetByteArrayAsync(\"https://cdn.example.com/chart.png\");\n    return new ImageContent(bytes) { MimeType = \"image/png\" };\n}","handlingStrategy":"validation","validationCode":"static void ValidateImageFunctionResult(ImageContent image)\n{\n    if (image.Data is not { IsEmpty: false })\n        throw new ArgumentException(\n            \"ImageContent returned from a function must contain binary Data, not just a Uri.\");\n}","typeGuard":"static bool HasBinaryData(ImageContent img) =>\n    img.Data is { IsEmpty: false };","tryCatchPattern":"null","preventionTips":["When returning images from kernel functions for Gemini, always include binary Data, not just a URI.","Download image bytes inside the function if only a URI is available.","Validate function return types in unit tests to ensure ImageContent results contain Data."],"tags":["google","gemini","image-content","function-calling","multimodal","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}