{"record":{"id":"7eefb911b861d9e7","repo":"microsoft/semantic-kernel","slug":"binary-content-does-not-contain-any-data-or-uri","errorCode":null,"errorMessage":"Binary content does not contain any data or uri.","messagePattern":"Binary 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":416,"sourceCode":"                    MimeType = GetMimeTypeFromBinaryContent(binaryContent),\n                    InlineData = Convert.ToBase64String(binaryContent.Data.Value.ToArray())\n                }\n            };\n        }\n\n        if (binaryContent.Uri is not null)\n        {\n            return new GeminiPart\n            {\n                FileData = new GeminiPart.FileDataPart\n                {\n                    MimeType = GetMimeTypeFromBinaryContent(binaryContent),\n                    FileUri = binaryContent.Uri ?? throw new InvalidOperationException(\"Binary content URI is empty.\")\n                }\n            };\n        }\n\n        throw new InvalidOperationException(\"Binary content does not contain any data or uri.\");\n    }\n\n    private static string GetMimeTypeFromBinaryContent(BinaryContent binaryContent)\n    {\n        return binaryContent.MimeType\n               ?? throw new InvalidOperationException(\"Binary content MimeType is empty.\");\n    }\n\n    private static void AddConfiguration(GeminiPromptExecutionSettings executionSettings, GeminiRequest request)\n    {\n        request.Configuration = new ConfigurationElement\n        {\n            Temperature = executionSettings.Temperature,\n            TopP = executionSettings.TopP,\n            TopK = executionSettings.TopK,\n            MaxOutputTokens = executionSettings.MaxTokens,\n            StopSequences = executionSettings.StopSequences,\n            CandidateCount = executionSettings.CandidateCount,","sourceCodeStart":398,"sourceCodeEnd":434,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiRequest.cs#L398-L434","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar bin = new BinaryContent(mimeType: \"application/pdf\");  // no data, no uri\n\n// after\nvar bytes = await File.ReadAllBytesAsync(\"doc.pdf\");\nvar bin = new BinaryContent(bytes, mimeType: \"application/pdf\");\n// or: var bin = new BinaryContent(uri: \"https://.../doc.pdf\", mimeType: \"application/pdf\");","handlingStrategy":"validation","validationCode":"static void EnsureBinaryUsable(BinaryContent c)\n{\n    if ((c.Data is null || c.Data.Value.IsEmpty) && c.Uri is null)\n        throw new InvalidOperationException(\"BinaryContent needs Data or Uri before sending to Gemini.\");\n}","typeGuard":"static bool HasBinaryPayload(BinaryContent c)\n    => (c.Data is { IsEmpty: false }) || c.Uri is not null;","tryCatchPattern":"try { await kernel.InvokePromptAsync(prompt); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Binary content does not contain any data or uri.\"))\n{ logger.LogError(ex, \"BinaryContent was empty; attach data or a URI.\"); throw; }","preventionTips":["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."],"tags":["gemini","binary","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}