-
Notifications
You must be signed in to change notification settings - Fork 818
feat: enable openai provider use aws profile #2230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JackYPCOnline
merged 6 commits into
strands-agents:main
from
JackYPCOnline:feature/openai
May 1, 2026
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d94a7f4
feat: enable openai provider use aws profile
JackYPCOnline 4687f26
fix: missing file
JackYPCOnline 6281721
fix: address bot's comments
JackYPCOnline 4e5837a
fix: address bot's comments
JackYPCOnline ff39434
fix: address comments
JackYPCOnline c676a8c
fix: address comments
JackYPCOnline File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| """Internal helpers for routing OpenAI-compatible clients to Bedrock Mantle. | ||
|
|
||
| Converts an ``aws_config`` dict into the ``base_url`` and ``api_key`` that the | ||
| OpenAI Python SDK consumes. Tokens are minted on demand via | ||
| ``aws_bedrock_token_generator.provide_token`` so long-running agents survive the | ||
| bearer token's maximum lifetime. | ||
|
|
||
| ``aws_bedrock_token_generator`` is imported lazily so that extras which reuse | ||
| the OpenAI package without pulling the Mantle dependency do not hit an | ||
| ``ImportError`` at module load. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from datetime import timedelta | ||
| from typing import Any, TypedDict | ||
|
|
||
| from typing_extensions import Required | ||
|
|
||
| _MANTLE_BASE_URL_TEMPLATE = "https://bedrock-mantle.{region}.api.aws/v1" | ||
|
|
||
|
|
||
| class AwsConfig(TypedDict, total=False): | ||
|
JackYPCOnline marked this conversation as resolved.
Outdated
|
||
| """AWS-side config for reaching Bedrock Mantle via an OpenAI-compatible client. | ||
|
|
||
| Attributes: | ||
| region: AWS region hosting the Bedrock Mantle endpoint. | ||
| credentials_provider: Optional botocore ``CredentialProvider`` forwarded to | ||
| ``provide_token``. Defaults to the AWS credential chain. | ||
| expiry: Optional ``timedelta`` for the bearer token's lifetime, forwarded | ||
| to ``provide_token``. | ||
| """ | ||
|
|
||
| region: Required[str] | ||
|
JackYPCOnline marked this conversation as resolved.
Outdated
|
||
| credentials_provider: Any | ||
|
JackYPCOnline marked this conversation as resolved.
Outdated
|
||
| expiry: timedelta | ||
|
|
||
|
|
||
| def resolve_bedrock_client_args(aws_config: AwsConfig, client_args: dict[str, Any] | None = None) -> dict[str, Any]: | ||
| """Resolve an ``AwsConfig`` (plus optional ``client_args``) into OpenAI client kwargs. | ||
|
|
||
| Mints a fresh bearer token on every call. When ``client_args`` is provided, its | ||
| entries are preserved except for ``base_url`` and ``api_key``, which are always | ||
| overridden by the values derived from ``aws_config``. | ||
|
|
||
| Raises: | ||
| ValueError: If ``aws_config['region']`` is missing or empty. | ||
| ImportError: If ``aws-bedrock-token-generator`` is not installed. | ||
| RuntimeError: If token minting fails (e.g. missing AWS credentials). | ||
| """ | ||
| region = aws_config.get("region") | ||
| if not region: | ||
| raise ValueError("aws_config must include a non-empty 'region'.") | ||
|
|
||
| try: | ||
| from aws_bedrock_token_generator import provide_token | ||
| except ImportError as e: | ||
| raise ImportError( | ||
| "aws_config requires the 'aws-bedrock-token-generator' package. " | ||
|
JackYPCOnline marked this conversation as resolved.
Outdated
|
||
| "Install it with: pip install strands-agents[openai]" | ||
| ) from e | ||
|
|
||
| # Only forward kwargs the user set; provide_token rejects expiry=None. | ||
| token_kwargs: dict[str, Any] = {"region": region} | ||
| if "credentials_provider" in aws_config: | ||
| token_kwargs["aws_credentials_provider"] = aws_config["credentials_provider"] | ||
| if "expiry" in aws_config: | ||
| token_kwargs["expiry"] = aws_config["expiry"] | ||
|
|
||
| try: | ||
| token = provide_token(**token_kwargs) | ||
| except Exception as e: | ||
| raise RuntimeError( | ||
| f"Failed to mint Bedrock Mantle bearer token for region '{region}'. " | ||
| "Verify your AWS credentials and network connectivity." | ||
| ) from e | ||
|
|
||
| resolved: dict[str, Any] = dict(client_args or {}) | ||
| resolved["base_url"] = _MANTLE_BASE_URL_TEMPLATE.format(region=region) | ||
|
JackYPCOnline marked this conversation as resolved.
|
||
| resolved["api_key"] = token | ||
| return resolved | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.