ValidationException
botocore.exceptions.ValidationException
Stack trace
botocore.exceptions.ValidationException: An error occurred (ValidationException) when calling the InvokeModel operation: Request body is invalid
Why it happens
AWS Bedrock expects the request body to strictly follow the API schema, including required parameters and correct JSON structure. If the request body is missing required fields, contains invalid JSON, or has incorrect parameter types, the service returns a ValidationException indicating the request body is invalid.
Detection
Enable detailed logging of the request payload before sending to AWS Bedrock and validate JSON structure and required fields programmatically to catch errors early.
Causes & fixes
Request body JSON is malformed or not valid JSON
Ensure the request body is properly serialized JSON using json.dumps() and validate JSON syntax before sending.
Missing required parameters in the request body as per AWS Bedrock API specification
Add all required parameters exactly as specified in the AWS Bedrock API documentation to the request body.
Incorrect data types or unexpected fields in the request body
Verify that all fields match the expected data types and remove any unsupported or extra fields.
Using incorrect API method or parameters incompatible with the chosen AWS Bedrock model
Confirm you are calling the correct API method with parameters supported by the model and AWS Bedrock version.
Code: broken vs fixed
import boto3
client = boto3.client('bedrock')
# Missing required 'body' parameter or malformed JSON
response = client.invoke_model(ModelId='my-model', Body='invalid-json') # This line triggers ValidationException
print(response) import os
import json
import boto3
client = boto3.client('bedrock',
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
region_name=os.environ.get('AWS_REGION', 'us-east-1'))
request_body = {
"inputText": "Hello, Bedrock!"
}
response = client.invoke_model(
ModelId='my-model',
Body=json.dumps(request_body) # Fixed: properly serialized JSON with required fields
)
print(response) Workaround
Catch ValidationException and log the raw request body for manual inspection; optionally, use a JSON schema validator locally before sending requests to Bedrock.
Prevention
Implement strict request body validation using JSON schemas or data models before sending requests to AWS Bedrock to ensure compliance with API requirements and avoid malformed requests.