{"record":{"id":"d0cb0d759c6bca7d","repo":"microsoft/semantic-kernel","slug":"imagecontent-must-have-either-data-or-a-uri","errorCode":null,"errorMessage":"ImageContent must have either Data or a Uri.","messagePattern":"ImageContent must have either Data or a Uri\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs","lineNumber":902,"sourceCode":"\n        throw new NotSupportedException($\"Role {message.Role} is not supported.\");\n    }\n\n    private static ChatMessageContentPart GetImageContentItem(ImageContent imageContent)\n    {\n        ChatImageDetailLevel? detailLevel = GetChatImageDetailLevel(imageContent);\n\n        if (imageContent.Data is { IsEmpty: false } data)\n        {\n            return ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(data), imageContent.MimeType, detailLevel);\n        }\n\n        if (imageContent.Uri is not null)\n        {\n            return ChatMessageContentPart.CreateImagePart(imageContent.Uri, detailLevel);\n        }\n\n        throw new ArgumentException($\"{nameof(ImageContent)} must have either Data or a Uri.\");\n    }\n\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        }","sourceCodeStart":884,"sourceCodeEnd":920,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs#L884-L920","documentation":"Thrown by GetImageContentItem. An ImageContent is turned into an OpenAI image part either from inline bytes (Data) or from a URI. If Data is empty/null AND Uri is null, the image part cannot be built because OpenAI requires one of the two, so an ArgumentException is raised naming ImageContent.","triggerScenarios":"Constructing ImageContent with neither Data nor Uri set (e.g. only a MimeType), or with an empty Data buffer and no fallback Uri, then including it in a chat message.","commonSituations":"Loading an image that failed to read (Data stayed empty) and forgetting to set Uri; building ImageContent from metadata only; a stream copy that produced zero bytes.","solutions":["Ensure ImageContent.Data is a non-empty byte buffer, or set ImageContent.Uri to a reachable URL.","After loading bytes, assert data.Length > 0 before constructing ImageContent.","If the source is a URL, prefer the Uri overload of ImageContent."],"exampleFix":"// before\nvar img = new ImageContent(null, \"image/png\");\n// after\nvar img = new ImageContent(await File.ReadAllBytesAsync(path), \"image/png\");","handlingStrategy":"validation","validationCode":"static ImageContent BuildImage(byte[]? data, Uri? uri, string mime) { if (data is { Length: > 0 }) return new ImageContent(data, mime); if (uri is not null) return new ImageContent(uri); throw new ArgumentException(\"ImageContent needs Data or Uri\"); }","typeGuard":"static bool ImageContentIsUsable(ImageContent i) => i.Data is { IsEmpty: false } || i.Uri is not null;","tryCatchPattern":"try { await client.GetChatCompletionAsync(history); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Data or a Uri\")) { /* load bytes / set Uri and retry */ }","preventionTips":["Assert data.Length > 0 after loading.","Prefer Uri overload when the source is a URL."],"tags":["openai","vision","image-content","argument-validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}