How to use Bedrock invoke_model API
Quick answer
Use the
boto3 client for bedrock-runtime to call invoke_model with the model ID and messages formatted as JSON. This method sends chat or text inputs to Bedrock models and returns the generated output in the response.PREREQUISITES
Python 3.8+AWS account with Bedrock accessAWS credentials configured (~/.aws/credentials or environment variables)pip install boto3
Setup
Install the boto3 library and configure your AWS credentials with Bedrock permissions. Ensure you have access to Bedrock models and the bedrock-runtime client is available in your AWS SDK.
pip install boto3 Step by step
This example shows how to invoke a Bedrock model using boto3 with the invoke_model API. It sends a chat message and prints the model's response.
import os
import json
import boto3
# Initialize the Bedrock runtime client
client = boto3.client('bedrock-runtime', region_name='us-east-1')
# Define the model ID for the Bedrock model you want to use
model_id = 'anthropic.claude-3-5-sonnet-20241022-v2:0'
# Prepare the chat messages payload
messages = [
{"role": "user", "content": [{"type": "text", "text": "Hello from Bedrock!"}]}
]
# Construct the request body as JSON string
body = json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 512,
"messages": messages
})
# Call invoke_model API
response = client.invoke_model(
modelId=model_id,
body=body
)
# Parse the response body
response_body = json.loads(response['body'].read())
# Extract the generated text
output_text = response_body['output']['message']['content'][0]['text']
print("Model response:", output_text) output
Model response: Hello from Bedrock! How can I assist you today?
Common variations
- Change
modelIdto use other Bedrock models likeamazon.titan-text-express-v1ormeta.llama3-1-70b-instruct-v1:0. - Adjust
max_tokensand other parameters in the JSONbodyto control output length and behavior. - Use
client.converse()for simpler chat interactions if supported. - Set
region_nameinboto3.clientto your Bedrock region.
Troubleshooting
- If you get
AccessDeniedException, verify your AWS IAM permissions include Bedrock access. - For
ModelNotFoundException, confirm themodelIdis correct and available in your region. - If
response['body']is empty or malformed, check network connectivity and AWS SDK version. - Ensure your AWS credentials are properly configured in environment variables or AWS config files.
Key Takeaways
- Use boto3's bedrock-runtime client with invoke_model to call Bedrock AI models.
- Format the request body as a JSON string with messages and parameters.
- Check AWS IAM permissions and model IDs if you encounter errors.