NodeJS `files.create` expects fs.ReadStream specifically

If the stream is obtained from elsewhere (e.g. S3), then you have to mock the type to pass the internal isFsStream check.

    // OpenAI expects fs.ReadStream specifically
    Object.setPrototypeOf(stream, fs.ReadStream.prototype)

    // OpenAI uses this to detect a file type
    stream.path = 'source.jpeg'

    const file = await this.openai.files.create({
      file: stream,
      purpose: 'assistants'
    })

Welcome to the dev forum @a.gurtovoi

There are multiple methods to upload a file

Here’s an example with the all the available methods:

import fs from 'fs';
import fetch from 'node-fetch';
import OpenAI, { toFile } from 'openai';

// Initialize OpenAI
const openai = new OpenAI();

// Function to upload a file using a mocked stream
async function uploadMockedStream(stream) {
  // Mock the stream to pass the internal `isFsStream` check
  Object.setPrototypeOf(stream, fs.ReadStream.prototype);
  stream.path = 'source.jpeg';

  // Upload the file
  const file = await openai.files.create({
    file: stream,
    purpose: 'assistants'
  });
  return file;
}

// Usage examples
(async () => {
  // Using fs.createReadStream()
  const fsFile = await openai.files.create({ file: fs.createReadStream('input.jsonl'), purpose: 'fine-tune' });
  console.log('Uploaded fs.createReadStream:', fsFile);

  // Using a web File API instance
  const fileAPI = new File(['my bytes'], 'input.jsonl');
  const fileAPIFile = await openai.files.create({ file: fileAPI, purpose: 'fine-tune' });
  console.log('Uploaded File API instance:', fileAPIFile);

  // Using a fetch Response
  const fetchResponse = await fetch('https://somesite/input.jsonl');
  const fetchFile = await openai.files.create({ file: fetchResponse, purpose: 'fine-tune' });
  console.log('Uploaded fetch Response:', fetchFile);

  // Using the toFile helper with a Buffer
  const bufferFile = await openai.files.create({
    file: await toFile(Buffer.from('my bytes'), 'input.jsonl'),
    purpose: 'fine-tune',
  });
  console.log('Uploaded Buffer with toFile:', bufferFile);

  // Using the toFile helper with a Uint8Array
  const uint8ArrayFile = await openai.files.create({
    file: await toFile(new Uint8Array([0, 1, 2]), 'input.jsonl'),
    purpose: 'fine-tune',
  });
  console.log('Uploaded Uint8Array with toFile:', uint8ArrayFile);

  // Uploading a mocked stream
  const mockedStream = // Obtain your stream from S3 or elsewhere
  const mockedStreamFile = await uploadMockedStream(mockedStream);
  console.log('Uploaded mocked stream:', mockedStreamFile);
})();