# 400 Error when I include base64 image in a prompt

**URL:** <https://community.openai.com/t/400-error-when-i-include-base64-image-in-a-prompt/788596>\
**Category:** API\
**Created:** [May 31, 2024, 3:32am UTC](https://community.openai.com/t/400-error-when-i-include-base64-image-in-a-prompt/788596 "2024-05-31T03:32:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cboldon](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/cboldon/32/385910_2.png) [@cboldon](https://community.openai.com/u/cboldon)\
**Post date:** [May 31, 2024, 3:32am UTC](https://community.openai.com/t/400-error-when-i-include-base64-image-in-a-prompt/788596/1 "2024-05-31T03:32:11Z")

</div>

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:

```auto
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));

```

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [May 31, 2024, 4:34am UTC](https://community.openai.com/t/400-error-when-i-include-base64-image-in-a-prompt/788596/2 "2024-05-31T04:34:07Z")

</div>

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:

```auto
[
    {
        "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.

---

<div class="post-metadata">

**Author:** ![cboldon](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/cboldon/32/385910_2.png) [@cboldon](https://community.openai.com/u/cboldon)\
**Post date:** [May 31, 2024, 4:58pm UTC](https://community.openai.com/t/400-error-when-i-include-base64-image-in-a-prompt/788596/3 "2024-05-31T16:58:21Z")

</div>

That was very helpful. I had misformatted the url object within the image\_url field.

---

<div class="post-metadata">

**Author:** ![vikashmeena7781](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/vikashmeena7781/32/393420_2.png) [@vikashmeena7781](https://community.openai.com/u/vikashmeena7781)\
**Post date:** [June 6, 2024, 6:06am UTC](https://community.openai.com/t/400-error-when-i-include-base64-image-in-a-prompt/788596/4 "2024-06-06T06:06:04Z")

</div>

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