Image editing with API- 400 Client Error, 500 Server Error

While working with API to edit image, I got the following error:

Attempt 1/3 failed: 400 Client Error: Bad Request for url:

"https://api.openai.com/v1/images/edits"

Retrying in 5 seconds…
Attempt 2/3 failed: 500 Server Error: Internal Server Error for url: https://api.openai.com/v1/images/edits
Retrying in 5 seconds…
Attempt 3/3 failed: 500 Server Error: Internal Server Error for url: https://api.openai.com/v1/images/edits
Error: Max retries reached. Check API key, network, or server status.
API Error: The server had an error processing your request. Sorry about that! You can retry your request, or contact us through our help center at help.openai.com if you keep seeing this error. (Please include the request ID req_e558c2bbb2416b28bc702c64bc86874a in your email.) (Code: None)
Error: Unexpected response format. Check API response.

The API key is working. I have checked. Some part of the Code is:

API endpoint

url = "https://api.openai.com/v1/images/edits"

Prepare the data payload

data = {
“model”: “dall-e-2”, # Use the correct model for image editing
“prompt”: prompt,
“n”: 1, # Generate one image
“response_format”: “b64_json”, # Return base64-encoded image
}

Set headers with authorization

headers = {
“Authorization”: f"Bearer {API_KEY}"
}

Files list, sending image as a file to OpenAI

files = {
“image”: (os.path.basename(image_path), open(image_path, “rb”), “image/png”),
}

Retry mechanism

max_retries = 3
retry_delay = 5 # seconds
for attempt in range(max_retries):
try:
# Send the request to the OpenAI API for image editing
response = requests.post(url, headers=headers, files=files, data=data)
response.raise_for_status() # Check for HTTP errors
break
except requests.exceptions.RequestException as e:
print(f"Attempt {attempt + 1}/{max_retries} failed: {e}“)
if attempt < max_retries - 1:
print(f"Retrying in {retry_delay} seconds…”)
time.sleep(retry_delay)
else:
print(“Error: Max retries reached. Check API key, network, or server status.”)
files[“image”][1].close() # Close the file before exit
exit(1)

Any idea about how to solve this?

This is not an issue with the API, this is an issue with the attempts made by the code to send to dall-e-2.

There is not a separate “data” if using requests library to post.

The entire request is multipart/form-data, and the parameters are “web form” just like the file submission is. The construction has a filename for the file, and not for the non-file data items, each sent separately.

The input image also must be exactly the output size, and a mask must be marked in 32 bit alpha channel (the image transparency), or in the alpha channel of a separate “mask” file sent. It cannot edit if you haven’t marked where in the image to replace.

Start with a basic request, using the OpenAI’s “openai” Python library SDK, which will return a download URL by default with dall-e-2.

from openai import OpenAI
client = OpenAI()

img = client.images.edit(
  image=open("crazyartist.png", "rb"),
  prompt="A crazed artist sits in front of his oil painting with a picture of a ballerina.",
  n=1,
  size="1024x1024"
)
print(img)

You will be able then to see if you’ve even satisfied the requirements of an input image. The library itself retries, so you don’t need to code that, and your code should be failing, not retrying, until you have made it actually work.

DALL-E 2 : damaged performance

OpenAI broke the model many months ago, it hardly follows prompts, and will output blobs of garbage. Ran just now to verify the API is up for you.

Input image, with mask areas marked in transparent red:

No requested content from the prompt: the previous lightbulb was just infilled with blue.