Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "2.39.0"
".": "2.40.0"
}
4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 262
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai/openai-2a971ccbb946726988e96654eaecceb6d9b5ad27f5793c0064b864f5686d4fc1.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai/openai-a6fedde02dc6241719ebb2589062ba2892baa8dbe928a2d718643beede85e7b3.yml
openapi_spec_hash: a712e4ee68f829570b3f5b267afa5a05
config_hash: bb69d8d0771dbac4a84fc6dca11e3ceb
config_hash: e02ca1082421dfe55b145c45e95d6126
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## 2.40.0 (2026-06-01)

Full Changelog: [v2.39.0...v2.40.0](https://github.com/openai/openai-python/compare/v2.39.0...v2.40.0)

### Features
* **api:** Add Amazon Bedrock Responses support

### Bug Fixes

* **api:** allow setting bedrock api keys on the client directly ([4d5bfde](https://github.com/openai/openai-python/commit/4d5bfdec37fa8a2b2a0413724755e586e627e28d))

## 2.39.0 (2026-06-01)

Full Changelog: [v2.38.0...v2.39.0](https://github.com/openai/openai-python/compare/v2.38.0...v2.39.0)
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,37 @@ In addition to the options provided in the base `OpenAI` client, the following o

An example of using the client with Microsoft Entra ID (formerly known as Azure Active Directory) can be found [here](https://github.com/openai/openai-python/blob/main/examples/azure_ad.py).

## Amazon Bedrock

To 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.

```py
from openai import BedrockOpenAI

# gets the bearer token from AWS_BEARER_TOKEN_BEDROCK and the region from AWS_REGION/AWS_DEFAULT_REGION
client = BedrockOpenAI()

response = client.responses.create(
model="openai.gpt-5.4",
input="Say hello!",
)

print(response.output_text)
```

`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.

Pass `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`.

Set `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`:

```py
client = BedrockOpenAI(
aws_region="us-west-2",
bedrock_token_provider=lambda: refresh_bedrock_token(),
)
```

## Versioning

This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions:
Expand Down
13 changes: 13 additions & 0 deletions examples/bedrock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from openai import BedrockOpenAI

client = BedrockOpenAI()

# For refreshed Bedrock bearer tokens:
# client = BedrockOpenAI(aws_region="us-west-2", bedrock_token_provider=get_bedrock_token)

response = client.responses.create(
model="openai.gpt-5.4",
input="Say hello!",
)

print(response.output_text)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "openai"
version = "2.39.0"
version = "2.40.0"
description = "The official Python library for the openai API"
dynamic = ["readme"]
license = "Apache-2.0"
Expand Down
42 changes: 39 additions & 3 deletions src/openai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
"AsyncStream",
"OpenAI",
"AsyncOpenAI",
"BedrockOpenAI",
"AsyncBedrockOpenAI",
"file_from_path",
"BaseModel",
"DEFAULT_TIMEOUT",
Expand All @@ -96,9 +98,10 @@
if not _t.TYPE_CHECKING:
from ._utils._resources_proxy import resources as resources

from .lib import azure as _azure, pydantic_function_tool as pydantic_function_tool
from .lib import azure as _azure, bedrock as _bedrock, pydantic_function_tool as pydantic_function_tool
from .version import VERSION as VERSION
from .lib.azure import AzureOpenAI as AzureOpenAI, AsyncAzureOpenAI as AsyncAzureOpenAI
from .lib.bedrock import BedrockOpenAI as BedrockOpenAI, AsyncBedrockOpenAI as AsyncBedrockOpenAI
from .lib._old_api import *
from .lib.streaming import (
AssistantEventHandler as AssistantEventHandler,
Expand Down Expand Up @@ -150,7 +153,7 @@

http_client: _httpx.Client | None = None

_ApiType = _te.Literal["openai", "azure"]
_ApiType = _te.Literal["openai", "azure", "amazon-bedrock"]

api_type: _ApiType | None = _t.cast(_ApiType, _os.environ.get("OPENAI_API_TYPE"))

Expand All @@ -162,6 +165,10 @@

azure_ad_token_provider: _azure.AzureADTokenProvider | None = None

_bedrock_api_key: str | None = None

bedrock_token_provider: _bedrock.BedrockTokenProvider | None = None


class _ModuleClient(OpenAI):
# Note: we have to use type: ignores here as overriding class members
Expand Down Expand Up @@ -294,10 +301,23 @@ class _AzureModuleClient(_ModuleClient, AzureOpenAI): # type: ignore
...


class _BedrockModuleClient(_ModuleClient, BedrockOpenAI): # type: ignore
@property # type: ignore
@override
def api_key(self) -> str | None:
return api_key if api_key is not None else _bedrock_api_key

@api_key.setter # type: ignore
def api_key(self, value: str | None) -> None: # type: ignore
global _bedrock_api_key

_bedrock_api_key = value


class _AmbiguousModuleClientUsageError(OpenAIError):
def __init__(self) -> None:
super().__init__(
"Ambiguous use of module client; please set `openai.api_type` or the `OPENAI_API_TYPE` environment variable to `openai` or `azure`"
"Ambiguous use of module client; please set `openai.api_type` or the `OPENAI_API_TYPE` environment variable to `openai`, `azure`, or `amazon-bedrock`"
)


Expand Down Expand Up @@ -370,6 +390,22 @@ def _load_client() -> OpenAI: # type: ignore[reportUnusedFunction]
)
return _client

if api_type == "amazon-bedrock":
_client = _BedrockModuleClient( # type: ignore
api_key=api_key,
bedrock_token_provider=bedrock_token_provider,
organization=organization,
project=project,
webhook_secret=webhook_secret,
base_url=base_url,
timeout=timeout,
max_retries=max_retries,
default_headers=default_headers,
default_query=default_query,
http_client=http_client,
)
return _client

_client = _ModuleClient(
api_key=api_key,
admin_api_key=admin_api_key,
Expand Down
2 changes: 1 addition & 1 deletion src/openai/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

__title__ = "openai"
__version__ = "2.39.0" # x-release-please-version
__version__ = "2.40.0" # x-release-please-version
Loading
Loading