API error 400 "Unsupported Image" for portrait orientation pictures

For past week or so, I’ve been getting BadRequestErrors consistently, and exclusively on portrait - orientation pictures. Image formats and sizes are all correct, and I can very consistently reproduce this error, which occurs on gpt-4o, gpt-4o-mini, and gpt-4-turbo.

BadRequestError: Error code: 400 - {‘error’: {‘message’: “You uploaded an unsupported image. Please make sure your image is below 20 MB in size and is of one the following formats: [‘png’, ‘jpeg’, ‘gif’, ‘webp’].”, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: ‘image_parse_error’}}

1 Like
  1. Where are these images coming from
  2. What happens if you remove the extension of this image in your own explorer, does the operating system properly infer the mime type?
1 Like
  1. Images are on a supabase storage bucket. I have confirmed no permission / access issues.

  2. Yep my OS properly infers mime.

Hi @RyanC

For low res mode, we expect a 512px x 512px image. For high res mode, the short side of the image should be less than 768px and the long side should be less than 2,000px.
Docs

1 Like

Hi @sps , thanks for the note. I am using high res mode, so shouldn’t the model automatically rescale large images? As an additional point – the images causing issues are un-altered taken on iphones (of various device models).

I have seen previous issues with photos taken with iPhones.

Maybe try processing the files? Convert them into a friendly, low-space format like webp, then deposit it into your database and try that?

1 Like

Yes, that processing does happen.

IIRC iPhone images are HEIC formatted. Are the uploaded images being converted into a supported format before they are sent to the API?

Can you share an example image that’s causing issues?

1 Like

Hi @sps , correct, they are being converted from HEIC to jpg, stored as jpg in the bucket. @RonaldGRuckus thanks for the suggestion. I tried with a webp but unfortunately got same issue.

Here is an example of a jpg image causing issue:

2 Likes

Strange. I’m not getting any errors with this image:

"image_description": "The image features an elegantly decorated bar with a vibrant design. The ceiling is adorned with a striped awning pattern in white and beige. The back of the bar showcases a variety of liquor bottles on shelves within an arched display, backed by floral wallpaper. In front, a marble bar counter is supported by an intricately patterned base, and it is flanked by three pink velvet bar stools. Overall, the setting conveys a chic and stylish atmosphere."
1 Like

Are you using the Supabase client library to perform these transformations/optimizations?

I’m wondering if there is something happening in-between the storage upload, transformation, and then passing of a link.

Have you tried downloading the file from the exact URL at the same time (or immediately before) sending the URL to OpenAI?

Hi @sps , were you pulling the file from a local path?

Yes, I downloaded the image first and then passed the base64 encoded image data in the request.

So strange, I just did the same and got the same error, "{‘error’: {‘message’: “You uploaded an unsupported image. Please make sure your image is below 20 MB in size and is of one the following formats: [‘png’, ‘jpeg’, ‘gif’, ‘webp’].”, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: ‘image_parse_error’}}. Here is my code for local image:

import base64
import requests

# OpenAI API Key
api_key = os.environ.get("OPENAI_API_KEY")

# Function to encode the image
def encode_image(image_path):
  with open(image_path, "rb") as image_file:
    return base64.b64encode(image_file.read()).decode('utf-8')

# Path to your image
image_path = "REPLACE WITH LOCAL FILEPATH"

# Getting the base64 string
base64_image = encode_image(image_path)

headers = {
  "Content-Type": "application/json",
  "Authorization": f"Bearer {api_key}"
}

payload = {
  "model": "gpt-4o-mini",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "What’s in this image?"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": f"data:image/jpeg;base64,{base64_image}"
          }
        }
      ]
    }
  ],
  "max_tokens": 300
}

response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)

print(response.json())

Sorry for the multiple messages here. It occured to me that the image you downloaded from this forum may have been resized when i uploaded it (which I just confirmed – it’s much smaller than the original at 760kb vs 3.3MB). The “resized” one works on my end.

"type": "image_url",
          "image_url": {
            "url": f"data:image/jpeg;base64,{base64_image}"
          }
        }
      ]

Maybe irrelevant but use a mime_type inferring library instead of hardcoding extensions, or even relying on the extension to be correct.

f = magic.Magic(mime=True, uncompress=True)
f.from_file(‘testdata/test.gz’)
‘text/plain’

Can you share a supabase link here instead of trying to upload it? It’s almost guaranteed to be modified

Thanks @RonaldGRuckus – I just tried to add a supabase link but this forum is telling me I can’t add links to my posts :expressionless:

Hi i’m getting same error for my app for API call to GPT4o. Starting Aug 5th there’s been an increase where 90% of images submitted to the api returns 400 error. Even images that were used in the past without issue and no changes were made to this part of my code.

@RyanC has this issue been resolved for you?

Hi @dchase , unfortunately, no. As a workaround I’m just reducing the size of images in my app before making API calls to GPT.

Just want to add that I am having this exact same problem as well. This is completely crippling my app and without finding any solution after scouring the web for hours, I feel like I have no choice but to turn to another model. This problem is occuring even with the old gpt-vision-preview model.

Please get us solutions fast!

1 Like