|
| 1 | +# Amazon Bedrock |
| 2 | + |
| 3 | +The Bedrock provider connects the standard synchronous and asynchronous OpenAI clients to Amazon Bedrock's |
| 4 | +OpenAI-compatible endpoints. The provider supports bearer tokens without additional dependencies. AWS Signature |
| 5 | +Version 4 (SigV4) requires the optional Bedrock dependencies: |
| 6 | + |
| 7 | +```sh |
| 8 | +pip install 'openai[bedrock]' |
| 9 | +``` |
| 10 | + |
| 11 | +Runtime endpoint selection requires a Python SDK release that includes SDK-290. The SDK supports Python 3.10 and newer. |
| 12 | + |
| 13 | +## Endpoint selection |
| 14 | + |
| 15 | +| `endpoint` | Default API root | SigV4 signing service | |
| 16 | +| --- | --- | --- | |
| 17 | +| `"mantle"` (default) | `https://bedrock-mantle.<region>.api.aws/openai/v1` | `bedrock-mantle` | |
| 18 | +| `"runtime"` | `https://bedrock-runtime.<region>.amazonaws.com/openai/v1` | `bedrock` | |
| 19 | + |
| 20 | +Runtime hostnames use the DNS suffix for the selected AWS partition. For example, the European sovereign region |
| 21 | +`eusc-de-east-1` uses `amazonaws.eu`. Canonical Runtime FIPS and dual-stack hostnames are also recognized. |
| 22 | + |
| 23 | +The region comes from `region`, `AWS_REGION`, `AWS_DEFAULT_REGION`, or, for AWS authentication, the selected AWS profile. |
| 24 | +Pass `base_url` or set `AWS_BEDROCK_BASE_URL` to override the derived API root. When `endpoint` is omitted, canonical |
| 25 | +Mantle and Runtime URLs select the corresponding endpoint and signing service automatically; otherwise Mantle remains |
| 26 | +the default. Custom or proxy URLs likewise use Mantle signing by default; pass `endpoint="runtime"` when a custom host |
| 27 | +requires Runtime signing. |
| 28 | + |
| 29 | +## Runtime Chat Completions |
| 30 | + |
| 31 | +Use an inference-profile ID such as `us.openai.gpt-5.6-sol`, `us.openai.gpt-5.6-terra`, or |
| 32 | +`us.openai.gpt-5.6-luna`. These deployments do not accept the corresponding bare model ID. Global inference profiles, |
| 33 | +such as `global.openai.gpt-5.6-sol`, require an AWS account and permissions that allow the corresponding profile. |
| 34 | + |
| 35 | +```python |
| 36 | +from openai import OpenAI |
| 37 | +from openai.providers import bedrock |
| 38 | + |
| 39 | +client = OpenAI( |
| 40 | + provider=bedrock( |
| 41 | + endpoint="runtime", |
| 42 | + region="us-west-2", |
| 43 | + api_key=None, |
| 44 | + ) |
| 45 | +) |
| 46 | + |
| 47 | +completion = client.chat.completions.create( |
| 48 | + model="us.openai.gpt-5.6-sol", |
| 49 | + messages=[{"role": "user", "content": "Say hello!"}], |
| 50 | +) |
| 51 | +print(completion.choices[0].message.content) |
| 52 | +``` |
| 53 | + |
| 54 | +`api_key=None` prevents `AWS_BEARER_TOKEN_BEDROCK` from shadowing AWS credentials and forces SigV4 authentication. Omit |
| 55 | +`region` to use the normal environment or AWS profile region chain. |
| 56 | + |
| 57 | +For streaming, set `stream=True`: |
| 58 | + |
| 59 | +```python |
| 60 | +stream = client.chat.completions.create( |
| 61 | + model="us.openai.gpt-5.6-sol", |
| 62 | + messages=[{"role": "user", "content": "Say hello!"}], |
| 63 | + stream=True, |
| 64 | +) |
| 65 | +for chunk in stream: |
| 66 | + print(chunk.choices[0].delta.content or "", end="", flush=True) |
| 67 | +``` |
| 68 | + |
| 69 | +The asynchronous client uses the same provider configuration: |
| 70 | + |
| 71 | +```python |
| 72 | +from openai import AsyncOpenAI |
| 73 | + |
| 74 | +client = AsyncOpenAI(provider=bedrock(endpoint="runtime", region="us-west-2", api_key=None)) |
| 75 | +completion = await client.chat.completions.create( |
| 76 | + model="us.openai.gpt-5.6-sol", |
| 77 | + messages=[{"role": "user", "content": "Say hello!"}], |
| 78 | +) |
| 79 | +``` |
| 80 | + |
| 81 | +See [`examples/bedrock_runtime.py`](examples/bedrock_runtime.py) for a runnable example supporting SigV4, bearer |
| 82 | +authentication, model selection, AWS profiles, and opt-in streaming. |
| 83 | + |
| 84 | +## Authentication |
| 85 | + |
| 86 | +Authentication is chosen in this order: |
| 87 | + |
| 88 | +1. Explicit bearer credentials, static AWS credentials, a named profile, or an AWS credential provider. |
| 89 | +2. The bearer token in `AWS_BEARER_TOKEN_BEDROCK`, unless `api_key=None` disables this fallback. |
| 90 | +3. The default AWS credential chain. |
| 91 | + |
| 92 | +Explicit bearer and AWS credential modes cannot be combined. A stale environment bearer token takes precedence over the |
| 93 | +implicit AWS credential chain; unset it or pass `api_key=None` when SigV4 is required. |
| 94 | + |
| 95 | +### Bearer credentials |
| 96 | + |
| 97 | +```python |
| 98 | +client = OpenAI( |
| 99 | + provider=bedrock( |
| 100 | + endpoint="runtime", |
| 101 | + region="us-west-2", |
| 102 | + api_key="your-bedrock-api-key", |
| 103 | + ) |
| 104 | +) |
| 105 | +``` |
| 106 | + |
| 107 | +A callable token provider is invoked before every request attempt, including retries. `AsyncOpenAI` also accepts an |
| 108 | +asynchronous callable: |
| 109 | + |
| 110 | +```python |
| 111 | +client = AsyncOpenAI( |
| 112 | + provider=bedrock( |
| 113 | + endpoint="runtime", |
| 114 | + region="us-west-2", |
| 115 | + token_provider=refresh_bedrock_token, |
| 116 | + ) |
| 117 | +) |
| 118 | +``` |
| 119 | + |
| 120 | +### AWS credentials and profiles |
| 121 | + |
| 122 | +Use the default AWS credential chain or select a named shared-config profile: |
| 123 | + |
| 124 | +```python |
| 125 | +client = OpenAI( |
| 126 | + provider=bedrock( |
| 127 | + endpoint="runtime", |
| 128 | + profile="my-aws-profile", |
| 129 | + api_key=None, |
| 130 | + ) |
| 131 | +) |
| 132 | +``` |
| 133 | + |
| 134 | +Temporary static credentials can include a session token: |
| 135 | + |
| 136 | +```python |
| 137 | +client = OpenAI( |
| 138 | + provider=bedrock( |
| 139 | + endpoint="runtime", |
| 140 | + region="us-west-2", |
| 141 | + access_key_id="your-access-key", |
| 142 | + secret_access_key="your-secret-key", |
| 143 | + session_token="your-session-token", |
| 144 | + ) |
| 145 | +) |
| 146 | +``` |
| 147 | + |
| 148 | +Pass `credential_provider` for refreshing AWS credentials. The provider is called for every signed request attempt. |
| 149 | +Signed requests require replayable request bodies and do not automatically follow redirects. |
| 150 | + |
| 151 | +## API routes and support limitations |
| 152 | + |
| 153 | +The SDK defaults to `/openai/v1`, matching |
| 154 | +[AWS's OpenAI model documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-openai.html). |
| 155 | +[AWS's Chat Completions documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/inference-chat-completions-mantle.html) |
| 156 | +also describes a `/v1` Runtime route. Override `base_url` when a deployment requires that route: |
| 157 | + |
| 158 | +```python |
| 159 | +client = OpenAI( |
| 160 | + provider=bedrock( |
| 161 | + endpoint="runtime", |
| 162 | + region="us-west-2", |
| 163 | + base_url="https://bedrock-runtime.us-west-2.amazonaws.com/v1", |
| 164 | + api_key=None, |
| 165 | + ) |
| 166 | +) |
| 167 | +``` |
| 168 | + |
| 169 | +The provider exposes normal Chat Completions and Responses resources, but AWS determines which routes, models, |
| 170 | +inference profiles, authentication methods, and streaming features each deployment accepts. Runtime Responses and |
| 171 | +streaming should be live-validated for the selected model, profile, route, authentication mode, and AWS account. |
| 172 | + |
| 173 | +Canonical AWS endpoints must use HTTPS and match the configured endpoint family and region. Bedrock credentials are |
| 174 | +never attached to a request whose origin differs from the configured API root. Explicitly configured custom or local |
| 175 | +HTTP proxies remain available when required; use them only inside a trusted environment. |
| 176 | + |
| 177 | +## Opt-in live verification |
| 178 | + |
| 179 | +The existing live harness requires explicit opt-in and valid AWS credentials: |
| 180 | + |
| 181 | +```sh |
| 182 | +BEDROCK_LIVE_TEST=1 BEDROCK_LIVE_ENDPOINT=runtime AWS_REGION=us-west-2 \ |
| 183 | + rye run pytest -q -s tests/lib/bedrock_live.py |
| 184 | +``` |
| 185 | + |
| 186 | +Runtime verification defaults to all three US GPT-5.6 inference profiles. Select authentication modes with |
| 187 | +`BEDROCK_LIVE_AUTHS=bearer,environment-bearer,token-provider,default-chain,profile,static`; select specific models with |
| 188 | +`BEDROCK_LIVE_MODELS`. Set `BEDROCK_LIVE_STREAM=1` to include streaming and `BEDROCK_LIVE_RESPONSES=1` to include |
| 189 | +Runtime Responses. Provide `AWS_PROFILE` or `BEDROCK_LIVE_PROFILE` for named-profile verification. |
0 commit comments