Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation Exception when calling the invoke_model function with Amazon Nova #4467

Closed
1 task
changchen96 opened this issue Mar 10, 2025 · 3 comments
Closed
1 task
Labels
bug This issue is a confirmed bug. needs-triage This issue or PR still needs to be triaged.

Comments

@changchen96
Copy link

changchen96 commented Mar 10, 2025

Describe the bug

I've hit an ValidationException error when trying to call the invoke_model function using boto3 and here's the code that I'm using currently:

with open(video_filepath, "rb") as video_file:
        base64_text = base64.b64encode(video_file.read()).decode("utf-8")
        try:
            bedrock = boto3.client(service_name="bedrock-runtime", region_name=os.getenv('BEDROCK_REGION'))
            body = [{
                "role": "user", 
                "content": [
                    {
                        "text": "Can you get me the title for this video?"
                    },
                    {
                        "video": {
                            "format": "mp4",
                            "bytes": base64_text
                        }
                    }
                ]
            }]

            inf_params = {"maxTokens": 300, "topP": 0.9, "temperature": 0.7}

            modelId = 'us.amazon.nova-lite-v1:0'
            request_body = {
                "schemaVersion": "messages-v1",
                "messages": body,
                "inferenceConfig": inf_params,
            }
            response = bedrock.invoke_model(modelId=modelId, body=json.dumps(request_body) )
            print("response here")
            print(response)
            response_body = json.loads(response.get("body").read())
            return response_body.get("content")
        
        except Exception as e:
            print(e)
            exit()
            logger.info("Claude Error:")
            logger.info("Error: %s", e)

This example is based on the documentation provided in the Amazon Nova documentation.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

The code returns the response from the Nova model without any errors returned.

Current Behavior

This error will be returned instead

An error occurred (ValidationException) when calling the InvokeModel operation: Malformed input request: #/messages/0/content/1: required key [toolUse] not found, please reformat your input and try again.

Reproduction Steps

with open(video_filepath, "rb") as video_file:
        base64_text = base64.b64encode(video_file.read()).decode("utf-8")
        try:
            bedrock = boto3.client(service_name="bedrock-runtime", region_name=os.getenv('BEDROCK_REGION'))
            body = [{
                "role": "user", 
                "content": [
                    {
                        "text": "Can you get me the title for this video?"
                    },
                    {
                        "video": {
                            "format": "mp4",
                            "bytes": base64_text
                        }
                    }
                ]
            }]

            inf_params = {"maxTokens": 300, "topP": 0.9, "temperature": 0.7}

            modelId = 'us.amazon.nova-lite-v1:0'
            request_body = {
                "schemaVersion": "messages-v1",
                "messages": body,
                "inferenceConfig": inf_params,
            }
            response = bedrock.invoke_model(modelId=modelId, body=json.dumps(request_body) )
            print("response here")
            print(response)
            response_body = json.loads(response.get("body").read())
            return response_body.get("content")
        
        except Exception as e:
            print(e)
            exit()
            logger.info("Claude Error:")
            logger.info("Error: %s", e)

Another way to replicate the error is found here, but in this example it's uploading from S3 instead:

https://stackoverflow.com/questions/79364187/validationexception-malformed-input-request-when-using-invokemodel-with-amazon

Possible Solution

No response

Additional Information/Context

No response

SDK version used

1.34.62 (EDIT: Updated to 1.37.9, still facing the same issue)

Environment details (OS name and version, etc.)

Amazon Linux 2

@changchen96 changchen96 added bug This issue is a confirmed bug. needs-triage This issue or PR still needs to be triaged. labels Mar 10, 2025
@ken11
Copy link

ken11 commented Mar 15, 2025

The error message

An error occurred (ValidationException) when calling the InvokeModel operation: Malformed input request: #/messages/0/content/1: required key [toolUse] not found, please reformat your input and try again.

is indeed confusing. In reality, the issue is not with a missing toolUse key, but rather with the incorrect structure of the request body.

Currently, the body being sent is:

"video": {
    "format": "mp4",
    "bytes": base64_text
}

However, as specified in the documentation, it should be formatted as

"video": {
    "format": "mp4",
    "source": { "bytes": base64_string }
}

The problem is that the source key is missing. Correcting this should resolve the issue.

Please note that this error message is generated by the endpoint itself, which means it is not specific to boto3; similar issues would occur with libraries in other languages as well.

Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

@changchen96
Copy link
Author

I had a look at the documentation again and have resolved the issue, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a confirmed bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

2 participants