Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ def __init__(
"""Create a Bedrock chat client and load AWS credentials.

Args:
region: Region to send Bedrock requests to; falls back to BEDROCK_REGION, then to the region
of ``boto3_session``, then to us-east-1.
region: Region to send Bedrock requests to; falls back to the region of ``boto3_session``,
then to BEDROCK_REGION, then to us-east-1. A supplied ``client`` keeps its own region.
model: Default model identifier; falls back to BEDROCK_CHAT_MODEL.
access_key: Optional AWS access key for manual credential injection.
secret_key: Optional AWS secret key paired with ``access_key``.
Expand Down Expand Up @@ -322,8 +322,12 @@ class MyOptions(BedrockChatOptions, total=False):
env_file_path=env_file_path,
env_file_encoding=env_file_encoding,
)
session_region = boto3_session.region_name if boto3_session and not client else None
region = settings.get("region") or session_region or DEFAULT_REGION
if client is not None:
client_region = getattr(getattr(client, "meta", None), "region_name", None)
region = client_region if isinstance(client_region, str) else (settings.get("region") or DEFAULT_REGION)
else:
session_region = boto3_session.region_name if boto3_session else None
region = region or session_region or settings.get("region") or DEFAULT_REGION
chat_model = settings.get("chat_model")

self._owns_client = client is None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ class RawBedrockEmbeddingClient(
Keyword Args:
model: The Bedrock embedding model ID (e.g. "amazon.titan-embed-text-v2:0").
Can also be set via environment variable BEDROCK_EMBEDDING_MODEL.
region: AWS region. Will try to load from BEDROCK_REGION env var,
if not set, the region of ``boto3_session`` is used, or without a session
the regular Boto3 configuration/loading applies (which may include other
env vars, config files, or instance metadata). Falls back to us-east-1.
region: AWS region. If not set, the region of ``boto3_session`` is used, then the
BEDROCK_REGION env var. Without a session the regular Boto3 configuration/loading
applies (which may include other env vars, config files, or instance metadata).
Falls back to us-east-1. A supplied ``client`` keeps its own region.
access_key: AWS access key for manual credential injection.
secret_key: AWS secret key paired with access_key.
session_token: AWS session token for temporary credentials.
Expand Down Expand Up @@ -128,18 +128,21 @@ def __init__(

if client:
self._bedrock_client = client
client_region = getattr(getattr(client, "meta", None), "region_name", None)
if isinstance(client_region, str):
resolved_region = client_region
else:
if not boto3_session:
session_kwargs: dict[str, Any] = {}
if region := settings.get("region"):
session_kwargs["region_name"] = region
if settings_region := settings.get("region"):
session_kwargs["region_name"] = settings_region
if (access_key := settings.get("access_key")) and (secret_key := settings.get("secret_key")):
session_kwargs["aws_access_key_id"] = access_key.get_secret_value()
session_kwargs["aws_secret_access_key"] = secret_key.get_secret_value()
if session_token := settings.get("session_token"):
session_kwargs["aws_session_token"] = session_token.get_secret_value()
boto3_session = Boto3Session(**session_kwargs)
resolved_region = settings.get("region") or boto3_session.region_name or DEFAULT_REGION
resolved_region = region or boto3_session.region_name or settings.get("region") or DEFAULT_REGION
self._bedrock_client = boto3_session.client(
"bedrock-runtime",
region_name=resolved_region,
Expand Down Expand Up @@ -240,9 +243,9 @@ class BedrockEmbeddingClient(
Keyword Args:
model: The Bedrock embedding model ID (e.g. "amazon.titan-embed-text-v2:0").
Can also be set via environment variable BEDROCK_EMBEDDING_MODEL.
region: AWS region. Can also be set via environment variable BEDROCK_REGION.
If not set, the region of ``boto3_session`` is used, or without a session
the regular Boto3 configuration. Defaults to "us-east-1".
region: AWS region. If not set, the region of ``boto3_session`` is used, then the
environment variable BEDROCK_REGION, or without a session the regular Boto3
configuration. Defaults to "us-east-1". A supplied ``client`` keeps its own region.
access_key: AWS access key for manual credential injection.
secret_key: AWS secret key paired with access_key.
session_token: AWS session token for temporary credentials.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,12 @@ def client(self, service_name: str, *, region_name: str, config: Any) -> _StubBe
("region", "env_region", "session_region", "expected_region"),
[
(None, None, "eu-west-1", "eu-west-1"),
("us-west-2", None, "eu-west-1", "us-west-2"),
(None, "ap-south-1", "eu-west-1", "ap-south-1"),
("us-west-2", "ap-south-1", "eu-west-1", "us-west-2"),
(None, "ap-south-1", "eu-west-1", "eu-west-1"),
(None, "ap-south-1", None, "ap-south-1"),
(None, None, None, "us-east-1"),
],
ids=["session-region", "region-argument-wins", "env-region-wins", "default-region"],
ids=["session-region", "region-argument-wins", "session-region-beats-env", "env-region", "default-region"],
)
async def test_bedrock_embedding_resolves_region_with_boto3_session(
monkeypatch: pytest.MonkeyPatch,
Expand All @@ -171,7 +172,7 @@ async def test_bedrock_embedding_resolves_region_with_boto3_session(
session_region: str | None,
expected_region: str,
) -> None:
"""A configured region wins, then the provided session's region, then the default region."""
"""The region argument wins, then the provided session's region, then BEDROCK_REGION, then the default."""
if env_region:
monkeypatch.setenv("BEDROCK_REGION", env_region)
else:
Expand Down Expand Up @@ -214,6 +215,20 @@ async def test_bedrock_embedding_ignores_boto3_session_when_runtime_client_suppl
assert client.region == "us-east-1"


async def test_bedrock_embedding_reports_region_of_supplied_runtime_client(monkeypatch: pytest.MonkeyPatch) -> None:
"""A preconfigured runtime client defines the region, so BEDROCK_REGION should not change it."""
monkeypatch.setenv("BEDROCK_REGION", "ap-south-1")
runtime = _StubBedrockEmbeddingRuntime()
runtime.meta.region_name = "eu-west-1"

client = BedrockEmbeddingClient(
model="amazon.titan-embed-text-v2:0",
client=runtime, # pyrefly: ignore[bad-argument-type] # ty: ignore[invalid-argument-type] # pyright: ignore[reportArgumentType]
)

assert client.region == "eu-west-1"


# region: Integration Tests

skip_if_bedrock_embedding_integration_tests_disabled = pytest.mark.skipif(
Expand Down
21 changes: 17 additions & 4 deletions python/packages/bedrock/tests/test_bedrock_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,12 @@ def test_init_uses_boto3_session_when_runtime_client_not_supplied() -> None:
("region", "env_region", "session_region", "expected_region"),
[
(None, None, "eu-west-1", "eu-west-1"),
("us-west-2", None, "eu-west-1", "us-west-2"),
(None, "ap-south-1", "eu-west-1", "ap-south-1"),
("us-west-2", "ap-south-1", "eu-west-1", "us-west-2"),
(None, "ap-south-1", "eu-west-1", "eu-west-1"),
(None, "ap-south-1", None, "ap-south-1"),
(None, None, None, "us-east-1"),
],
ids=["session-region", "region-argument-wins", "env-region-wins", "default-region"],
ids=["session-region", "region-argument-wins", "session-region-beats-env", "env-region", "default-region"],
)
def test_init_resolves_region_with_boto3_session(
monkeypatch: pytest.MonkeyPatch,
Expand All @@ -681,7 +682,7 @@ def test_init_resolves_region_with_boto3_session(
session_region: str | None,
expected_region: str,
) -> None:
"""A configured region wins, then the provided session's region, then the default region."""
"""The region argument wins, then the provided session's region, then BEDROCK_REGION, then the default."""
if env_region:
monkeypatch.setenv("BEDROCK_REGION", env_region)
else:
Expand Down Expand Up @@ -711,6 +712,18 @@ def test_init_ignores_boto3_session_region_when_runtime_client_supplied(monkeypa
assert client.service_url() == "https://bedrock-runtime.us-east-1.amazonaws.com"


def test_init_reports_region_of_supplied_runtime_client(monkeypatch: pytest.MonkeyPatch) -> None:
"""A preconfigured runtime client defines the region, so BEDROCK_REGION should not change it."""
monkeypatch.setenv("BEDROCK_REGION", "ap-south-1")
runtime = _StubBedrockRuntime()
setattr(runtime, "meta", MagicMock(region_name="eu-west-1")) # noqa: B010

client = BedrockChatClient(model="amazon.titan-text", client=cast(BaseClient, runtime))

assert client.region == "eu-west-1"
assert client.service_url() == "https://bedrock-runtime.eu-west-1.amazonaws.com"


@pytest.mark.parametrize("secret_type", [str, SecretString], ids=["str", "secret"])
@pytest.mark.parametrize(
("client_type", "module"),
Expand Down
Loading