microsoft/semantic-kernel · error · InvalidOperationException
Audio content does not contain any data or uri.
Error message
Audio content does not contain any data or uri.
What it means
Thrown by CreateGeminiPartFromAudio when an AudioContent has neither usable binary Data (null or empty) nor a Uri. The connector cannot build a Gemini part because Gemini needs either inline base64 bytes or a file URI. This is the terminal fallback after both branches are skipped.
Source
Thrown at dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs:380
MimeType = GetMimeTypeFromAudioContent(audioContent),
InlineData = Convert.ToBase64String(audioContent.Data.Value.ToArray())
}
};
}
if (audioContent.Uri is not null)
{
return new GeminiPart
{
FileData = new GeminiPart.FileDataPart
{
MimeType = GetMimeTypeFromAudioContent(audioContent),
FileUri = audioContent.Uri ?? throw new InvalidOperationException("Audio content URI is empty.")
}
};
}
throw new InvalidOperationException("Audio content does not contain any data or uri.");
}
private static string GetMimeTypeFromAudioContent(AudioContent audioContent)
{
return audioContent.MimeType
?? throw new InvalidOperationException("Audio content MimeType is empty.");
}
private static GeminiPart CreateGeminiPartFromBinary(BinaryContent binaryContent)
{
// Binary data takes precedence over URI.
if (binaryContent.Data is { IsEmpty: false })
{
return new GeminiPart
{
InlineData = new GeminiPart.InlineDataPart
{
MimeType = GetMimeTypeFromBinaryContent(binaryContent),View on GitHub (pinned to c028a0c7dc)
Solutions
- Ensure the AudioContent has either Data set (read the file bytes/stream into it) or a Uri set before adding it to ChatHistory.
- Check audioContent.Data is non-null/non-empty or audioContent.Uri is non-null before invoking the kernel.
- Verify the source file path/URL exists and is readable when loading audio content.
Example fix
// before
var audio = new AudioContent(); // no data, no uri
history.AddUserMessage(audio);
// after
var bytes = await File.ReadAllBytesAsync("clip.wav");
var audio = new AudioContent(bytes, mimeType: "audio/wav");
// or: var audio = new AudioContent(uri: "https://.../clip.wav", mimeType: "audio/wav");
history.AddUserMessage(audio); Defensive patterns
Strategy: validation
Validate before calling
static void EnsureAudioUsable(AudioContent c)
{
if ((c.Data is null || c.Data.Value.IsEmpty) && c.Uri is null)
throw new InvalidOperationException("AudioContent needs Data or Uri before sending to Gemini.");
} Type guard
static bool HasAudioPayload(AudioContent 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("Audio content does not contain any data or uri."))
{ logger.LogError(ex, "AudioContent was empty; attach data or a URI."); throw; } Prevention
- Always load audio bytes or set a URI before adding AudioContent to ChatHistory.
- Verify the source file/URL exists and is readable.
- Unit-test content construction paths to assert Data or Uri is set.
When it happens
Trigger: Constructing an AudioContent and adding it to a chat history without populating Data or Uri; e.g. new AudioContent() with defaults, or an AudioContent whose stream failed to load so Data stayed null/empty.
Common situations: Loading an audio file from a path that does not exist or fails silently; deserializing an AudioContent from JSON that omits both data and uri; a helper that creates a placeholder AudioContent before the real bytes are attached.
Related errors
- Audio 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/009efb8ac60c15c7.
Report an issue: GitHub.