400 Error when I include base64 image in a prompt

When I call the API with a text only prompt, things work fine. However, when I try to add an image to the prompt as well I’m getting a 400 error. I’ve looked at the spec and everything looks correct. Maybe another set of eyes can see what I’m doing wrong here.

Here is the code:

Message message = new Message();
        message.setRole("user");

        Content contentText = new Content();
        contentText.setType("text");
        contentText.setText(promptText);

        // Validate and trim the base64 string if necessary
        if (base64ImageStr != null && !base64ImageStr.isEmpty()) {
            base64ImageStr = base64ImageStr.replace("\n", "").replace("\r", "");
        }

        Content contentImage = new Content();
        contentImage.setType("image");
        contentImage.setImage_url("data:image/jpeg;base64," + base64ImageStr);

        message.setContent(List.of(contentText, contentImage));

        request.setMessages(List.of(message));

You don’t modify the contents of a base64 unless you want to corrupt it.

This is the format of message that must be sent to chat completions:

[
    {
        "role": "system",
        "content": [
            {"type": "text", "text": system_prompt}
        ]
    },
    {
        "role": "user",
        "content": [
            {"type": "text", "text": "Describe this art style."},
            {
                "type": "image_url",
                "image_url": {"url": f"data:image/jpeg;base64,{encoded_string}"}
            }
        ]
    }
]

Assistants does not accept base64 images.

2 Likes

That was very helpful. I had misformatted the url object within the image_url field.

1 Like

Can you please send the right formatted code ?, I did not understand why the original code is not right.
Thanks