{"record":{"id":"0e9c89faa2fe3b67","repo":"microsoft/semantic-kernel","slug":"filereferencecontent-cannot-be-converted-to-respon","errorCode":null,"errorMessage":"FileReferenceContent cannot be converted to ResponseContentPart. Only FileReferenceContent with a file id is supported.","messagePattern":"FileReferenceContent cannot be converted to ResponseContentPart\\. Only FileReferenceContent with a file id is supported\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/OpenAI/Extensions/KernelContentExtensions.cs","lineNumber":57,"sourceCode":"            var dataUri = new Uri($\"data:{content.MimeType};base64,{Convert.ToBase64String(content.Data.Value.ToArray())}\");\n            return ResponseContentPart.CreateInputImagePart(dataUri);\n        }\n\n        throw new NotSupportedException(\"ImageContent cannot be converted to ResponseContentPart. Only ImageContent with a uri or binary data is supported.\");\n    }\n\n    internal static ResponseContentPart ToResponseContentPart(this BinaryContent content)\n    {\n        return content.Data is not null\n            ? ResponseContentPart.CreateInputFilePart(new BinaryData(content.Data), content.MimeType, Guid.NewGuid().ToString())\n            : throw new NotSupportedException(\"AudioContent cannot be converted to ResponseContentPart. Only AudioContent with binary data is supported.\");\n    }\n\n    internal static ResponseContentPart ToResponseContentPart(this FileReferenceContent content)\n    {\n        return content.FileId is not null\n            ? ResponseContentPart.CreateInputFilePart(content.FileId)\n            : throw new NotSupportedException(\"FileReferenceContent cannot be converted to ResponseContentPart. Only FileReferenceContent with a file id is supported.\");\n    }\n}\n","sourceCodeStart":39,"sourceCodeEnd":60,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/OpenAI/Extensions/KernelContentExtensions.cs#L39-L60","documentation":"The FileReferenceContent overload of ToResponseContentPart needs content.FileId to build an input-file part referencing an uploaded file. If FileId is null, it throws NotSupportedException.","triggerScenarios":"Adding FileReferenceContent to a message before/without a file upload returning an id.","commonSituations":"File upload failed silently so FileId was never set; constructing FileReferenceContent manually without an id.","solutions":["Upload the file via the files API first and assign the returned id to FileReferenceContent.FileId.","Check the upload result for a non-null id before constructing the content.","Handle upload failures explicitly instead of continuing with an empty FileReferenceContent."],"exampleFix":"// before\nvar fileRef = new FileReferenceContent();\nmessage.Items.Add(fileRef);\n// after\nvar upload = await client.UploadFileAsync(filePath, \"assistants\");\nvar fileRef = new FileReferenceContent { FileId = upload.Value.Id };\nmessage.Items.Add(fileRef);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(fileReferenceContent.FileId))\n    throw new ArgumentException(\"FileReferenceContent must have a FileId.\");","typeGuard":"static bool HasFileId(FileReferenceContent c) => !string.IsNullOrEmpty(c.FileId);","tryCatchPattern":"try { var part = fileRef.ToResponseContentPart(); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"FileReferenceContent cannot be converted\")) {\n    // upload the file first, then assign the returned id\n}","preventionTips":["Upload files and capture the id before constructing FileReferenceContent.","Check upload responses for a non-null id.","Handle upload failures before continuing."],"tags":["openai","file-reference","invalid-argument"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}