Cannot pass an image to GPT-4o

I have a valid PIL image (size 1654 x 2339) that I try to include in my query, to no avail.

The code is the following (with langchain):

buffer = BytesIO()
image.save(buffer, format=“JPEG”)
img_str = base64.b64encode(buffer.getvalue())

orders = model.invoke(
[
HumanMessage(
content=[
{“type”: “text”, “text”: my_prompt},
{
“type”: “image_url”,
“image_url”: {“url”: f"data:image/jpeg;base64,{img_str}"},
},
]
)
])

I get the following error:
BadRequestError: Error code: 400 - {'error': {'message': "You uploaded an unsupported image. Please make sure your image has of one the following formats: ['png', 'jpeg', 'gif', 'webp'].", 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_image_format'}}

Reading the answers that have been posted on this forum, I have tried to reduce the size of the image to 760 x 1075 but in this case I receive another error:
BadRequestError: Error code: 400 - {'error': {'message': 'Invalid base64 image_url.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_base64'}}

Though the encoding looks valid to me: ‘/9j/4AAQSkZJRgABAQAA…’.

What’s the problem?

1 Like

Sorry, can you try again now? Image uploads were broken for a little bit, but we’ve since fixed the issue: OpenAI Status - File upload failing for API and ChatGPT users.

6 Likes

That’s ok, I found the problem: I had to decode in utf-8- the base64-encoded content of the image:

img_str = base64.b64encode(buffer.getvalue()).decode(“utf-8”)

1 Like