Node package openai.files.create can't upload file

I’m developing a typescript project which receive the uploaded files from users and I will pass some of the files to OpenAI API.

What I have is

const uploadedFile = {
  fieldname: 'file',
  originalname: 'my.csv',
  encoding: '7bit',
  mimetype: 'text/csv',
  buffer: <Buffer  e8 b2 ... 15264 more bytes>,
  size: 15266
}

What I tried is

  1. Directly upload
const uploadedFile = await openai.files.create({
      // @ts-expect-error ignore
      file: uploadedFile,
      purpose: 'assistants',
    });

[Nest] 23161 - 03/20/2024, 11:17:56 AM ERROR [ExceptionsHandler] 413 The data value transmitted exceeds the capacity limit.
Error: 413 The data value transmitted exceeds the capacity limit.

Its size is only 15KB, hard to believe it exceeded the upload limit.

  1. Blob
const blob = new Blob([uploadedFile.buffer], { type: file.mimetype });
const uploadedFile = await openai.files.create({
      // @ts-expect-error ignore
      file: blob,
      purpose: 'assistants',
    });

// console.log(blog) is
Blob { size: 15266, type: ‘text/csv’ }
// The error message of openai.files.create
[Nest] 22537 - 03/20/2024, 10:38:13 AM ERROR [ExceptionsHandler] 400 ‘file’ is a required property
Error: 400 ‘file’ is a required property

Did you ever get a resolution to this? I’m having the same issue. 33kb file and getting the error " 413 The data value transmitted exceeds the capacity limit.".

Just solved it. I was trying to upload it this way:

// Read the document into a Buffer
const documentBuffer = fs.readFileSync(filePath);
console.log(Buffer length: ${documentBuffer.length} bytes);

// Upload the document to OpenAI and get the file ID
const response = await openai.files.create({
purpose: ‘assistants’,
file: documentBuffer
});

Which wasn’t working. I changed it to this and it worked:

const response = await openai.files.create({
purpose: ‘assistants’,
file: fs.createReadStream(filePath)
});