How do you deal with constant API errors?

When I run this basic api request

from openai import OpenAI

api_key = "sk-proj-AAAAAAAAAAAAAAAAAAAAAAAA"
client = OpenAI(api_key = api_key)

for i in range(10):
  try:
    response = client.chat.completions.create(
      model="gpt-4o",
      messages=[
        {
          "role": "user",
          "content": [
            {"type": "text", "text": "What's in this image?"},
            {
              "type": "image_url",
              "image_url": {
                "url": "https://firebasestorage.googleapis.com/v0/b/libre-951c9.appspot.com/o/images%2F676ad9eeb1ab2f9f12e44885%2Fd4775eeb-a9de-45b8-822b-43081bc0f611__17351243526206805673596199865694.png?alt=media&token=c363bf92-4764-4beb-afc9-4d85fda2f90f",
              }
            },
          ],
        }
      ]
    )
    print(f"{i} SUCCESS: {response.choices[0].message.content[:60]}")
  except Exception as e:
    print(f"{i} ERROR: {e.status_code}, {e.type}, {e.code}, {e.body['message'][:60]}")

I keep getting errors 400 like this:

0 ERROR: 400, invalid_request_error, invalid_image_url, Timeout while downloading https://firebasestorage.googleapis
1 ERROR: 400, invalid_request_error, invalid_image_url, Timeout while downloading https://firebasestorage.googleapis
2 ERROR: 400, invalid_request_error, invalid_image_url, Timeout while downloading https://firebasestorage.googleapis
3 ERROR: 400, invalid_request_error, invalid_image_url, Timeout while downloading https://firebasestorage.googleapis
4 ERROR: 400, invalid_request_error, invalid_image_url, Timeout while downloading https://firebasestorage.googleapis
5 SUCCESS: The image shows a close-up of a wooden surface with a light 
6 SUCCESS: The image shows a wooden surface with a natural grain patter
7 SUCCESS: This image shows a close-up of a wooden surface with a light
8 ERROR: 400, invalid_request_error, invalid_image_url, Timeout while downloading https://firebasestorage.googleapis
9 SUCCESS: The image shows a wooden surface, such as a tabletop or desk

Do you guys just push through the frequent errors by retrying 5 times? Or how do you deal with all these errors?

1 Like

Welcome to the community!

TBH, I just encode the images as data urls, that basically gets rid of this entire error surface altogether.

I don’t really know why people fetch images remotely :confused: