python/index.md +53 −8
928 928
929## Amazon Bedrock929## Amazon Bedrock
930 930
931931To use this library with [Amazon Bedrock's OpenAI-compatible API](https://docs.aws.amazon.com/bedrock/latest/userguide/models-api-compatibility.html), use the `BedrockOpenAI` class instead of the `OpenAI` class.To use this library with [Amazon Bedrock's OpenAI-compatible API](https://docs.aws.amazon.com/bedrock/latest/userguide/models-api-compatibility.html), configure the standard `OpenAI` client with the Bedrock provider.
932
933Install the optional Bedrock dependencies to use the standard AWS credential chain and SigV4 authentication:
934
935```sh
936pip install 'openai[bedrock]'
937```
932 938
933```py939```py
934940from openai import BedrockOpenAIfrom openai import OpenAI
941from openai.providers import bedrock
935 942
936943# gets the bearer token from AWS_BEARER_TOKEN_BEDROCK and the region from AWS_REGION/AWS_DEFAULT_REGION# Uses your normal AWS credentials. You can omit region when it is
937944client = BedrockOpenAI()# configured through AWS_REGION, AWS_DEFAULT_REGION, or your AWS profile.
945client = OpenAI(
946 provider=bedrock(
947 region="us-west-2",
948 )
949)
938 950
939response = client.responses.create(951response = client.responses.create(
940 model="openai.gpt-5.4",952 model="openai.gpt-5.4",
944print(response.output_text)956print(response.output_text)
945```957```
946 958
947959`BedrockOpenAI` configures AWS bearer auth and the Bedrock Mantle endpoint, then uses the normal SDK resources. AWS controls which endpoints and features are supported; unsupported calls surface the provider's normal HTTP errors through the SDK.The provider configures AWS authentication and the Bedrock Mantle endpoint while retaining the normal SDK resources, retries, streaming, and error handling. AWS controls which endpoints and features are supported; unsupported calls surface the provider's normal HTTP errors through the SDK.
960
961The default AWS credential chain supports environment credentials, shared credentials and config files, named profiles, SSO and assume-role profiles, and workload credentials such as ECS, EKS, and EC2 metadata. To select a named profile:
962
963```py
964client = OpenAI(
965 provider=bedrock(
966 profile="my-profile",
967 )
968)
969```
970
971You can also pass `access_key_id` and `secret_access_key`, with an optional `session_token`, or a refreshable `credential_provider` that returns botocore-compatible credentials. Explicit bearer and AWS credential options are mutually exclusive.
948 972
949973Pass `base_url` or set `AWS_BEDROCK_BASE_URL` to override the derived `https://bedrock-mantle.<region>.api.aws/openai/v1` endpoint. The legacy module client supports `openai.api_type = "amazon-bedrock"` or `OPENAI_API_TYPE=amazon-bedrock`.Pass `base_url` to `bedrock(...)` or set `AWS_BEDROCK_BASE_URL` to override the derived `https://bedrock-mantle.<region>.api.aws/openai/v1` endpoint.
950 974
951975Set `AWS_BEARER_TOKEN_BEDROCK` to an [Amazon Bedrock API key](https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys.html). To refresh tokens yourself, pass a provider instead of `api_key`:SigV4 requests require replayable, fully serialized request bodies. Standard JSON requests already meet this requirement, and response streaming is unaffected. Low-level one-shot request streams must be buffered before sending, or sent with bearer authentication and retries disabled.
976
977Bearer tokens remain available as a compatibility or manual authentication mode. Set `AWS_BEARER_TOKEN_BEDROCK` to an [Amazon Bedrock API key](https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys.html), pass `api_key`, or provide a refresh callback:
952 978
953```py979```py
980client = OpenAI(
981 provider=bedrock(
982 region="us-west-2",
983 token_provider=lambda: refresh_bedrock_token(),
984 )
985)
986```
987
988Without explicit authentication, `AWS_BEARER_TOKEN_BEDROCK` takes precedence over the default AWS credential chain for backwards compatibility.
989
990### Legacy `BedrockOpenAI` client
991
992`BedrockOpenAI` and `AsyncBedrockOpenAI` remain available for existing applications and delegate to the same provider implementation. New applications should prefer `OpenAI(provider=bedrock(...))`.
993
994```py
995from openai import BedrockOpenAI
996
954client = BedrockOpenAI(997client = BedrockOpenAI(
955 aws_region="us-west-2",998 aws_region="us-west-2",
956999 bedrock_token_provider=lambda: refresh_bedrock_token(), aws_profile="my-profile",
957)1000)
958```1001```
959 1002
1003The legacy module client also continues to support `openai.api_type = "amazon-bedrock"` or `OPENAI_API_TYPE=amazon-bedrock`.
1004
960## Versioning1005## Versioning
961 1006
962This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions:1007This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions: