microsoft/semantic-kernel · error · NotSupportedException
FileReferenceContent cannot be converted to ResponseContentP
Error message
FileReferenceContent cannot be converted to ResponseContentPart. Only FileReferenceContent with a file id is supported.
What it means
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.
Source
Thrown at dotnet/src/Agents/OpenAI/Extensions/KernelContentExtensions.cs:57
var dataUri = new Uri($"data:{content.MimeType};base64,{Convert.ToBase64String(content.Data.Value.ToArray())}");
return ResponseContentPart.CreateInputImagePart(dataUri);
}
throw new NotSupportedException("ImageContent cannot be converted to ResponseContentPart. Only ImageContent with a uri or binary data is supported.");
}
internal static ResponseContentPart ToResponseContentPart(this BinaryContent content)
{
return content.Data is not null
? ResponseContentPart.CreateInputFilePart(new BinaryData(content.Data), content.MimeType, Guid.NewGuid().ToString())
: throw new NotSupportedException("AudioContent cannot be converted to ResponseContentPart. Only AudioContent with binary data is supported.");
}
internal static ResponseContentPart ToResponseContentPart(this FileReferenceContent content)
{
return content.FileId is not null
? ResponseContentPart.CreateInputFilePart(content.FileId)
: throw new NotSupportedException("FileReferenceContent cannot be converted to ResponseContentPart. Only FileReferenceContent with a file id is supported.");
}
}
View on GitHub (pinned to c028a0c7dc)
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.
Example fix
// before
var fileRef = new FileReferenceContent();
message.Items.Add(fileRef);
// after
var upload = await client.UploadFileAsync(filePath, "assistants");
var fileRef = new FileReferenceContent { FileId = upload.Value.Id };
message.Items.Add(fileRef); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(fileReferenceContent.FileId))
throw new ArgumentException("FileReferenceContent must have a FileId."); Type guard
static bool HasFileId(FileReferenceContent c) => !string.IsNullOrEmpty(c.FileId);
Try / catch
try { var part = fileRef.ToResponseContentPart(); }
catch (NotSupportedException ex) when (ex.Message.Contains("FileReferenceContent cannot be converted")) {
// upload the file first, then assign the returned id
} Prevention
- Upload files and capture the id before constructing FileReferenceContent.
- Check upload responses for a non-null id.
- Handle upload failures before continuing.
When it happens
Trigger: Adding FileReferenceContent to a message before/without a file upload returning an id.
Common situations: File upload failed silently so FileId was never set; constructing FileReferenceContent manually without an id.
Related errors
- Invalid OpenAI client type '{connection.Type}' was specified
- Unsupported role {message.Role.Label}. Only system, user, de
- Unsupported content type {content.GetType().Name}. Cannot co
- ImageContent cannot be converted to ResponseContentPart. Onl
- AudioContent cannot be converted to ResponseContentPart. Onl
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/0e9c89faa2fe3b67.
Report an issue: GitHub.