Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

400 'file' is a required property when Creating File in CloudFlare Worker #804

Open
1 task done
EvanPiro opened this issue Apr 29, 2024 · 4 comments
Open
1 task done
Labels
bug Something isn't working

Comments

@EvanPiro
Copy link

EvanPiro commented Apr 29, 2024

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • This is an issue with the Node library

Describe the bug

I get the following error when I try to create a file from a CloudFlare worker environment.

 {
      error: {
        message: "'file' is a required property",
        type: 'invalid_request_error',
        param: null,
        code: null
      }
    }

I'm able to successfully create a file from a vanilla nodejs environment. I've also confirmed that CURL requests are working.

To Reproduce

Run the following code in a CloudFlare worker environment.

 const llm = new OpenAI({
    apiKey: env.OPENAI_API_KEY,
  });

  
  const file = await toFile(
    new FormDataBlob([
      new TextEncoder().encode("text is here"),
    ]),
    "finetune.jsonl"
  );

  const res = await llm.files.create({
    file,
    purpose: "fine-tune",
  });

Code snippets

No response

OS

macOS

Node version

v18.0.0

Library version

4.38.5

@EvanPiro EvanPiro added the bug Something isn't working label Apr 29, 2024
@EvanPiro
Copy link
Author

EvanPiro commented May 7, 2024

Has anyone else experienced this? This is a product level blocker on my end and I'm willing to assist in the fix.

@rattrayalex
Copy link
Collaborator

Thanks for reporting, we'll take a look soon. In the meantime, you might try methods other than FormDataBlob to instantiate the file contents, like converting the string to an ArrayBuffer.

@benogle
Copy link

benogle commented May 18, 2024

I came up against this as well. I was trying to upload a PDF, but couldn't get the lib to accept anything other than a FsFileStream from fs.createReadStream. Not blobs, buffers, formdata Files, nothing. It'd either error with the OP's 'file' is required property, or reject the type in addFormValue.

It'd be nice if it could accept a regular Readable stream, not just a file stream from fs. Or maybe a custom openai File object that specified the buffer/stream plus filename and other file metadata that a plain stream / buffer doesn't specify.

After looking at the code, my workaround was to make something that would pass through isFileLike(). This works:

const file = {
  type: 'application/pdf',
  name: 'test.pdf',
  filename: 'test.pdf',
  lastModified: new Date().getTime(),
  arrayBuffer: () => convertThingYouHaveToBuffer(),

  // It doesn't use these, but it needs them to work around type checking
  size: 1,
  text: () => '',
  slice: () => [],
}

@EvanPiro
Copy link
Author

@benogle I've tried that and am still getting the same error. Here is my code:

    const id = uuidv4();

    await env.DOC_BUCKET.put(id, trainingStr);

    const obj = await env.DOC_BUCKET.get(id);

    const file = {
      type: "application/jsonl",
      name: "training.jsonl",
      filename: "training.jsonl",
      lastModified: new Date().getTime(),
      arrayBuffer: async () => await obj.arrayBuffer(),

      // It doesn't use these, but it needs them to work around type checking
      size: 1,
      text: async () => await obj.text(),
      slice: (): any => [],
    };

    const res = await llm.files.create({
      file,
      purpose: "fine-tune",
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants