fix(bedrock): correctly infer region from AWS_DEFAULT_REGION and explicit aws_profile arg#1286
Open
alvinttang wants to merge 1 commit intoanthropics:mainfrom
Open
fix(bedrock): correctly infer region from AWS_DEFAULT_REGION and explicit aws_profile arg#1286alvinttang wants to merge 1 commit intoanthropics:mainfrom
alvinttang wants to merge 1 commit intoanthropics:mainfrom
Conversation
…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>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #892.
_infer_region()had two related gaps that could causeAnthropicBedrock()to build its base URL for the wrong region:AWS_DEFAULT_REGIONnot checked directly.AWS_DEFAULT_REGIONis the standard env var boto3/botocore uses for region configuration. The function only checkedAWS_REGIONbefore falling through toboto3.Session(). Users relying onAWS_DEFAULT_REGIONwould get the correct answer (boto3 picks it up), but the precedence and intent were not explicit.aws_profilenot forwarded to boto3 during region inference._infer_region()was called with no arguments and created a plainboto3.Session(), ignoring anyaws_profilepassed to the client. This meantAnthropicBedrock(aws_profile="us-west-2-profile")would resolve the region from the default profile (or env), buildbedrock-runtime.<wrong-region>.amazonaws.comas 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 checksAWS_DEFAULT_REGIONafterAWS_REGION._infer_regionaccepts and forwardsaws_profiletoboto3.Session(profile_name=...).AnthropicBedrock.__init__andAsyncAnthropicBedrock.__init__assignself.aws_profilebefore 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— verifiesAWS_DEFAULT_REGIONis picked up.test_aws_region_env_takes_precedence_over_aws_default_region— verifiesAWS_REGIONwins when both are set.test_region_infer_from_explicit_aws_profile_arg— verifies thataws_profile=passed directly to the client is used when inferring region, even whenAWS_PROFILEis not set in the environment.All 9 tests pass.