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
6 changes: 4 additions & 2 deletions src/strands/models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import logging
import os
from typing import Any, Iterable, Literal, Optional, cast

import boto3
Expand Down Expand Up @@ -96,7 +97,8 @@ def __init__(
Args:
boto_session: Boto Session to use when calling the Bedrock Model.
boto_client_config: Configuration to use when creating the Bedrock-Runtime Boto Client.
region_name: AWS region to use for the Bedrock service. Defaults to "us-west-2".
region_name: AWS region to use for the Bedrock service.
Defaults to the AWS_REGION environment variable if set, or "us-west-2" if not set.
**model_config: Configuration options for the Bedrock model.
"""
if region_name and boto_session:
Expand All @@ -108,7 +110,7 @@ def __init__(
logger.debug("config=<%s> | initializing", self.config)

session = boto_session or boto3.Session(
region_name=region_name or "us-west-2",
region_name=region_name or os.getenv("AWS_REGION") or "us-west-2",
)
client_config = boto_client_config or BotocoreConfig(user_agent_extra="strands-agents")
self.client = session.client(
Expand Down
11 changes: 11 additions & 0 deletions tests/strands/models/test_bedrock.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import unittest.mock

import boto3
Expand Down Expand Up @@ -99,6 +100,16 @@ def test__init__with_custom_region(bedrock_client):
mock_session_cls.assert_called_once_with(region_name=custom_region)


def test__init__with_environment_variable_region(bedrock_client):
"""Test that BedrockModel uses the provided region."""
_ = bedrock_client
os.environ["AWS_REGION"] = "eu-west-1"

with unittest.mock.patch("strands.models.bedrock.boto3.Session") as mock_session_cls:
_ = BedrockModel()
mock_session_cls.assert_called_once_with(region_name="eu-west-1")


def test__init__with_region_and_session_raises_value_error():
"""Test that BedrockModel raises ValueError when both region and session are provided."""
with pytest.raises(ValueError):
Expand Down