To use BedrockConverseModel, you need to either install pydantic-ai, or install pydantic-ai-slim with the bedrock optional group:
pipinstall"pydantic-ai-slim[bedrock]"
uvadd"pydantic-ai-slim[bedrock]"
Configuration
To use AWS Bedrock, you'll need an AWS account with Bedrock enabled and appropriate credentials. You can use either AWS credentials directly or a pre-configured boto3 client.
BedrockModelName contains a list of available Bedrock models, including models from Anthropic, Amazon, Cohere, Meta, and Mistral.
Environment variables
You can set your AWS credentials as environment variables (among other options):
exportAWS_ACCESS_KEY_ID='your-access-key'exportAWS_SECRET_ACCESS_KEY='your-secret-key'exportAWS_DEFAULT_REGION='us-east-1'# or your preferred region
frompydantic_aiimportAgentfrompydantic_ai.models.bedrockimportBedrockConverseModel,BedrockModelSettings# Define Bedrock model settings with guardrail and performance configurationsbedrock_model_settings=BedrockModelSettings(bedrock_guardrail_config={'guardrailIdentifier':'v1','guardrailVersion':'v1','trace':'enabled'},bedrock_performance_configuration={'latency':'optimized'})model=BedrockConverseModel(model_name='us.amazon.nova-pro-v1:0')agent=Agent(model=model,model_settings=bedrock_model_settings)
provider argument
You can provide a custom BedrockProvider via the provider argument. This is useful when you want to specify credentials directly or use a custom boto3 client:
frompydantic_aiimportAgentfrompydantic_ai.models.bedrockimportBedrockConverseModelfrompydantic_ai.providers.bedrockimportBedrockProvider# Using AWS credentials directlymodel=BedrockConverseModel('anthropic.claude-3-sonnet-20240229-v1:0',provider=BedrockProvider(region_name='us-east-1',aws_access_key_id='your-access-key',aws_secret_access_key='your-secret-key',),)agent=Agent(model)...
You can also pass a pre-configured boto3 client:
importboto3frompydantic_aiimportAgentfrompydantic_ai.models.bedrockimportBedrockConverseModelfrompydantic_ai.providers.bedrockimportBedrockProvider# Using a pre-configured boto3 clientbedrock_client=boto3.client('bedrock-runtime',region_name='us-east-1')model=BedrockConverseModel('anthropic.claude-3-sonnet-20240229-v1:0',provider=BedrockProvider(bedrock_client=bedrock_client),)agent=Agent(model)...