microsoft/semantic-kernel · warning · InvalidOperationException
Image content URI is empty.
Error message
Image content URI is empty.
What it means
Declared in GeminiRequest.CreateGeminiPartFromImage inside the 'if (imageContent.Uri is not null)' branch as 'imageContent.Uri ?? throw new InvalidOperationException(...)'. This throw is DEAD CODE: the enclosing if-statement already guarantees imageContent.Uri is not null, so the null-coalescing operator's right-hand side can never execute. The throw cannot fire under any circumstance. If it theoretically could, it would mean the Uri property has race-condition semantics (returned non-null for the 'is' check but null for the ?? operator).
Source
Thrown at dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs:308
{
return new GeminiPart
{
InlineData = new GeminiPart.InlineDataPart
{
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)
{View on GitHub (pinned to c028a0c7dc)
Solutions
- No action needed — this throw cannot fire. If you believe you are hitting this error, check the stack trace to confirm the actual source.
- The real error users hit when an ImageContent lacks data AND uri is error 337 ('Image content does not contain any data or uri.').
Example fix
null
Defensive patterns
Strategy: validation
Validate before calling
// This error is unreachable (dead code). No validation is needed.
// The real concern is error 337 (no data and no uri).
// Validate ImageContent properly:
if (image.Data is null or { IsEmpty: true } && image.Uri is null)
throw new ArgumentException("ImageContent must have Data or Uri."); Type guard
null
Try / catch
null
Prevention
- This exception is dead code — it cannot fire because the enclosing if-statement already checks Uri is not null.
- If you see an image-related error, check for error 337 or 338 instead.
When it happens
Trigger: This exception is unreachable in practice. The code path requires imageContent.Uri to be non-null (checked by the if-condition) and then uses '?? throw' on the same property, which is logically contradictory. No caller input can trigger it without the ImageContent.Uri property having side-effectful or non-deterministic behavior between calls.
Common situations: This is a code-level defensive check that is logically impossible to trigger given the surrounding control flow. It may represent a copy-paste pattern or defensive coding habit. Developers reading this should not expect to ever see this exception at runtime.
Related errors
- Image content does not contain any data or uri.
- Image content MimeType is empty.
- ImageContent in function result must contain binary data.
- MaxTokens {maxTokens} is not valid, the value must be greate
- Unexpected response from model
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/6d497a08e1270699.
Report an issue: GitHub.