From 07253eb5cd9638f521514b806c817d79a3d71099 Mon Sep 17 00:00:00 2001 From: feranmiodugbemi <87916077+feranmiodugbemi@users.noreply.github.com> Date: Wed, 25 Sep 2024 01:34:32 +0000 Subject: [PATCH] Update AWS region logic to include env variable, config, and default --- src/anthropic/lib/bedrock/_client.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/anthropic/lib/bedrock/_client.py b/src/anthropic/lib/bedrock/_client.py index f7298adc..ca6f6fcf 100644 --- a/src/anthropic/lib/bedrock/_client.py +++ b/src/anthropic/lib/bedrock/_client.py @@ -3,6 +3,7 @@ import os from typing import Any, Union, Mapping, TypeVar from typing_extensions import Self, override +import boto3 import httpx @@ -116,9 +117,8 @@ def __init__( self.aws_access_key = aws_access_key - if aws_region is None: - aws_region = os.environ.get("AWS_REGION") or "us-east-1" - self.aws_region = aws_region + self.aws_region = aws_region or os.environ.get("AWS_REGION") or self.get_region_from_boto3() or "us-east-1" + self.aws_session_token = aws_session_token @@ -167,6 +167,12 @@ def _prepare_request(self, request: httpx.Request) -> None: ) request.headers.update(headers) + def get_region_from_boto3( + self + ) -> str | None: + session = boto3.Session() + return session.region_name + def copy( self, *,