microsoft/semantic-kernel · error · InvalidOperationException
Image content does not contain any data or uri.
Error message
Image content does not contain any data or uri.
What it means
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.
Source
Thrown at dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs:313
MimeType = GetMimeTypeFromImageContent(imageContent),
InlineData = Convert.ToBase64String(imageContent.Data.Value.ToArray())
}
};
}
if (imageContent.Uri is not null)
{
return new GeminiPart
{
FileData = new GeminiPart.FileDataPart
{
MimeType = GetMimeTypeFromImageContent(imageContent),
FileUri = imageContent.Uri ?? throw new InvalidOperationException("Image content URI is empty.")
}
};
}
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.");
}
View on GitHub (pinned to c028a0c7dc)
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.
Example fix
// before — no data, no uri
var content = new ImageContent { MimeType = "image/png" };
history.AddUserMessage("Describe this", content);
// after — provide data
var bytes = await File.ReadAllBytesAsync("image.png");
var content = new ImageContent(bytes) { MimeType = "image/png" };
history.AddUserMessage("Describe this", content); Defensive patterns
Strategy: validation
Validate before calling
static void ValidateImageContent(ImageContent image)
{
bool hasData = image.Data is { IsEmpty: false };
bool hasUri = image.Uri is not null;
if (!hasData && !hasUri)
throw new ArgumentException(
"ImageContent must contain either Data (bytes) or Uri.");
} Type guard
static bool HasImagePayload(ImageContent img) =>
img.Data is { IsEmpty: false } || img.Uri is not null; Try / catch
try { await client.GetChatMessageContentsAsync(history, settings, ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("does not contain any data or uri"))
{
logger.LogError("ImageContent missing both Data and Uri.");
throw;
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Image content MimeType is empty.
- ImageContent in function result must contain binary data.
- MaxTokens {maxTokens} is not valid, the value must be greate
- Chat history can't contain only system messages.
- Auto-invocation of tool calls may only be used with a {nameo
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/2dfce4e2a19de67f.
Report an issue: GitHub.