|
1 | | -from typing import cast |
| 1 | +from typing import cast, get_args |
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 | from pytest_mock import MockerFixture |
|
16 | 16 | with try_import() as imports_successful: |
17 | 17 | from mypy_boto3_bedrock_runtime import BedrockRuntimeClient |
18 | 18 |
|
19 | | - from pydantic_ai.providers.bedrock import BedrockModelProfile, BedrockProvider |
| 19 | + from pydantic_ai.models.bedrock import LatestBedrockModelNames |
| 20 | + from pydantic_ai.providers.bedrock import BEDROCK_GEO_PREFIXES, BedrockModelProfile, BedrockProvider |
20 | 21 |
|
21 | 22 |
|
22 | 23 | pytestmark = pytest.mark.skipif(not imports_successful(), reason='bedrock not installed') |
@@ -100,3 +101,51 @@ def test_bedrock_provider_model_profile(env: TestEnv, mocker: MockerFixture): |
100 | 101 |
|
101 | 102 | unknown_model = provider.model_profile('unknown.unknown-model') |
102 | 103 | assert unknown_model is None |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.parametrize('prefix', BEDROCK_GEO_PREFIXES) |
| 107 | +def test_bedrock_provider_model_profile_all_geo_prefixes(env: TestEnv, prefix: str): |
| 108 | + """Test that all cross-region inference geo prefixes are correctly handled.""" |
| 109 | + env.set('AWS_DEFAULT_REGION', 'us-east-1') |
| 110 | + provider = BedrockProvider() |
| 111 | + |
| 112 | + model_name = f'{prefix}.anthropic.claude-sonnet-4-5-20250929-v1:0' |
| 113 | + profile = provider.model_profile(model_name) |
| 114 | + |
| 115 | + assert profile is not None, f'model_profile returned None for {model_name}' |
| 116 | + |
| 117 | + |
| 118 | +def test_bedrock_provider_model_profile_with_unknown_geo_prefix(env: TestEnv): |
| 119 | + env.set('AWS_DEFAULT_REGION', 'us-east-1') |
| 120 | + provider = BedrockProvider() |
| 121 | + |
| 122 | + model_name = 'narnia.anthropic.claude-sonnet-4-5-20250929-v1:0' |
| 123 | + profile = provider.model_profile(model_name) |
| 124 | + assert profile is None, f'model_profile returned {profile} for {model_name}' |
| 125 | + |
| 126 | + |
| 127 | +def test_latest_bedrock_model_names_geo_prefixes_are_supported(): |
| 128 | + """Ensure all geo prefixes used in LatestBedrockModelNames are in BEDROCK_GEO_PREFIXES. |
| 129 | +
|
| 130 | + This test prevents adding new model names with geo prefixes that aren't handled |
| 131 | + by the provider's model_profile method. |
| 132 | + """ |
| 133 | + model_names = get_args(LatestBedrockModelNames) |
| 134 | + |
| 135 | + missing_prefixes: set[str] = set() |
| 136 | + |
| 137 | + for model_name in model_names: |
| 138 | + # Model names with geo prefixes have 3+ dot-separated parts: |
| 139 | + # - No prefix: "anthropic.claude-xxx" (2 parts) |
| 140 | + # - With prefix: "us.anthropic.claude-xxx" (3 parts) |
| 141 | + parts = model_name.split('.') |
| 142 | + if len(parts) >= 3: |
| 143 | + geo_prefix = parts[0] |
| 144 | + if geo_prefix not in BEDROCK_GEO_PREFIXES: # pragma: no cover |
| 145 | + missing_prefixes.add(geo_prefix) |
| 146 | + |
| 147 | + if missing_prefixes: # pragma: no cover |
| 148 | + pytest.fail( |
| 149 | + f'Found geo prefixes in LatestBedrockModelNames that are not in BEDROCK_GEO_PREFIXES: {missing_prefixes}. ' |
| 150 | + f'Please add them to BEDROCK_GEO_PREFIXES' |
| 151 | + ) |
0 commit comments