Skip to content

fix(bedrock): correctly infer region from AWS_DEFAULT_REGION and explicit aws_profile arg#1286

Open
alvinttang wants to merge 1 commit intoanthropics:mainfrom
alvinttang:fix/bedrock-region-detection
Open

fix(bedrock): correctly infer region from AWS_DEFAULT_REGION and explicit aws_profile arg#1286
alvinttang wants to merge 1 commit intoanthropics:mainfrom
alvinttang:fix/bedrock-region-detection

Conversation

@alvinttang
Copy link
Copy Markdown

Summary

Fixes #892.

_infer_region() had two related gaps that could cause AnthropicBedrock() to build its base URL for the wrong region:

  1. AWS_DEFAULT_REGION not checked directly. AWS_DEFAULT_REGION is the standard env var boto3/botocore uses for region configuration. The function only checked AWS_REGION before falling through to boto3.Session(). Users relying on AWS_DEFAULT_REGION would get the correct answer (boto3 picks it up), but the precedence and intent were not explicit.

  2. aws_profile not forwarded to boto3 during region inference. _infer_region() was called with no arguments and created a plain boto3.Session(), ignoring any aws_profile passed to the client. This meant AnthropicBedrock(aws_profile="us-west-2-profile") would resolve the region from the default profile (or env), build bedrock-runtime.<wrong-region>.amazonaws.com as the base URL, and then sign requests using the named profile — resulting in a cross-region mismatch that breaks inference.

Changes

  • _infer_region(aws_profile=None) now checks AWS_DEFAULT_REGION after AWS_REGION.
  • _infer_region accepts and forwards aws_profile to boto3.Session(profile_name=...).
  • Both AnthropicBedrock.__init__ and AsyncAnthropicBedrock.__init__ assign self.aws_profile before calling _infer_region(aws_profile) so the profile is available at inference time.

Tests

Three new tests in tests/lib/test_bedrock.py:

  • test_region_infer_from_aws_default_region_env — verifies AWS_DEFAULT_REGION is picked up.
  • test_aws_region_env_takes_precedence_over_aws_default_region — verifies AWS_REGION wins when both are set.
  • test_region_infer_from_explicit_aws_profile_arg — verifies that aws_profile= passed directly to the client is used when inferring region, even when AWS_PROFILE is not set in the environment.

All 9 tests pass.

…profile arg

- `_infer_region()` now checks `AWS_DEFAULT_REGION` in addition to `AWS_REGION`,
  since `AWS_DEFAULT_REGION` is the standard env var recognised by boto3/botocore.
  Previously only `AWS_REGION` was checked, so users who set only
  `AWS_DEFAULT_REGION` would fall through to the boto3 session path and get the
  right answer, but the intent was not explicit and `AWS_REGION` having higher
  priority was undocumented.

- `_infer_region()` now accepts an optional `aws_profile` argument and passes it
  to `boto3.Session()`. Before this fix, `AnthropicBedrock(aws_profile="foo")`
  would resolve the region from a profileless `boto3.Session()` (ignoring the
  named profile's region), meaning the base URL could be built for the wrong
  region even though subsequent requests were signed against the correct profile.

Fixes anthropics#892

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@alvinttang alvinttang requested a review from a team as a code owner March 24, 2026 05:15
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