Skip to content

Commit 86273f2

Browse files
fix: validate providers only when litellm provider registry is available
Co-authored-by: openhands <openhands@all-hands.dev>
1 parent 8f1e553 commit 86273f2

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

openhands-sdk/openhands/sdk/llm/utils/unverified_models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ def _get_litellm_provider_names() -> set[str]:
111111
_LITELLM_PROVIDER_NAMES = _get_litellm_provider_names()
112112

113113

114+
def _has_provider_registry() -> bool:
115+
return bool(_LITELLM_PROVIDER_NAMES)
116+
117+
114118
def _extract_model_and_provider(model: str) -> tuple[str, str, str]:
115119
"""Extract provider and model information from a model identifier.
116120
@@ -147,7 +151,10 @@ def _extract_model_and_provider(model: str) -> tuple[str, str, str]:
147151
provider = split[0]
148152
model_id = separator.join(split[1:])
149153

150-
if provider not in _LITELLM_PROVIDER_NAMES:
154+
# If we have a provider registry, only accept known LiteLLM providers.
155+
# If not, fall back to the historical behavior to avoid dropping all
156+
# provider groupings in older LiteLLM versions.
157+
if _has_provider_registry() and provider not in _LITELLM_PROVIDER_NAMES:
151158
return "", model, ""
152159

153160
return provider, model_id, separator

tests/sdk/llm/test_model_list.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def test_unverified_models_fallback_when_no_provider_list():
4949
"openai/gpt-4o", # treated as unverified (provider validation disabled)
5050
"anthropic/claude-sonnet-4-20250514", # treated as unverified
5151
"o3", # openhands model -> excluded
52-
"custom-provider/custom-model", # invalid provider -> bucketed under "other"
53-
"us.anthropic.claude-3-5-sonnet-20241022-v2:0", # invalid provider prefix
54-
"1024-x-1024/gpt-image-1.5", # invalid provider prefix
52+
"custom-provider/custom-model",
53+
"us.anthropic.claude-3-5-sonnet-20241022-v2:0",
54+
"1024-x-1024/gpt-image-1.5",
5555
]
5656

5757
with (
@@ -66,10 +66,17 @@ def test_unverified_models_fallback_when_no_provider_list():
6666
):
6767
result = get_unverified_models()
6868

69+
# Without provider validation, openai/anthropic entries are treated as
70+
# verified and therefore are excluded from the unverified mapping.
6971
assert "openai" not in result
7072
assert "anthropic" not in result
73+
74+
# When LiteLLM doesn't expose a provider registry, we keep historical behavior
75+
# (split on '/' or '.') rather than bucketing everything under "other".
7176
assert result == {
72-
"other": ["openai/gpt-4o", "anthropic/claude-sonnet-4-20250514"] + models[3:]
77+
"custom-provider": ["custom-model"],
78+
"us": ["anthropic.claude-3-5-sonnet-20241022-v2:0"],
79+
"1024-x-1024": ["gpt-image-1.5"],
7380
}
7481

7582

0 commit comments

Comments
 (0)