Getting 400 error trying to generate image with Dalle-3?

I have used the Dalle-2 API for months to generate images without fail. Today I made my first attempt to use Dalle-3. Unfortunately I am geting 400 BAD_REQUEST errors every time. Below is the error object I am getting from the Axios POST request, with my request object embedded.

Can someone help me figure this out?

{
  "message": "Request failed with status code 400",
  "name": "Error",
  "stack": "Error: Request failed with status code 400\n")",
  "config": {
    "url": "https://api.openai.com/v1/images/generations",
    "method": "post",
    "data": "{\"model\":\"dalle-e-3\",\"prompt\":\"ducks\",\"n\":1,\"size\":\"1024x1024\",\"response_format\":\"b64_json\"}",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/json",
      "Authorization": "Bearer <my API key>",
      "User-Agent": "axios/0.19.2",
      "Content-Length": 92
    },
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1
  }
}

Are you using the latest library? cURL?

Check out the Images API docs and DALLE3 Cookbook page if you haven’t yet.

Hi Paul. I’m using the axios fetch library from my Node.js back-end server. It worked fine with Dalle-2:

       // For now, Dalle-3 only allows one image to be created at a time.
        if (theNumberOfImagesToMake !== 1)
            throw new Error(`${errPrefix}Dalle-3 only generates one image at a time.  Use parallel calls to this function (see generateImagesWithDalle3())`);

        const Dalle3RequestObj =
            {
                model: theModel,
                prompt: usePrompt,
                n: theNumberOfImagesToMake, // Number of images to generate. Must be set to 1 for now.
                size: `${theImageSize}x${theImageSize}`,

                // NOTE!!: The response format 'b64_json' is oddly named.
                //  It is absolutely not a JSON object or an object at all.
                //  It's just the name of a property that you will find in each
                //  result element in the results array that contains a PNG
                //  image in base 64 encoded string format.
                response_format: 'b64_json',
            }

        try {
            if (bVerbose)
                console.info('generate-images', `Making generate image request to Dalle-3 with prompt: ${Dalle3RequestObj.prompt}`);

            const responseObj = await axios.post(
                URL_OPENAI_DALLE_3_ENDPOINT,
                Dalle3RequestObj,
                {
                    headers: {
                        "Content-Type": "application/json",
                        "Authorization": `Bearer ${apiKey}`
                    },
                }
            );

Yes I did look at those pages you linked me. Thanks. I didn’t see anything different than what I was doing, except for the fact that I have to do 1 image at a time.

In your code, you have specified the model as "dalle-e-3", but according to the documentation, the correct parameter should be "dall-e-3" (without the extra ā€˜e’)

1 Like

Thanks Paul! That was it. Is there any way to get more detailed information about what causes a 400 error? Perhaps something like ā€œinvalid model nameā€ in this case?

1 Like

Well, 400 in general is ā€œbad request,ā€ but I missed the typo on my first glance. With a 400 error, though, you want to look at what you’re sending - parameters. Glad you got it sorted.

1 Like