TypeError: openai.createImage is not a function

I’m having trouble debugging this error. I’m able successfully make a request to openai.createCompletion(), but openai.createImage() gives me the TypeError mentioned in the title of this post.

    const imageResponse = await fetch("/api/imagegen", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ title: title }),
    });

/imagegen.js

import { Configuration, OpenAIApi } from "openai";

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

export default async function (req, res) {
    console.log(openai);
    const image = await openai.createImage({
        prompt: generatePrompt(req.body.title),
        n: 1,
        size: "1024x1024",
    });

    res.status(200).json({ result: image.data.choices[0].text });
}

function generatePrompt(title) {
    return `${title} in the style of high-quality food photography`;
  }

Solved: I had to upgrade openai to 3.1.0

I have upgraded to 3.1.0 but still the same error

This is still a problem on 3.2.1.

const response = await openai.createImage({
^

TypeError: openai.createImage is not a function

Getting typeerror and i am in version 4.2.0.

1 Like

Has your error been resolve?
if yes kindly give the solution

See the API docs. For openai 4.x:

import OpenAI from "openai";

const openai = new OpenAI();

async function main() {
  const image = await openai.images.generate({ prompt: "A cute baby sea otter" });

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

now i am getting this
BadRequestError: Billing hard limit has been reached

You can adjust your usage limits here.

1 Like
generateImage: async (req, res) => {
    try {
      const openai = new OpenAI();

      const response = await openai.images.generate({
        model: "dall-e-3",
        prompt: "man on the moon",
        n: 1,
        size: "1024x1024",
      });

      res.status(200).json({
        success: true,
        data: response.data,
      });
    } catch (error) {
      if (error.response) {
        console.log(error.response.status);
        console.log(error.response.data);
      } else {
        console.log(error.message);
      }

      res.status(400).json({
        success: false,
        error: "The image could not be generated",
      });
    }
  },
1 Like