Skip to content

feat(auth): add AWS Bedrock token provider#3135

Open
tbuatois wants to merge 6 commits intoopenai:mainfrom
tbuatois:feat/aws-bedrock-token-provider
Open

feat(auth): add AWS Bedrock token provider#3135
tbuatois wants to merge 6 commits intoopenai:mainfrom
tbuatois:feat/aws-bedrock-token-provider

Conversation

@tbuatois
Copy link
Copy Markdown

feat(auth): add built-in AWS Bedrock token provider

Add aws_bedrock_token_provider() to generate bearer tokens for AWS Bedrock OpenAI-compatible endpoints using SigV4 presigned URLs.

  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

Amazon Bedrock provides OpenAI-compatible API endpoints, but today users must install and configure a separate library (aws-bedrock-token-generator-python) to generate the required bearer tokens. This PR builds that token generation directly into the SDK.

What it does:

  • New aws_bedrock_token_provider() function in openai.auth
  • Generates bedrock-api-key-<base64-encoded-presigned-url> tokens via SigV4 signing
  • Credentials resolved from the standard AWS credential chain (env vars, profiles, IMDS, etc.)
  • Tokens are cached and automatically refreshed before expiry
  • botocore is an optional dependency via pip install 'openai[bedrock]'

Usage:

from openai import OpenAI
from openai.auth import aws_bedrock_token_provider

client = OpenAI(
    base_url="https://bedrock-runtime.us-east-1.amazonaws.com/openai/v1",
    api_key=aws_bedrock_token_provider(region="us-east-1"),
)

Files changed (5):

File Change
src/openai/auth/_workload.py New aws_bedrock_token_provider() function
src/openai/auth/__init__.py Re-export the new function
pyproject.toml Add openai[bedrock] optional dependency for botocore
README.md Usage documentation in the Workload Identity section
tests/test_auth.py 4 unit tests (token generation, custom region, missing credentials, missing botocore)

Additional context & links

Add aws_bedrock_token_provider() to generate bearer tokens for AWS
Bedrock OpenAI-compatible endpoints using SigV4 presigned URLs.

- Credentials resolved from standard AWS chain (env, profiles, IMDS)
- Tokens cached and auto-refreshed before expiry
- botocore available via openai[bedrock] optional dependency
@tbuatois tbuatois requested a review from a team as a code owner April 28, 2026 12:34
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a9f9f2cce1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/openai/auth/_workload.py Outdated
@tbuatois
Copy link
Copy Markdown
Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cc64a8b16b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/openai/auth/_workload.py Outdated
now = time.monotonic()
if _cached_token[0] is None or now >= _refresh_at[0]:
_cached_token[0] = _generate_token()
_refresh_at[0] = now + max(token_duration - 60, token_duration * 0.9)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Account for AWS credential expiry in token cache

The refresh schedule is based only on token_duration, but Bedrock bearer tokens signed with temporary AWS credentials stop working when those credentials expire, which can be much earlier than the requested token duration (for example, 15-minute STS credentials). With the current logic, a token can be reused for nearly an hour after its signing credentials have expired, causing intermittent 401s until _refresh_at is reached. The cache policy should incorporate credential expiration (or avoid caching) so expired signed tokens are not returned.

Useful? React with 👍 / 👎.

Comment thread tests/test_auth.py Outdated
Comment on lines +221 to +222
import botocore.auth
import botocore.awsrequest
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Guard botocore imports in Bedrock auth tests

These tests import real botocore modules unconditionally, but botocore is only declared as an optional extra and is not part of the default dev lockfile used by the standard nox test session. In a normal test environment without openai[bedrock], this helper raises ModuleNotFoundError before the new tests run, which can fail CI/test runs unrelated to Bedrock support. The tests should skip when botocore is unavailable or fully mock these modules.

Useful? React with 👍 / 👎.

@tbuatois
Copy link
Copy Markdown
Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 71df8f4131

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

region: str | None = None,
profile: str | None = None,
token_duration: int = 3600,
) -> Callable[[], str]:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Add async-compatible Bedrock token provider

aws_bedrock_token_provider() currently returns a synchronous Callable[[], str], but AsyncOpenAI unconditionally awaits api_key providers in AsyncOpenAI._refresh_api_key. If an async user passes this new helper (the same way as the sync example), requests fail at runtime with a TypeError because str is not awaitable. This makes the new Bedrock auth path unusable for async clients unless users write their own wrapper.

Useful? React with 👍 / 👎.

@tbuatois
Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant