Skip to content

Commit 4db15c8

Browse files
committed
Fix: resolve default region-specific model initialization in default agent settings
1 parent ba929f3 commit 4db15c8

File tree

3 files changed

+1024
-993
lines changed

3 files changed

+1024
-993
lines changed

src/strands/models/bedrock.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
logger = logging.getLogger(__name__)
3030

3131
DEFAULT_BEDROCK_REGION = "us-west-2"
32-
DEFAULT_BEDROCK_MODEL_ID = "us.anthropic.claude-sonnet-4-20250514-v1:0"
32+
DEFAULT_BEDROCK_MODEL_ID = "anthropic.claude-sonnet-4-20250514-v1:0"
3333

3434
BEDROCK_CONTEXT_WINDOW_OVERFLOW_MESSAGES = [
3535
"Input is too long for requested model",
@@ -137,7 +137,11 @@ def __init__(
137137
# get default model id based on resolved region
138138
resolved_model_id = self._get_default_model_for_region(resolved_region)
139139
if resolved_model_id == "":
140-
raise ValueError("default model {} is not available in {} region. Specify another model".format(DEFAULT_BEDROCK_MODEL_ID, resolved_region))
140+
raise ValueError(
141+
"default model {} is not available in {} region. Specify another model".format(
142+
DEFAULT_BEDROCK_MODEL_ID, resolved_region
143+
)
144+
)
141145

142146
self.config = BedrockModel.BedrockConfig(model_id=resolved_model_id)
143147
self.update_config(**model_config)
@@ -357,15 +361,18 @@ def _generate_redaction_events(self) -> list[StreamEvent]:
357361
return events
358362

359363
def _get_default_model_for_region(self, region: str) -> str:
360-
client = boto3.client("bedrock", region_name=region)
361-
response = client.list_inference_profiles()
362-
inferenceProfileSummary = response["inferenceProfileSummaries"]
363-
364-
for profile in inferenceProfileSummary:
365-
if DEFAULT_BEDROCK_MODEL_ID in profile["inferenceProfileId"]:
366-
return profile["inferenceProfileId"]
367-
368-
return ""
364+
try:
365+
client = boto3.client("bedrock", region_name=region)
366+
response = client.list_inference_profiles()
367+
inference_profile_summary = response["inferenceProfileSummaries"]
368+
369+
for profile in inference_profile_summary:
370+
if DEFAULT_BEDROCK_MODEL_ID in profile["inferenceProfileId"]:
371+
return str(profile["inferenceProfileId"])
372+
373+
return ""
374+
except ClientError as e:
375+
raise e
369376

370377
@override
371378
async def stream(

tests/strands/agent/test_agent.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def test_agent__init__with_default_model():
211211
agent = Agent()
212212

213213
assert isinstance(agent.model, BedrockModel)
214-
assert agent.model.config["model_id"] == DEFAULT_BEDROCK_MODEL_ID
214+
assert agent.model.config["model_id"] and DEFAULT_BEDROCK_MODEL_ID in agent.model.config["model_id"]
215215

216216

217217
def test_agent__init__with_explicit_model(mock_model):
@@ -891,7 +891,9 @@ def test_agent__del__(agent):
891891
def test_agent_init_with_no_model_or_model_id():
892892
agent = Agent()
893893
assert agent.model is not None
894-
assert agent.model.get_config().get("model_id") == DEFAULT_BEDROCK_MODEL_ID
894+
assert agent.model.get_config().get("model_id") and DEFAULT_BEDROCK_MODEL_ID in agent.model.get_config().get(
895+
"model_id"
896+
)
895897

896898

897899
def test_agent_tool_no_parameter_conflict(agent, tool_registry, mock_randint, agenerator):

0 commit comments

Comments
 (0)