createVariation example code from api docs doesn't seem to work

I’m trying to create an image variant using the example code (node.js) from the API reference docs: (I can’t include a link to the docs here)

Here is the exact code I’m using:

import fs from "fs";
import OpenAI from "openai";

const openai = new OpenAI({apiKey: 'my-api-key-here'});

async function main() {
  const image = await openai.images.createVariation({
    image: fs.createReadStream("./.tmp/tmp.png"),
  });

  console.log(image.data);
}
main();

I consistently get a generic Connection Error from openai. Here is the output of running this simple program:

miketorra@Mikes-MacBook-Pro server % node ./OAITEST.js
(node:2522) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
file:///Users/miketorra/x6/hawk-web/server/node_modules/openai/core.mjs:285
            throw new APIConnectionError({ cause: response });
                  ^

APIConnectionError: Connection error.
    at OpenAI.makeRequest (file:///Users/miketorra/x6/hawk-web/server/node_modules/openai/core.mjs:285:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async main (file:///Users/miketorra/x6/hawk-web/server/OAITEST.js:7:17) {
  status: undefined,
  headers: undefined,
  error: undefined,
  code: undefined,
  param: undefined,
  type: undefined,
  cause: FetchError: request to https://api.openai.com/v1/images/variations failed, reason: read ECONNRESET
      at ClientRequest.<anonymous> (/Users/miketorra/x6/hawk-web/server/node_modules/node-fetch/lib/index.js:1501:11)
      at ClientRequest.emit (node:events:519:28)
      at TLSSocket.socketErrorListener (node:_http_client:495:9)
      at TLSSocket.emit (node:events:531:35)
      at emitErrorNT (node:internal/streams/destroy:169:8)
      at emitErrorCloseNT (node:internal/streams/destroy:128:3)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    type: 'system',
    errno: 'ECONNRESET',
    code: 'ECONNRESET'
  }
}

Node.js v21.5.0

I’ve tried making sure the image fits all the criteria outlined here: (I can’t include a link to the post here, but it’s on the OpenAI community forum titled “bug-error-413-the-data-value-transmitted-exceeds-the-capacity-limit-when-calling-v1-images-edits”). I’ve tried different images of different sizes. I haven’t been able to get this API call to work, but I’m using the exact example from the docs (except of course I don’t have a ‘known good’ image to use as input, other than knowing that it works when I ask ChatGPT to create a variant, which I’m aware is different).

Is there a functioning example of how to generate an image variant via API?

Hey there and welcome to the community!

Are you using a VPN?

Have you tried using just the completions API to see if that worked?

No, I’m not using a VPN.

Yes, the completions API works for me.

Does anyone have any suggestions? This is literally the exact example code copied from the documentation, and the error message is generic and gives no suggestion as to what the problem might be.

The only variable I can think of is the image itself, although again the error message gives no hint that that may be the issue. Is there a known good image I could test the API with? Does the API work?

After taking another peek at this (my apologies for not catching this earlier, I’m getting better each day at API debugging lol), I notice your call looks like this:

Maybe try the python version first?

response = client.images.create_variation(
 image=open("image_edit_original.png", "rb"),
 n=2,
 size="1024x1024"
)

src: https://platform.openai.com/docs/api-reference/images/createVariation

EDIT: I just now realized that you’re calling from node, not python, so that explains the different syntax. Have you tried running a python script first to see if the effect is the same?

1 Like