From aaf881ff8d8c51e96188e764b51d0e1c66cc4b2b Mon Sep 17 00:00:00 2001 From: Willow Lopez <100782273+Oxygen56@users.noreply.github.com> Date: Tue, 2 Jun 2026 02:13:37 +0800 Subject: [PATCH] fix(bedrock): pass aws_profile to _infer_region() for correct region detection When aws_profile is specified in AnthropicBedrock or AnthropicBedrockClient but AWS_REGION is not set, _infer_region() previously created a boto3.Session() without profile_name, defaulting to the 'default' profile and ignoring the caller's explicitly configured profile. This caused cross-region inference failures for users with non-default AWS profiles. Fix: accept an optional aws_profile parameter in _infer_region() and pass it through to boto3.Session(profile_name=aws_profile). Closes #892. --- src/anthropic/lib/bedrock/_client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/anthropic/lib/bedrock/_client.py b/src/anthropic/lib/bedrock/_client.py index cda0690df..ac571bc0b 100644 --- a/src/anthropic/lib/bedrock/_client.py +++ b/src/anthropic/lib/bedrock/_client.py @@ -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. @@ -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: @@ -178,7 +178,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=aws_profile) if aws_region is None else aws_region self.aws_profile = aws_profile self.aws_session_token = aws_session_token @@ -343,7 +343,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=aws_profile) if aws_region is None else aws_region self.aws_profile = aws_profile self.aws_session_token = aws_session_token