Skip to content

Send images as Base64 files to AWS BedRock Agent

1

Hi There.

Is there a way I can send image files as base64 or s3 repository link to a BedRock Agent using InvokeAgent API?

I've noted that in the syntax we have this part:

response = client.invoke_agent(
    agentAliasId='string',
    agentId='string',
    enableTrace=True|False,
    endSession=True|False,
    inputText='string',
    memoryId='string',
    sessionId='string',
    sessionState={
        'files': [
            {
                'name': 'string',
                'source': {
                    'byteContent': {
                        'data': b'bytes',
                        'mediaType': 'string'
                    },
                    's3Location': {
                        'uri': 'string'
                    },
                    'sourceType': 'S3'|'BYTE_CONTENT'
                },
                'useCase': 'CODE_INTERPRETER'|'CHAT'
            },
        ],

Unfortunately I didn't find any documents or examples on how to fill these parts.

I've tried passing a base64 image in

 'files': [
            {
                'name': 'uploaded_picture',
                'source': {
                    'byteContent': {
                        'data': b'{base64}',
                        'mediaType': 'image/jpeg'
                    },

                    'sourceType': 'BYTE_CONTENT'
                },
                'useCase': 'CHAT'
            },
        ],

But Bedrock returned me the error:

ParamValidationError: Parameter validation failed:
Unknown parameter in sessionState: "files", must be one of: sessionAttributes, promptSessionAttributes

Has someone tried to do that and could help?

Here is my full python code:

import boto3

# Initialize the boto3 client
agents_runtime_client = boto3.client('bedrock-agent-runtime', region_name='sa-east-1')


def invoke_agent(agent_id, agent_alias_id, session_id, prompt, image_path = None):
    """
    Sends a prompt for the agent to process and respond to.

    :param agent_id: The unique identifier of the agent to use.
    :param agent_alias_id: The alias of the agent to use.
    :param session_id: The unique identifier of the session. Use the same value across requests
                       to continue the same conversation.
    :param prompt: The prompt that you want Claude to complete.
    :return: Inference response from the model.
    """

    try:
        
        
        image_data = None
        if image_path:
            with open(image_path, "rb") as image_file:
                image_data = base64.b64encode(image_file.read()).decode('utf-8')

        # Note: The execution time depends on the foundation model, complexity of the agent,
        # and the length of the prompt. In some cases, it can take up to a minute or more to
        # generate a response.
        response = agents_runtime_client.invoke_agent(
            agentId=agent_id,
            agentAliasId=agent_alias_id,
            sessionId=session_id,
            inputText=input_text,
            sessionState={
            'files': [
            {
                'name': 'picture',
                'source': {
                    'byteContent': {
                        'data': image_data,
                        'mediaType': 'image/jpeg'
                    },
                    'sourceType': 'BYTE_CONTENT'
                },
                'useCase': 'CHAT'
            }
        ]
            }
        )
            

        completion = ""

        for event in response.get("completion"):
            chunk = event["chunk"]
            completion = completion + chunk["bytes"].decode()

    except ClientError as e:
        logger.error(f"Couldn't invoke agent. {e}")
        raise

    return completion


# Define the chat parameters
agent_alias_id = "XXXXX"
agent_id = "XXXXX"
session_id = str(uuid.uuid4())
input_text = "Hello"

asked 2 years ago5.1K views

6 Answers
1

Same issue here! Did anyone have any luck passing an image to the bedrock agent?

LE: I've read through the documentation like a thousand times, and all references to the sessionState dict point to code interpretation type of usecase

Contains information about the files used by code interpreter

As such I don't think that's the path fwd to be including an image in the prompt for an agent. I'm trying now a different path, whereby I ask the cloude3 model ( via the model API not agent API ) to describe the image with some prompt engineering, and then pass that description into the original prompt.

If anything changes in the agent API to allow this to be better streamlined, please let us know.

answered 2 years ago

1

It looks like your request is consistent with the API doc and it's that specific files parameter that's failing validation, so I'd suggest to double-check whether you're running the most up-to-date version of boto3 and botocore? Seems like the most likely cause would be an old boto version not supporting that parameter yet.

Generally, I believe you should be on the right approach for passing in the base64 bytes.

AWS
EXPERT

answered 2 years ago

  • Thank you. That was indeed a problem. Now I upgraded my boto3 library and it stopped sending that error, replacing it for a new one: "EventStreamError: An error occurred (validationException) when calling the InvokeAgent operation: The overridden prompt that you provided is incorrectly formatted. Check the format for errors, such as invalid JSON, and retry your request."

    This error is the same one of when I try to upload an image directly into the Bedrock Agent Test Window on AWS Bedrock UI

0

As suggested by @Alex_T, I updated my boto3 library and that solved the first error I encountered. Now I got a new error about problem with my prompt or file format... Been thinking if Amazon Agent can really receive images, cause there's nothing on the Documentation that firmly says it.

The script is now like this:


def invoke_agent(agent_id, agent_alias_id, session_id, input_text, image_path = None):
    """
    Sends a prompt for the agent to process and respond to.

    :param agent_id: The unique identifier of the agent to use.
    :param agent_alias_id: The alias of the agent to use.
    :param session_id: The unique identifier of the session. Use the same value across requests
                       to continue the same conversation.
    :param prompt: The prompt that you want Claude to complete.
    :return: Inference response from the model.
    """

    try:
        
        
        image_data = None
        if image_path:
            with open(image_path, "rb") as image_file:
                image_data = base64.b64encode(image_file.read()).decode('utf-8')

        # Note: The execution time depends on the foundation model, complexity of the agent,
        # and the length of the prompt. In some cases, it can take up to a minute or more to
        # generate a response.
        response = agents_runtime_client.invoke_agent(
            agentId=agent_id,
            agentAliasId=agent_alias_id,
            sessionId=session_id,
            inputText=input_text,
            sessionState={
            'files': [
            {
                'name': 'picture',
                'source': {
                    'byteContent': {
                        'data': f"b'{image_data}'",
                        'mediaType': 'image/jpeg'
                    },
                    'sourceType': 'BYTE_CONTENT'
                },
                'useCase': 'CHAT'
            }
        ]
            }
        )
            

        completion = ""

        for event in response.get("completion"):
            chunk = event["chunk"]
            completion = completion + chunk["bytes"].decode()

    except Exception as e:
        print(f"Couldn't invoke agent. {e}")
        raise

    return completion


# Define the chat parameters
agent_alias_id = "XXXXXXXX"
agent_id = "XXXXXXX"
session_id = 'XXXXXXX'
input_text = "Hello ma friend"

But now the error is:

EventStreamError: An error occurred (validationException) when calling the InvokeAgent operation: The overridden prompt that you provided is incorrectly formatted. Check the format for errors, such as invalid JSON, and retry your request.

And it's the same error when I try to upload a file directly into the Agent Test UI in Bedrock, like in the print below:

answered 2 years ago

0

Does someone have a clue on that?

answered 2 years ago

0

Has anyone found a solution? I am getting the same error "Check the format for errors, such as invalid JSON," Using the latest boto3 & BotoCore: 1.36.13

answered 2 years ago

0

You can't currently send image files to a Bedrock Agent. It can take other file types per the code above (ex. XLS, DOC, PDF) but internally the Bedrock Agent framework extracts text and passes it to the LLM model. Even though the model itself may be visually capable, it can't receive images within the context of a Bedrock Agent - for example, if you save a .png file as a .pdf and upload it, the Agent will say it doesn't see anything in the file. To process images, you'll need to use the model through Bedrock directly, for example via the Converse API or the Invoke API.

answered a year ago

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.