Dall-e-2 code "Bad request error"

Hi everyone,

I am using what I thought to be very simple code (I am not an experienced coder, but have been using this dall-e-2 code for about a year now) and suddenly it isn’t working.

This is the code I’m running.

I start by opening a new terminal, running python3.12, then:

from openai import OpenAI
client = OpenAI()
response = client.images.generate(
model="dall-e-2",
prompt="A picture of a two-headed dog",

size="1024x1024",
n=1,
)

Then I get an error (which I’ve never gotten before) that reads:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/resources/images.py", line 256, in generate
    return self._post(
           ^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/_base_client.py", line 1240, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/_base_client.py", line 921, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/_base_client.py", line 1020, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': None, 'type': 'invalid_request_error', 'param': None, 'code': None}}
>>> image_url = response.data[0].url
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'response' is not defined

Can anyone help me fix the problem I’m running into here? Thank you so much!

I’d make sure you library is up to date…

Or maybe try not sending model at all, as I think it defaults to DALLE2?

Welcome to the community!

3 Likes

Hello,

From what I found in regard to your problem. Seems your code stopped may have stopped working due to a change in the OpenAI Python SDK.

If you’ve been using model=“dall-e-2” in images.generate(), that’s no longer supported in SDK v1.0.0 and up. See link below for additional details.

OpenAI Python v1.0.0 Migration Guide #742

Possible solution.

  • Remove this line from your code :
model="dall-e-2"
  • Update the OpenAI Python SDK to the latest version:
pip install --upgrade openai

Additionally, make sure you are working inside a virtual environment to avoid global package issues.

Let me know this helps,

Jorge.

1 Like

The script works.

It only depends on the correct version of the OpenAI python library, in my case openai.__version__ is 1.78.0:

pip install --upgrade openai

and that you have set the API key as an environment variable OPENAI_API_KEY without any garbage characters within.

1 Like

I did the command you suggested to upgrade openai, then ran the exact code that you ran and it worked for about 10 sessions, and now it’s not working again!

I checked that my API key is stored in my .zshrc file as OPENAI_API_KEY. I saved this file, then ran source ~/.zshrc and started a new terminal.

When the new terminal opened, I ran pip install --upgrade openai again to check that I had updated it, and it said “requirement satisfied” throughout the response.

So I tried to run the dall-e-2 code you ran again and then I still got the same error,

>>> client = OpenAI()
>>> response = client.images.generate(
... prompt="A photo of a dog",
... size="1024x1024",
... n=1,
... )
print(response)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/resources/images.py", line 322, in generate
    return self._post(
           ^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/_base_client.py", line 1239, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/_base_client.py", line 1034, in request
    raise self._make_status_error_from_response(err.response) from None
openai.InternalServerError: Error code: 500 - {'error': {'message': 'The server had an error while processing your request. Sorry about that!', 'type': 'server_error', 'param': None, 'code': None}}
>>> print(response)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'response' is not defined
type or paste code here

Any other ideas? I’m using python3.12.

Also, if this helps provide more context, I just opened a new terminal and immediately ran

echo $OPENAI_API_KEY

and it gave me back my current correct API key.

First: are you pasting “print(response)” to a Python REPL environment at the same time as the response function?

Got to do one at a time. Maybe it is the technique that failed.

You can also move to dall-e-3 if the service is being super-flaky with soft deprecation.

Need some reliable code beyond your wishes? Try here:

Hi again, looked further into your issue.

You can try running this command to make sure key and enviroment are correct:

python3.12 -c "from openai import OpenAI; print(OpenAI().models.list())"

If it returns a list of models, your API key and Python environment are working correctly.

Now, following up on your issue since you mentioned things stopped working after about 10 successful runs:

A 500 Error typically means there’s a problem on OpenAI’s side, not in your code. These errors are usually temporary. Try the same code again after a short wait.

If the issue persists, it could also be related to rate limits or unstable network conditions. Slowing down your requests or checking your internet connection might help.

To ensure your local environment isn’t contributing to the issue, follow the steps below to isolate dependencies and eliminate package conflicts.


Clean Environment Setup

  1. (Optional) Delete the old virtual environment

    rm -rf venv
    
  2. Create and Activate a new virtual environment

    python3.12 -m venv venv
    source venv/bin/activate
    
  3. Upgrade pip and install the OpenAI SDK

    python3 -m pip install --upgrade pip
    pip install --upgrade openai
    
  4. Verify the API key and environment

    python3.12 -c "from openai import OpenAI; print(OpenAI().models.list())"
    

    If this prints a list of models, then everything is configured properly.

Hope this helps,

Jorge.

Thank you so much! I did all of those steps and everything worked, then I re-ran my dall-e-2 image generation code and got the same 500 error followed immediately by

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'response' is not defined

:frowning:
My account is funded too so I doubt that’s the issue. Thanks again for your help!

Hey Scooty,

Sorry to hear you’re still running into that 500 error.

Unfortunately, I’m limited on resources at the moment, so I can’t replicate to further troubleshoot it on my end.

Best of luck, and definitely let me know if you find a fix. I’d love to hear what worked.

Jorge.