Create Image Edit in Node.js library - watch out for the order of positional arguments

Seen there have been quite a few people struggling with the create image edit method when including a mask image in their api call. I was getting a 400 Bad Request error for a few hours yesterday, and it took me a while to figure out, but the API Reference documents the wrong order for createImageEdit’s positional arguments. The positional order inside OpenAI’s Node.js library is (image → prompt → mask), but the documentation uses (image → mask → prompt)…

Create image edit in API reference:

const response = await openai.createImageEdit(
  fs.createReadStream("otter.png"), # image
  fs.createReadStream("mask.png"), # mask
  "A cute baby sea otter wearing a beret", # prompt
  ...
);

But… the createImageEdit method in ‘openai’ node_module (openai/dist/api.js line 383) reads:

createImageEdit: (image, prompt, mask, n, size, responseFormat, user, options = {}) => __awaiter(this, void 0, void 0, function* () {

Super easy fix - just swap the position of ‘mask’ and ‘prompt’ inside your calls:

# Change this...
const response = await openai.createImageEdit(
  fs.createReadStream("otter.png"),
  fs.createReadStream("mask.png"),
  "A cute baby sea otter wearing a beret",
  ...
);

# to this...
const response = await openai.createImageEdit(
  fs.createReadStream("otter.png"),
  "A cute baby sea otter wearing a beret",
  fs.createReadStream("mask.png"),
  ...
);

This inconsistency shouldn’t exist in other libraries as Node.js seems to be the only one that uses positional arguments for this method.

Hopefully this helps save people some time.

3 Likes

What is responseFormat, user, options? Are they are necessary? Can we do same in JavaScript? I am using JavaScript and getting an error "‘image’ is the required property. I am trying to send data using FormData. My image is ImageData.

In terms of the node method documentation, responseFormat, user and options weren’t necessary for my project back in July.

They’re optional parameters which can be appended to the request body. They’re explained in the API reference (https://platform.openai.com/docs/api-reference/images/createEdit) - I’ve also attached them below:



And in terms of your specific error - ‘image’ is a required property - it could be one of these three things:

(1) Invalid file path. Make sure your file path points to the correct location.

(2) Unopened image. It’s important to call fs.createReadStream() on the image to stream it as binary data and make sure you’re not just passing a string to the API!

So, make sure your call takes the following format:
image: fs.createReadStream("valid_file_path/image.png")

(3) ImageData. I’m unsure if the OpenAI still supports sending the image as different data types - it sounds like this could also be the issue from your description. I know they can return images as b64-encoded (when specified using the responseFormat argument), but don’t know whether you can still pass images directly to the API in this format too. It may be worth checking how your image is encoded before it’s passed to the createVariation endpoint?

Hope this helps! :slight_smile:

1 Like