createImageVariation | source.on is not a function

I’m looking for the correct way to read a PNG from S3 and send it to the openai API.

Given:

const command = new GetObjectCommand({
    Bucket: BUCKET,
    Key: `library/${sourceId}.png`
});

const result = await s3.send(command);
const stream = Readable.from(result.Body);

const response = await openai.createImageVariation({ image: stream });

I get the error:

Uncaught TypeError: source.on is not a function
    at Function.DelayedStream.create (/home/ec2-user/environment/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
    at FormData.CombinedStream.append (/home/ec2-user/environment/node_modules/combined-stream/lib/combined_stream.js:45:37)
    at FormData.append (/home/ec2-user/environment/node_modules/form-data/lib/form_data.js:75:3)
    at Object.<anonymous> (/home/ec2-user/environment/node_modules/openai/dist/api.js:454:36)
    at Generator.next (<anonymous>)
    at /home/ec2-user/environment/node_modules/openai/dist/api.js:21:71
    at new Promise (<anonymous>)
    at __awaiter (/home/ec2-user/environment/node_modules/openai/dist/api.js:17:12)
    at Object.createImageVariation (/home/ec2-user/environment/node_modules/openai/dist/api.js:439:87)

I have also tried:

const response = await openai.createImageVariation({ image: response.Body });

and I get the same error, source.on is not a function.

TIA

Given the dataset on which chatgpt was trained, the code produced does not always work. In this case, the example you posted stopped working in node 16.10.

> const buffer = await streamToBuffer(result.Body);
Uncaught TypeError [ERR_MISSING_ARGS]: The "streams" argument must be specified
    at __node_internal_captureLargerStackTrace (node:internal/errors:478:5)
    at new NodeError (node:internal/errors:387:5)
    at pipelineImpl (node:internal/streams/pipeline:181:11)
    at node:stream/promises:28:5
    at new Promise (<anonymous>)
    at pipeline (node:stream/promises:17:10)
    at REPL62:1:49 {
  code: 'ERR_MISSING_ARGS'
}

We identified the solution. Rather than passing a JSON object (as demonstrated in the createImage API) the createImageVariation expects parameters.

Final solution:

const command = new GetObjectCommand({
    Bucket: BUCKET,
    Key: `library/${sourceId}.png`
});

const result = await s3.send(command);
const body = result.Body;
body.name = "image.png"

const response = await openai.createImageVariation(body);
1 Like