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

  1. Upload the file via the files API first and assign the returned id to FileReferenceContent.FileId.
  2. Check the upload result for a non-null id before constructing the content.
  3. 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

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


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/0e9c89faa2fe3b67. Report an issue: GitHub.