|
11 | 11 |
|
12 | 12 | import strands |
13 | 13 | from strands.models import BedrockModel |
14 | | -from strands.models.bedrock import DEFAULT_BEDROCK_MODEL_ID, DEFAULT_BEDROCK_REGION, DEFAULT_READ_TIMEOUT |
| 14 | +from strands.models.bedrock import ( |
| 15 | + _DEFAULT_BEDROCK_MODEL_ID, |
| 16 | + DEFAULT_BEDROCK_MODEL_ID, |
| 17 | + DEFAULT_BEDROCK_REGION, |
| 18 | + DEFAULT_READ_TIMEOUT, |
| 19 | +) |
15 | 20 | from strands.types.exceptions import ModelThrottledException |
16 | 21 | from strands.types.tools import ToolSpec |
17 | 22 |
|
| 23 | +FORMATTED_DEFAULT_MODEL_ID = DEFAULT_BEDROCK_MODEL_ID.format("us") |
| 24 | + |
18 | 25 |
|
19 | 26 | @pytest.fixture |
20 | 27 | def session_cls(): |
@@ -119,7 +126,7 @@ def test__init__default_model_id(bedrock_client): |
119 | 126 | model = BedrockModel() |
120 | 127 |
|
121 | 128 | tru_model_id = model.get_config().get("model_id") |
122 | | - exp_model_id = DEFAULT_BEDROCK_MODEL_ID |
| 129 | + exp_model_id = FORMATTED_DEFAULT_MODEL_ID |
123 | 130 |
|
124 | 131 | assert tru_model_id == exp_model_id |
125 | 132 |
|
@@ -1543,3 +1550,88 @@ def test_tool_choice_none_no_warning(model, messages, captured_warnings): |
1543 | 1550 | model.format_request(messages, tool_choice=None) |
1544 | 1551 |
|
1545 | 1552 | assert len(captured_warnings) == 0 |
| 1553 | + |
| 1554 | + |
| 1555 | +def test_get_default_model_with_warning_supported_regions_shows_no_warning(captured_warnings): |
| 1556 | + """Test get_model_prefix_with_warning doesn't warn for supported region prefixes.""" |
| 1557 | + BedrockModel._get_default_model_with_warning("us-west-2") |
| 1558 | + BedrockModel._get_default_model_with_warning("eu-west-2") |
| 1559 | + assert len(captured_warnings) == 0 |
| 1560 | + |
| 1561 | + |
| 1562 | +def test_get_default_model_for_supported_eu_region_returns_correct_model_id(captured_warnings): |
| 1563 | + model_id = BedrockModel._get_default_model_with_warning("eu-west-1") |
| 1564 | + assert model_id == "eu.anthropic.claude-sonnet-4-20250514-v1:0" |
| 1565 | + assert len(captured_warnings) == 0 |
| 1566 | + |
| 1567 | + |
| 1568 | +def test_get_default_model_for_supported_us_region_returns_correct_model_id(captured_warnings): |
| 1569 | + model_id = BedrockModel._get_default_model_with_warning("us-east-1") |
| 1570 | + assert model_id == "us.anthropic.claude-sonnet-4-20250514-v1:0" |
| 1571 | + assert len(captured_warnings) == 0 |
| 1572 | + |
| 1573 | + |
| 1574 | +def test_get_default_model_for_supported_gov_region_returns_correct_model_id(captured_warnings): |
| 1575 | + model_id = BedrockModel._get_default_model_with_warning("us-gov-west-1") |
| 1576 | + assert model_id == "us-gov.anthropic.claude-sonnet-4-20250514-v1:0" |
| 1577 | + assert len(captured_warnings) == 0 |
| 1578 | + |
| 1579 | + |
| 1580 | +def test_get_model_prefix_for_ap_region_converts_to_apac_endpoint(captured_warnings): |
| 1581 | + """Test _get_default_model_with_warning warns for APAC regions since 'ap' is not in supported prefixes.""" |
| 1582 | + model_id = BedrockModel._get_default_model_with_warning("ap-southeast-1") |
| 1583 | + assert model_id == "apac.anthropic.claude-sonnet-4-20250514-v1:0" |
| 1584 | + |
| 1585 | + |
| 1586 | +def test_get_default_model_with_warning_unsupported_region_warns(captured_warnings): |
| 1587 | + """Test _get_default_model_with_warning warns for unsupported regions.""" |
| 1588 | + BedrockModel._get_default_model_with_warning("ca-central-1") |
| 1589 | + assert len(captured_warnings) == 1 |
| 1590 | + assert "This region ca-central-1 does not support" in str(captured_warnings[0].message) |
| 1591 | + assert "our default inference endpoint" in str(captured_warnings[0].message) |
| 1592 | + |
| 1593 | + |
| 1594 | +def test_get_default_model_with_warning_no_warning_with_custom_model_id(captured_warnings): |
| 1595 | + """Test _get_default_model_with_warning doesn't warn when custom model_id provided.""" |
| 1596 | + model_config = {"model_id": "custom-model"} |
| 1597 | + model_id = BedrockModel._get_default_model_with_warning("ca-central-1", model_config) |
| 1598 | + |
| 1599 | + assert model_id == "custom-model" |
| 1600 | + assert len(captured_warnings) == 0 |
| 1601 | + |
| 1602 | + |
| 1603 | +def test_init_with_unsupported_region_warns(session_cls, captured_warnings): |
| 1604 | + """Test BedrockModel initialization warns for unsupported regions.""" |
| 1605 | + BedrockModel(region_name="ca-central-1") |
| 1606 | + |
| 1607 | + assert len(captured_warnings) == 1 |
| 1608 | + assert "This region ca-central-1 does not support" in str(captured_warnings[0].message) |
| 1609 | + |
| 1610 | + |
| 1611 | +def test_init_with_unsupported_region_custom_model_no_warning(session_cls, captured_warnings): |
| 1612 | + """Test BedrockModel initialization doesn't warn when custom model_id provided.""" |
| 1613 | + BedrockModel(region_name="ca-central-1", model_id="custom-model") |
| 1614 | + assert len(captured_warnings) == 0 |
| 1615 | + |
| 1616 | + |
| 1617 | +def test_override_default_model_id_uses_the_overriden_value(captured_warnings): |
| 1618 | + with unittest.mock.patch("strands.models.bedrock.DEFAULT_BEDROCK_MODEL_ID", "custom-overridden-model"): |
| 1619 | + model_id = BedrockModel._get_default_model_with_warning("us-east-1") |
| 1620 | + assert model_id == "custom-overridden-model" |
| 1621 | + |
| 1622 | + |
| 1623 | +def test_no_override_uses_formatted_default_model_id(captured_warnings): |
| 1624 | + model_id = BedrockModel._get_default_model_with_warning("us-east-1") |
| 1625 | + assert model_id == "us.anthropic.claude-sonnet-4-20250514-v1:0" |
| 1626 | + assert model_id != _DEFAULT_BEDROCK_MODEL_ID |
| 1627 | + assert len(captured_warnings) == 0 |
| 1628 | + |
| 1629 | + |
| 1630 | +def test_custom_model_id_not_overridden_by_region_formatting(session_cls): |
| 1631 | + """Test that custom model_id is not overridden by region formatting.""" |
| 1632 | + custom_model_id = "custom.model.id" |
| 1633 | + |
| 1634 | + model = BedrockModel(model_id=custom_model_id) |
| 1635 | + model_id = model.get_config().get("model_id") |
| 1636 | + |
| 1637 | + assert model_id == custom_model_id |
0 commit comments