{"record":{"id":"e3b578c4c3ce76ee","repo":"microsoft/semantic-kernel","slug":"binarycontent-must-have-data-bytes","errorCode":null,"errorMessage":"BinaryContent must have Data bytes.","messagePattern":"BinaryContent must have Data bytes\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs","lineNumber":922,"sourceCode":"\n    private static ChatMessageContentPart GetAudioContentItem(AudioContent audioContent)\n    {\n        if (audioContent.Data is { IsEmpty: false } data)\n        {\n            return ChatMessageContentPart.CreateInputAudioPart(BinaryData.FromBytes(data), GetChatInputAudioFormat(audioContent.MimeType));\n        }\n\n        throw new ArgumentException($\"{nameof(AudioContent)} must have Data bytes.\");\n    }\n\n    private static ChatMessageContentPart GetBinaryContentItem(BinaryContent binaryContent)\n    {\n        if (binaryContent.Data is { IsEmpty: false } data)\n        {\n            return ChatMessageContentPart.CreateFilePart(BinaryData.FromBytes(data), binaryContent.MimeType, Guid.NewGuid().ToString());\n        }\n\n        throw new ArgumentException($\"{nameof(BinaryContent)} must have Data bytes.\");\n    }\n\n    private static ChatInputAudioFormat GetChatInputAudioFormat(string? mimeType)\n    {\n        if (string.IsNullOrWhiteSpace(mimeType))\n        {\n            return ChatInputAudioFormat.Mp3;\n        }\n\n        return mimeType.ToUpperInvariant() switch\n        {\n            \"AUDIO/WAV\" => ChatInputAudioFormat.Wav,\n            \"AUDIO/MP3\" => ChatInputAudioFormat.Mp3,\n            _ => throw new NotSupportedException($\"Unsupported audio format '{mimeType}'. Supported formats are 'audio/wav' and 'audio/mp3'.\")\n        };\n    }\n\n    private static ChatImageDetailLevel? GetChatImageDetailLevel(ImageContent imageContent)","sourceCodeStart":904,"sourceCodeEnd":940,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs#L904-L940","documentation":"Thrown by GetBinaryContentItem. BinaryContent is sent as a file part and OpenAI requires the actual bytes; empty/null Data cannot form a file, so an ArgumentException is raised. This guards generic binary attachments (e.g. PDF/file uploads) before they reach the wire.","triggerScenarios":"Adding a BinaryContent whose Data is empty or null to a chat message, for example a file that failed to load or a placeholder BinaryContent with only a MimeType.","commonSituations":"Uploading a file that was deleted/not yet written; zero-byte attachment; constructing BinaryContent from a stream left at EOF.","solutions":["Ensure BinaryContent.Data is a non-empty byte buffer before use.","Check the file size > 0 before reading it into BinaryContent.","Delay adding the content until the bytes are loaded."],"exampleFix":"// before\nvar file = new BinaryContent(null, \"application/pdf\");\n// after\nvar file = new BinaryContent(await File.ReadAllBytesAsync(path), \"application/pdf\");","handlingStrategy":"validation","validationCode":"static BinaryContent BuildBinary(byte[]? data, string mime) => data is { Length: > 0 } ? new BinaryContent(data, mime) : throw new ArgumentException(\"BinaryContent needs non-empty Data\");","typeGuard":"static bool BinaryContentIsUsable(BinaryContent b) => b.Data is { IsEmpty: false };","tryCatchPattern":"try { await client.GetChatCompletionAsync(history); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Data bytes\")) { /* load file and retry */ }","preventionTips":["Check file size > 0 before reading.","Avoid placeholder BinaryContent objects."],"tags":["openai","file-upload","binary-content","argument-validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}