Skip to content

fix(bedrock): raise CredentialsError when AWS credentials cannot be resolved - #2046

Open
Lushiyao5 wants to merge 1 commit into
anthropics:mainfrom
Lushiyao5:fix/bedrock-credentials-error
Open

Lushiyao5 wants to merge 1 commit into
anthropics:mainfrom
Lushiyao5:fix/bedrock-credentials-error

Conversation

@Lushiyao5

Copy link
Copy Markdown

Closes #2038

When AnthropicBedrock / AsyncAnthropicBedrock has no API key and boto3 cannot resolve any AWS credentials, get_auth_headers raised a bare RuntimeError("could not resolve credentials from session"). That is not an anthropic.AnthropicError, so callers — and frameworks wrapping the SDK like pydantic-ai's AnthropicModel — cannot classify it as a configuration problem without matching on the message string.

anthropic>=2 ships anthropic.CredentialsError ("credentials cannot be loaded from local files, config, or the environment"), which is exactly this case; use it in the Bedrock path.

Change: one line in src/anthropic/lib/bedrock/_auth.py — raise CredentialsError(...) instead of raise RuntimeError(...), plus the import. Behavior for the resolvable-credentials path is unchanged.

Test (tests/lib/test_bedrock.py): test_missing_aws_credentials_raises_credentials_error patches _get_session to return a session with no credentials and asserts the raised error is anthropic.CredentialsError with the same message. tests/lib/test_bedrock.py + tests/lib/test_aws_auth.py: 102 passed, 4 failed — the four failures are test_region_infer_* profile/environment tests that also fail at main without this change (verified on HEAD), unrelated to the credentials-error path.

…esolved

When AnthropicBedrock / AsyncAnthropicBedrock has no API key and boto3
cannot resolve any AWS credentials, get_auth_headers raised a bare
RuntimeError. That is not an anthropic.AnthropicError, so callers (and
frameworks wrapping the SDK, like pydantic-ai) cannot classify it as a
configuration problem without matching on the message string.

anthropic>=2 ships anthropic.CredentialsError for exactly this case
('credentials cannot be loaded from local files, config, or the
environment'); use it in the Bedrock path.

Closes anthropics#2038
@Lushiyao5
Lushiyao5 requested a review from a team as a code owner October 30, 2026 10:32

This branch has not been deployed

No deployments
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.

AnthropicBedrock raises a bare RuntimeError when no AWS credentials resolve, instead of an anthropic error class (e.g. CredentialsError)

1 participant