How to intermediate · 3 min read

How to use async with AWS Bedrock

Quick answer
Use Python's asyncio with boto3 by running bedrock-runtime client calls in an executor or thread pool since boto3 is synchronous. Alternatively, use aioboto3 to create an async client and call converse asynchronously for AWS Bedrock AI model inference.

PREREQUISITES

  • Python 3.8+
  • AWS credentials configured (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY)
  • pip install boto3 aioboto3

Setup

Install the required packages and configure AWS credentials for authentication. You need boto3 and aioboto3 for async support.

  • Install packages: pip install boto3 aioboto3
  • Configure AWS credentials via ~/.aws/credentials or environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
bash
pip install boto3 aioboto3

Step by step

This example demonstrates how to perform an asynchronous call to AWS Bedrock's converse API using aioboto3. It sends a user message to an Anthropic Claude model and prints the response.

python
import asyncio
import aioboto3
import json

async def async_bedrock_converse():
    session = aioboto3.Session()
    async with session.client('bedrock-runtime', region_name='us-east-1') as client:
        messages = [{"role": "user", "content": [{"type": "text", "text": "Hello from async AWS Bedrock!"}]}]
        response = await client.converse(
            modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
            messages=messages
        )
        # Extract text from response
        content = response['output']['message']['content'][0]['text']
        print("Response:", content)

if __name__ == '__main__':
    asyncio.run(async_bedrock_converse())
output
Response: Hello from async AWS Bedrock! How can I assist you today?

Common variations

You can use synchronous boto3 calls wrapped in asyncio.to_thread() if you prefer not to use aioboto3. Also, you can change modelId to other Bedrock models like amazon.titan-text-express-v1 or meta.llama3-1-70b-instruct-v1:0. For streaming responses, AWS Bedrock currently does not support streaming via converse.

python
import asyncio
import boto3

client = boto3.client('bedrock-runtime', region_name='us-east-1')

async def sync_call_in_thread():
    messages = [{"role": "user", "content": [{"type": "text", "text": "Hello from sync boto3 in async!"}]}]
    response = await asyncio.to_thread(
        client.converse,
        modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
        messages=messages
    )
    content = response['output']['message']['content'][0]['text']
    print("Response:", content)

if __name__ == '__main__':
    asyncio.run(sync_call_in_thread())
output
Response: Hello from sync boto3 in async! How can I help you?

Troubleshooting

  • If you get botocore.exceptions.NoCredentialsError, ensure AWS credentials are properly configured in environment variables or AWS config files.
  • If aioboto3 client fails to connect, verify your AWS region and network connectivity.
  • For asyncio.run() errors, ensure you are running Python 3.7+ and not inside an existing event loop (e.g., Jupyter). Use alternative event loop management if needed.

Key Takeaways

  • Use aioboto3 to create an async AWS Bedrock client for true asynchronous calls.
  • Alternatively, wrap synchronous boto3 calls in asyncio.to_thread() to avoid blocking.
  • Always configure AWS credentials and region correctly to avoid authentication errors.
Verified 2026-04 · anthropic.claude-3-5-sonnet-20241022-v2:0, amazon.titan-text-express-v1, meta.llama3-1-70b-instruct-v1:0
Verify ↗