openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
22.05k stars 3.04k forks source link

client.chat.completions.create not working with base64 images #1635

Closed gustavofuhr closed 1 month ago

gustavofuhr commented 1 month ago

Confirm this is an issue with the Python library and not an underlying OpenAI API

Describe the bug

No idea why, but when using the lib to give gtp-4o-mini an image in base64 it's giving me the following error:

openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid type for 'messages[0].content[1].image_url': expected an object, but got a string instead.", 'type': 'invalid_request_error', 'param': 'messages[0].content[1].image_url', 'code': 'invalid_type'}}

It's essentialy the same code provided in https://platform.openai.com/docs/guides/vision/uploading-base-64-encoded-images which works fine for me.

To Reproduce

Just run the code snippet, I guess.

Code snippets

client = OpenAI(
  project='',
  api_key=""
)

response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": "Describe this image in a few words."},
                    {
                        "type": "image_url",
                        "image_url": f"data:image/jpeg;base64,{base64_image}"
                    },
                ],
            }
        ],
        max_tokens=100
    )

OS

macOS

Python version

Python 3.10.14

Library version

1.40.3

RobertCraigie commented 1 month ago

This is not a bug, your request isn't correct.

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "Describe this image in a few words."},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{base64_image}",
                    },
                },
            ],
        }
    ],
    max_tokens=100,
)