microsoft/semantic-kernel · error · InvalidOperationException
Binary content does not contain any data or uri.
Error message
Binary content does not contain any data or uri.
What it means
Thrown by CreateGeminiPartFromBinary when a BinaryContent has neither usable Data (null or empty) nor a Uri. Gemini needs either inline bytes or a file URI, so the connector cannot build a part. This is the terminal fallback after both branches are skipped.
Source
Thrown at dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs:416
MimeType = GetMimeTypeFromBinaryContent(binaryContent),
InlineData = Convert.ToBase64String(binaryContent.Data.Value.ToArray())
}
};
}
if (binaryContent.Uri is not null)
{
return new GeminiPart
{
FileData = new GeminiPart.FileDataPart
{
MimeType = GetMimeTypeFromBinaryContent(binaryContent),
FileUri = binaryContent.Uri ?? throw new InvalidOperationException("Binary content URI is empty.")
}
};
}
throw new InvalidOperationException("Binary content does not contain any data or uri.");
}
private static string GetMimeTypeFromBinaryContent(BinaryContent binaryContent)
{
return binaryContent.MimeType
?? throw new InvalidOperationException("Binary content MimeType is empty.");
}
private static void AddConfiguration(GeminiPromptExecutionSettings executionSettings, GeminiRequest request)
{
request.Configuration = new ConfigurationElement
{
Temperature = executionSettings.Temperature,
TopP = executionSettings.TopP,
TopK = executionSettings.TopK,
MaxOutputTokens = executionSettings.MaxTokens,
StopSequences = executionSettings.StopSequences,
CandidateCount = executionSettings.CandidateCount,View on GitHub (pinned to c028a0c7dc)
Solutions
- Populate BinaryContent with either Data (bytes/stream) or a Uri before adding to the request.
- Verify the data source (file/URL) loaded successfully before constructing the content.
- Guard: check binaryContent.Data is non-empty or binaryContent.Uri is non-null before invoking.
Example fix
// before
var bin = new BinaryContent(mimeType: "application/pdf"); // no data, no uri
// after
var bytes = await File.ReadAllBytesAsync("doc.pdf");
var bin = new BinaryContent(bytes, mimeType: "application/pdf");
// or: var bin = new BinaryContent(uri: "https://.../doc.pdf", mimeType: "application/pdf"); Defensive patterns
Strategy: validation
Validate before calling
static void EnsureBinaryUsable(BinaryContent c)
{
if ((c.Data is null || c.Data.Value.IsEmpty) && c.Uri is null)
throw new InvalidOperationException("BinaryContent needs Data or Uri before sending to Gemini.");
} Type guard
static bool HasBinaryPayload(BinaryContent c)
=> (c.Data is { IsEmpty: false }) || c.Uri is not null; Try / catch
try { await kernel.InvokePromptAsync(prompt); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Binary content does not contain any data or uri."))
{ logger.LogError(ex, "BinaryContent was empty; attach data or a URI."); throw; } Prevention
- Populate BinaryContent with Data or a Uri before adding to the request.
- Verify the data source loaded successfully.
- Guard content construction with a helper that asserts a payload is present.
When it happens
Trigger: Adding a BinaryContent to a Gemini request that was constructed without Data and without Uri; e.g. a placeholder or one whose stream load failed leaving Data null.
Common situations: Deserializing a BinaryContent from JSON missing both fields; a media loader that returns an empty content object on error; forgetting to attach bytes/uri after construction.
Related errors
- Binary content MimeType is empty.
- 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
- GeminiPart is invalid. One and only one property among Text,
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/7eefb911b861d9e7.
Report an issue: GitHub.