Skip to content

Bedrock Converse Invocation for Llama 3.2 90B Vision works with png input but not jpeg

0

I have a simple python program (fully included below) that I can use with Bedrock that works for a .png file, but when I change to use a jpeg, I get errors. Why does PNG work but JPEG doesn't?

Error I get when using a jpeg file and sending json as the format:

botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the Converse operation: The model returned the following errors: The provided image does not match the specified image format.

Here is how i constructed the png message :

messages = [
        {
            "role": "user",
            "content": [
                {"text": "Describe image"},
                {
                    "image": {
                        "format": "png",
                        "source": {"bytes": image},
                    }
                },
            ],
        }
    ]

but when I have the same file exported as a .jpeg and used with a message

messages = [
        {
            "role": "user",
            "content": [
                {"text": "Describe image"},
                {
                    "image": {
                        "format": "jpeg",
                        "source": {"bytes": image},
                    }
                },
            ],
        }
    ]

I get the following error:

botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the Converse operation: The model returned the following errors: The provided image does not match the specified image format.

Here is what ImageMagick's identify says about the two files:

example.png PNG 512x640 512x640+0+0 8-bit sRGB 239357B 0.000u 0:00.001

example.jpg JPEG 512x640 512x640+0+0 8-bit sRGB 51497B 0.000u 0:00.001

Full Example Python program used:

import boto3

MODEL_ID = "arn:aws:bedrock:us-east-2:<ACCOUNT_ID_HERE>:inference-profile/us.meta.llama3-2-90b-instruct-v1:0"

def main():
    # Load and encode image as base64
    with open("example.png", "rb") as f:
        image = f.read()

    messages = [
        {
            "role": "user",
            "content": [
                {"text": "Describe image"},
                {
                    "image": {
                        "format": "png",
                        "source": {"bytes": image},
                    }
                },
            ],
        }
    ]

    bedrock_runtime = boto3.client("bedrock-runtime")
    response = bedrock_runtime.converse(
        modelId=MODEL_ID,
        messages=messages,
    )
    response_text = response["output"]["message"]["content"][0]["text"]
    print(response_text)


if __name__ == "__main__":
    main()

asked 2 years ago548 views

1 Answer
2
Accepted Answer

The Llama 3.2 90B Vision model in Amazon Bedrock currently supports PNG format for image inputs, but not JPEG. This is why your code works with PNG files but fails with JPEG files.

When you specify "format": "png" in your message structure, you're telling the model to expect a PNG image. The model then successfully processes the PNG file you provide. However, when you change this to "format": "jpeg" and provide a JPEG file, the model encounters an error because it's not designed to handle JPEG inputs.

To use your JPEG image with this model, you would need to convert it to PNG format before sending it to the Bedrock Converse API. You can use a library like Pillow (PIL) in Python to perform this conversion.

Here's how you could modify your code to convert a JPEG to PNG before sending it to the model:

from PIL import Image
import io

# Load JPEG image
jpeg_image = Image.open("example.jpg")

# Convert to PNG
png_buffer = io.BytesIO()
jpeg_image.save(png_buffer, format="PNG")
png_bytes = png_buffer.getvalue()

# Use png_bytes in your messages structure
messages = [
    {
        "role": "user",
        "content": [
            {"text": "Describe image"},
            {
                "image": {
                    "format": "png",
                    "source": {"bytes": png_bytes},
                }
            },
        ],
    }
]

This approach allows you to use JPEG images with the Llama 3.2 90B Vision model by converting them to PNG format before sending them to the API. Remember to keep the "format": "png" in your message structure, as the model expects PNG input.
Sources
Introducing Llama 3.2 models from Meta in Amazon Bedrock: A new generation of multimodal vision and lightweight models | AWS News Blog
Vision use cases with Llama 3.2 11B and 90B models from Meta | AWS Machine Learning Blog

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Why is something like this not included in the documentation or mentioned anywhere?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.