View a markdown version of this page

Configuration - AWS SDK for Python v4

Developer Preview: This documentation covers the AWS SDK for Python v4, which is in Developer Preview and intended for evaluation and testing only. Do not use it for production workloads. For production applications, use the AWS SDK for Python (Boto3). To understand the differences between the two SDKs, see Choosing the right AWS SDK for Python.

Configuration

The AWS SDK for Python v4 resolves client configuration from values that you pass in code, environment variables, and the shared AWS config files. If you create a client without a configuration (client = BedrockRuntimeClient()), the client resolves its configuration from your environment the first time you call an operation.

Prerequisites

Before you use the SDK, configure the following:

  • An AWS Region. Set it with the AWS_REGION environment variable, in your ~/.aws/config file, or in code. The Region is required and has no default.

  • AWS credentials from a supported credential source. For more information, see Credential providers.

Quick start

The following example creates a client without a configuration and sends a request.

from aws_sdk_bedrock_runtime import BedrockRuntimeClient from aws_sdk_bedrock_runtime.models import ( ContentBlockText, ConverseOutputMessage, ) def main() -> None: with BedrockRuntimeClient() as client: response = client.converse( model_id="global.amazon.nova-2-lite-v1:0", messages=[ {"role": "user", "content": [{"text": "What is 2 + 2?"}]} ], ) if isinstance(response.output, ConverseOutputMessage): for block in response.output.value.content: if isinstance(block, ContentBlockText): print(block.value) if __name__ == "__main__": main()

This example assumes that you set the Region with AWS_REGION or the region setting in your config file, and that the default credential resolver chain can find credentials. The with statement closes the client when the block ends.

Explicit configuration

Instead of letting the client resolve its configuration on the first call, you can resolve the configuration yourself and pass it to the client. Explicit resolution lets you override specific values and validate the configuration before you call an operation. Call resolve() on the config class for the service. For an asynchronous client, use the Async config class and await resolve().

Synchronous client
from aws_sdk_bedrock_runtime import BedrockRuntimeClient, BedrockRuntimeConfig def create_client() -> BedrockRuntimeClient: config = BedrockRuntimeConfig.resolve(region="us-west-2", max_attempts=5) return BedrockRuntimeClient(config=config)
Asynchronous client
from aws_sdk_bedrock_runtime import AsyncBedrockRuntimeClient, AsyncBedrockRuntimeConfig async def create_client() -> AsyncBedrockRuntimeClient: config = await AsyncBedrockRuntimeConfig.resolve( region="us-west-2", max_attempts=5 ) return AsyncBedrockRuntimeClient(config=config)

The synchronous and asynchronous config classes have the same fields and resolve them the same way. For fields that take a component, such as an HTTP client or a credential resolver, use components that match the client mode. The rest of this chapter uses synchronous examples. For more information about the two client types, see Synchronous and asynchronous clients. For details about resolution, see Configuration resolution.

Next steps