Skip to content

Fix Bedrock region detection to respect aws_profile and AWS_DEFAULT_REGION#1608

Open
Sarmkadan wants to merge 1 commit into
anthropics:mainfrom
Sarmkadan:fix/bedrock-region-detection
Open

Fix Bedrock region detection to respect aws_profile and AWS_DEFAULT_REGION#1608
Sarmkadan wants to merge 1 commit into
anthropics:mainfrom
Sarmkadan:fix/bedrock-region-detection

Conversation

@Sarmkadan
Copy link
Copy Markdown

Summary

Fixes #892

The Bedrock client's _infer_region() function had two issues causing incorrect region detection:

  • aws_profile not forwarded: When a user passed aws_profile="my-profile" to AnthropicBedrock(), the profile parameter was not passed through to _infer_region(), so boto3.Session() was created without the profile name. This meant the profile-specific region from ~/.aws/config was ignored, and the client would fall back to us-east-1 instead of using the profile's configured region. (The AWS_PROFILE env var still worked because boto3 reads it internally, but the explicit constructor parameter did not.)

  • AWS_DEFAULT_REGION not checked: The function only checked the AWS_REGION env var but not AWS_DEFAULT_REGION, which is also supported by boto3 and the AWS CLI.

Changes

  • _infer_region() now accepts an aws_profile parameter and passes it to boto3.Session(profile_name=...)
  • AWS_DEFAULT_REGION is checked as a fallback when AWS_REGION is unset
  • self.aws_profile is assigned before _infer_region() is called so the profile is available
  • Removed redundant or "us-east-1" fallbacks in _prepare_request (both sync and async) since _infer_region() already guarantees a non-None return

Resolution order (updated)

  1. AWS_REGION environment variable
  2. AWS_DEFAULT_REGION environment variable
  3. Region from boto3.Session (respects AWS_PROFILE env var, ~/.aws/config, and the explicit aws_profile constructor parameter)
  4. Hard-coded us-east-1 (legacy fallback)

Test plan

  • Added test_region_infer_from_aws_default_region - verifies AWS_DEFAULT_REGION is respected
  • Added test_aws_region_takes_precedence_over_aws_default_region - verifies AWS_REGION wins over AWS_DEFAULT_REGION
  • Added test_region_infer_from_explicit_aws_profile_param - verifies the aws_profile constructor param is used for region inference (sync client)
  • Added test_region_infer_from_explicit_aws_profile_param_async - same for async client
  • All 16 existing + new tests pass

…FAULT_REGION

The _infer_region() function was not receiving the aws_profile parameter,
so when a user passed aws_profile="my-profile" to AnthropicBedrock(), the
profile-specific region from ~/.aws/config was ignored during region
inference. The function also only checked AWS_REGION, missing the
AWS_DEFAULT_REGION environment variable that boto3 and AWS CLI support.

Changes:
- Pass aws_profile to _infer_region() so boto3.Session resolves the
  correct profile-specific region
- Check AWS_DEFAULT_REGION as fallback when AWS_REGION is unset
- Set self.aws_profile before calling _infer_region() so it is available
- Remove redundant "or us-east-1" fallbacks in _prepare_request since
  _infer_region() already guarantees a non-None return value

Fixes anthropics#892
@Sarmkadan Sarmkadan requested a review from a team as a code owner May 28, 2026 18:58
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.

Bedrock client failing to detect AWS region correctly can cause cross-region inference to fail

1 participant