openai-node
openai-node copied to clipboard
createImageVariation gives unclear error when input image is not square
Describe the bug
Using the function createImageVariation with a non square image results in the following error:
(node:58341) UnhandledPromiseRejectionWarning: Error: Request failed with status code 400
at createError (/Users/kenny.lindahl/Dev/test/open-ai-gpt/node_modules/axios/lib/core/createError.js:16:15)
at settle (/Users/kenny.lindahl/Dev/test/open-ai-gpt/node_modules/axios/lib/core/settle.js:17:12)
at IncomingMessage.handleStreamEnd (/Users/kenny.lindahl/Dev/test/open-ai-gpt/node_modules/axios/lib/adapters/http.js:322:11)
at IncomingMessage.emit (events.js:387:35)
at endReadableNT (internal/streams/readable.js:1317:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21)
Solution:
Option 1: The client should know that the image is not square and throw an error and not call the API.
Option 2: Alternatively it should change the image size (add margin, not stretch the image content) so it can be sent to the API with a successful response.
To Reproduce
Complete node program that reproduces the issue:
const { Configuration, OpenAIApi } = require("openai");
const fs = require("fs");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
(async () => {
const response = await openai.createImageVariation(
fs.createReadStream(__dirname + "/images/non-square-image.png"),
2,
"1024x1024"
);
console.log("-------------------");
console.log(response);
})();
Code snippets
No response
OS
macOS Monterey: 12.5.1 (21G83)
Node version
v14.17.3
Library version
3.1.0
I had the same issue with the response always being that it must be a valid PNG file and less than 4MB, but it didn't mention that it has to be a square so I was confused for hours not knowing what the issue even was.
I managed to bypass the issue by using the sharp library:
const buffer = await sharp(Buffer.from(image.data, "base64"))
.png()
.resize(400, 400, {
fit: sharp.fit.inside,
withoutEnlargement: true,
})
.toBuffer();
buffer.name = "input.png"
const response = await openai.createImageVariation(
buffer,
)
// Consume the response the way you want
// [...]
Both options mentioned by @KennyLindahl are good in my opinion.
Thank you for the report! Our upcoming v4 of this library does a better job of highlighting 4xx error messages; give it a try: https://github.com/openai/openai-node/discussions/182