{"record":{"id":"2dfce4e2a19de67f","repo":"microsoft/semantic-kernel","slug":"image-content-does-not-contain-any-data-or-uri","errorCode":null,"errorMessage":"Image content does not contain any data or uri.","messagePattern":"Image content does not contain any data or uri\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs","lineNumber":313,"sourceCode":"                    MimeType = GetMimeTypeFromImageContent(imageContent),\n                    InlineData = Convert.ToBase64String(imageContent.Data.Value.ToArray())\n                }\n            };\n        }\n\n        if (imageContent.Uri is not null)\n        {\n            return new GeminiPart\n            {\n                FileData = new GeminiPart.FileDataPart\n                {\n                    MimeType = GetMimeTypeFromImageContent(imageContent),\n                    FileUri = imageContent.Uri ?? throw new InvalidOperationException(\"Image content URI is empty.\")\n                }\n            };\n        }\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","sourceCodeStart":295,"sourceCodeEnd":331,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs#L295-L331","documentation":"Thrown by GeminiRequest.CreateGeminiPartFromImage when an ImageContent has neither Data (byte array) nor Uri set. The method first checks for Data, then checks for Uri, and if both are absent, there is nothing to send to the model — no inline bytes and no file reference.","triggerScenarios":"Passing an ImageContent instance where both the Data property (ReadOnlyMemory<byte>?) and the Uri property (Uri?) are null/empty. The check 'imageContent.Data is { IsEmpty: false }' fails (data is null or empty), and 'imageContent.Uri is not null' also fails (uri is null).","commonSituations":"Creating an ImageContent with only a MimeType or metadata but no actual image data or URI. Loading image data asynchronously and passing the ImageContent before the load completes. Deserialization from a message where the image attachment was not properly populated.","solutions":["Ensure ImageContent is constructed with either Data or Uri: new ImageContent(dataBytes) or new ImageContent(imageUri).","Validate ImageContent before adding to chat history: if (image.Data is null or { IsEmpty: true } && image.Uri is null) throw new ArgumentException(\"Image must have data or URI\").","If loading images asynchronously, await the load before constructing the content."],"exampleFix":"// before — no data, no uri\nvar content = new ImageContent { MimeType = \"image/png\" };\nhistory.AddUserMessage(\"Describe this\", content);\n\n// after — provide data\nvar bytes = await File.ReadAllBytesAsync(\"image.png\");\nvar content = new ImageContent(bytes) { MimeType = \"image/png\" };\nhistory.AddUserMessage(\"Describe this\", content);","handlingStrategy":"validation","validationCode":"static void ValidateImageContent(ImageContent image)\n{\n    bool hasData = image.Data is { IsEmpty: false };\n    bool hasUri = image.Uri is not null;\n    if (!hasData && !hasUri)\n        throw new ArgumentException(\n            \"ImageContent must contain either Data (bytes) or Uri.\");\n}","typeGuard":"static bool HasImagePayload(ImageContent img) =>\n    img.Data is { IsEmpty: false } || img.Uri is not null;","tryCatchPattern":"try { await client.GetChatMessageContentsAsync(history, settings, ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not contain any data or uri\"))\n{\n    logger.LogError(\"ImageContent missing both Data and Uri.\");\n    throw;\n}","preventionTips":["Always populate either Data or Uri when constructing ImageContent.","If loading images asynchronously, await completion before creating ImageContent.","Validate image content at the point of creation, not just before the API call."],"tags":["google","gemini","image-content","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}