Skip to content

Commit 66bc1f0

Browse files
committed
fix: only strip known provider prefixes
1 parent 53f455f commit 66bc1f0

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

packages/optimization/src/ldai_optimizer/client.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,40 @@ def _find_model_config(
9191
return global_match if global_match is not None else matching[0]
9292

9393

94+
# Known provider prefixes used by the LD API (e.g. "Anthropic.claude-3").
95+
# Only strip the first segment when it is one of these known values so that
96+
# model IDs whose first dotted segment is NOT a provider — such as Bedrock
97+
# cross-region inference IDs like "us.amazon.nova-pro-v1:0" — are left intact.
98+
_KNOWN_PROVIDER_PREFIXES: frozenset = frozenset({
99+
"Anthropic",
100+
"Bedrock",
101+
"Cohere",
102+
"Google",
103+
"Groq",
104+
"Meta",
105+
"Mistral",
106+
"OpenAI",
107+
"Perplexity",
108+
})
109+
110+
94111
def _strip_provider_prefix(model: str) -> str:
95112
"""Strip the provider prefix from a model identifier returned by the LD API.
96113
97114
API model keys are formatted as "Provider.model-name" (e.g. "OpenAI.gpt-5",
98-
"Anthropic.claude-opus-4.6"). Only the part after the first period is needed
99-
by the underlying LLM clients. If no period is present the string is returned
100-
unchanged.
115+
"Anthropic.claude-opus-4.6"). Only the segment before the first period is
116+
stripped, and only when it is a recognised provider name. This prevents
117+
incorrectly stripping region prefixes from Bedrock cross-region inference
118+
IDs such as "us.amazon.nova-pro-v1:0".
101119
102120
:param model: Raw model string from the API.
103-
:return: Model name with provider prefix removed.
121+
:return: Model name with provider prefix removed, or the original string if
122+
the first segment is not a known provider.
104123
"""
105-
return model.split(".", 1)[-1]
124+
prefix, _, rest = model.partition(".")
125+
if prefix in _KNOWN_PROVIDER_PREFIXES and rest:
126+
return rest
127+
return model
106128

107129

108130
def _compute_validation_count(pool_size: int) -> int:

packages/optimization/tests/test_client.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
_MAX_STANDARD_HISTORY_LENGTH,
1717
_compute_validation_count,
1818
_find_model_config,
19+
_strip_provider_prefix,
1920
_trim_history,
2021
)
2122
from ldai_optimizer.util import judge_passed
@@ -129,6 +130,40 @@ def _make_client(ldai: MagicMock | None = None) -> OptimizationClient:
129130
# ---------------------------------------------------------------------------
130131

131132

133+
# ---------------------------------------------------------------------------
134+
# _strip_provider_prefix
135+
# ---------------------------------------------------------------------------
136+
137+
138+
class TestStripProviderPrefix:
139+
def test_strips_known_anthropic_prefix(self):
140+
assert _strip_provider_prefix("Anthropic.claude-opus-4-5") == "claude-opus-4-5"
141+
142+
def test_strips_known_openai_prefix(self):
143+
assert _strip_provider_prefix("OpenAI.gpt-4o") == "gpt-4o"
144+
145+
def test_strips_known_bedrock_prefix(self):
146+
# "Bedrock.us.amazon.nova-pro-v1:0" → region prefix is preserved
147+
assert _strip_provider_prefix("Bedrock.us.amazon.nova-pro-v1:0") == "us.amazon.nova-pro-v1:0"
148+
149+
def test_does_not_strip_bedrock_region_prefix(self):
150+
# Raw Bedrock cross-region ID has no provider prefix — must be unchanged
151+
assert _strip_provider_prefix("us.amazon.nova-pro-v1:0") == "us.amazon.nova-pro-v1:0"
152+
153+
def test_does_not_strip_eu_region_prefix(self):
154+
assert _strip_provider_prefix("eu.anthropic.claude-3-5-sonnet-20241022-v2:0") == "eu.anthropic.claude-3-5-sonnet-20241022-v2:0"
155+
156+
def test_no_period_returns_unchanged(self):
157+
assert _strip_provider_prefix("gpt-4o") == "gpt-4o"
158+
159+
def test_empty_string_returns_unchanged(self):
160+
assert _strip_provider_prefix("") == ""
161+
162+
def test_preserves_dots_in_model_name_after_stripping(self):
163+
# Multiple dots after the provider prefix are preserved
164+
assert _strip_provider_prefix("Anthropic.claude-3.5-sonnet") == "claude-3.5-sonnet"
165+
166+
132167
# ---------------------------------------------------------------------------
133168
# _find_model_config
134169
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)