Skip to content
Closed
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
8 changes: 4 additions & 4 deletions src/anthropic/lib/bedrock/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _prepare_options(input_options: FinalRequestOptions) -> FinalRequestOptions:
return options


def _infer_region() -> str:
def _infer_region(aws_profile: str | None = None) -> str:
"""
Infer the AWS region from the environment variables or
from the boto3 session if available.
Expand All @@ -77,7 +77,7 @@ def _infer_region() -> str:
try:
import boto3

session = boto3.Session()
session = boto3.Session(profile_name=aws_profile)
if session.region_name:
aws_region = session.region_name
except ImportError:
Expand Down Expand Up @@ -161,7 +161,7 @@ def __init__(

self.aws_access_key = aws_access_key

self.aws_region = _infer_region() if aws_region is None else aws_region
self.aws_region = _infer_region(aws_profile) if aws_region is None else aws_region
self.aws_profile = aws_profile

self.aws_session_token = aws_session_token
Expand Down Expand Up @@ -303,7 +303,7 @@ def __init__(

self.aws_access_key = aws_access_key

self.aws_region = _infer_region() if aws_region is None else aws_region
self.aws_region = _infer_region(aws_profile) if aws_region is None else aws_region
self.aws_profile = aws_profile

self.aws_session_token = aws_session_token
Expand Down
55 changes: 55 additions & 0 deletions tests/lib/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,58 @@ def test_region_infer_from_specified_profile(
client = AnthropicBedrock()

assert client.aws_region == next(profile for profile in profiles if profile["name"] == aws_profile)["region"]


@pytest.mark.parametrize(
"profiles, aws_profile",
[
pytest.param(
[{"name": "default", "region": "us-east-2"}, {"name": "custom", "region": "eu-west-1"}],
"custom",
id="custom profile via constructor",
),
],
)
def test_region_infer_from_constructor_aws_profile(
mock_aws_config: None, # noqa: ARG001
profiles: t.List[AwsConfigProfile],
aws_profile: str,
monkeypatch: t.Any,
) -> None:
"""aws_profile passed to constructor should be used for region inference,
even when AWS_PROFILE env var is not set."""
monkeypatch.delenv("AWS_REGION", raising=False)
monkeypatch.delenv("AWS_DEFAULT_REGION", raising=False)
monkeypatch.delenv("AWS_PROFILE", raising=False)

client = AnthropicBedrock(aws_profile=aws_profile)

expected_region = next(p for p in profiles if p["name"] == aws_profile)["region"]
assert client.aws_region == expected_region


@pytest.mark.parametrize(
"profiles, aws_profile",
[
pytest.param(
[{"name": "default", "region": "us-east-2"}, {"name": "custom", "region": "eu-west-1"}],
"custom",
id="async custom profile via constructor",
),
],
)
def test_region_infer_from_constructor_aws_profile_async(
mock_aws_config: None, # noqa: ARG001
profiles: t.List[AwsConfigProfile],
aws_profile: str,
monkeypatch: t.Any,
) -> None:
"""AsyncAnthropicBedrock should also use constructor aws_profile for region inference."""
monkeypatch.delenv("AWS_REGION", raising=False)
monkeypatch.delenv("AWS_DEFAULT_REGION", raising=False)
monkeypatch.delenv("AWS_PROFILE", raising=False)

client = AsyncAnthropicBedrock(aws_profile=aws_profile)

expected_region = next(p for p in profiles if p["name"] == aws_profile)["region"]
assert client.aws_region == expected_region