Search code examples
pythonpycharmopenai-api

How do I fix; openai.BadRequestError: Error code: 400?


from openai import OpenAI

prompt = input("What would you like an image of? ")

def generate_image(prompt):
    response = (client.images.generate(
        model="dall-e-3",
        prompt=prompt,
        n=1,
        quality="standard",
        size="1024x1024"
    ))
    image_url = response["data"][0]["url"]
    return image_url

I have tried redownloading OpenAI, changed different aspects of the image and check to make sure my API_Key was correct and active


Solution

  • Usually 400 comes with an additional message, because Bad Request is a pretty generic HTTP status code. In my case, trying to reproduce the example led to the following error:

    openai.BadRequestError: Error code: 400 - {'error': {'code': 'billing_hard_limit_reached', 'message': 'Billing hard limit has been reached', 'param': None, 'type': 'invalid_request_error'}}
    

    It seems that when trying to insert bad data, the wrong data error message has priority over this one. Could be, maybe you have a free account?

    Take a look over your remaining credits: https://platform.openai.com/settings/organization/billing/overview.

    We won't know for sure, until you look at the associated response body of your request.