π OpenHands: AI-Driven Development
fix: surface unverified bare LLM names in the provider dropdowns (#14209)
Joe Lavertyf591d66fe67e
diff --git a/openhands/utils/llm.py b/openhands/utils/llm.pyindex 54a3c8876d..7b77e44110 100644--- a/openhands/utils/llm.py+++ b/openhands/utils/llm.py@@ -230,8 +230,10 @@ def _assign_provider(model: str) -> str:"""Prefix a bare model name with its canonical provider.Models that already contain a ``/`` provider separator are returned- unchanged. Only well-known bare names (OpenAI, Anthropic, Mistral)- are prefixed.+ unchanged. Bare names are first checked against the SDK's verified+ sets (cheap, no network), then fall back to LiteLLM's own routing+ tables so that unverified names like ``claude-opus-4-7`` or+ ``gemini-2.0-flash`` still reach the provider-keyed dropdown."""if '/' in model:return model@@ -244,7 +246,12 @@ def _assign_provider(model: str) -> str:return f'anthropic/{model}'if model in _BARE_MISTRAL_MODELS:return f'mistral/{model}'- return model++ try:+ _, provider, _, _ = get_llm_provider(model)+ except Exception:+ return model+ return f'{provider}/{model}' if provider else modeldef _derive_verified_models(openhands_models: list[str]) -> list[str]:diff --git a/tests/unit/utils/test_llm_utils.py b/tests/unit/utils/test_llm_utils.pyindex 665a20a997..6bd0b99c65 100644--- a/tests/unit/utils/test_llm_utils.py+++ b/tests/unit/utils/test_llm_utils.py@@ -60,15 +60,52 @@ class TestAssignProvider:_assign_provider('mistral-large-latest') == 'mistral/mistral-large-latest')- def test_prefixed_and_unknown_models_remain_unchanged(self, monkeypatch):- """Test that only known bare models are rewritten."""+ def test_prefixed_models_remain_unchanged(self, monkeypatch):+ """Test that already-prefixed models are returned untouched."""monkeypatch.setattr(llm_utils, '_BARE_OPENAI_MODELS', {'gpt-5.2'})monkeypatch.setattr(llm_utils, '_BARE_ANTHROPIC_MODELS', set())monkeypatch.setattr(llm_utils, '_BARE_MISTRAL_MODELS', set())assert _assign_provider('openai/gpt-5.2') == 'openai/gpt-5.2'- assert _assign_provider('cohere.command-r-v1:0') == 'cohere.command-r-v1:0'- assert _assign_provider('custom-model') == 'custom-model'++ def test_unresolvable_bare_models_remain_unchanged(self, monkeypatch):+ """Bare names LiteLLM cannot resolve fall through unchanged."""+ monkeypatch.setattr(llm_utils, '_BARE_OPENAI_MODELS', set())+ monkeypatch.setattr(llm_utils, '_BARE_ANTHROPIC_MODELS', set())+ monkeypatch.setattr(llm_utils, '_BARE_MISTRAL_MODELS', set())++ assert _assign_provider('totally-made-up-model-xyz') == (+ 'totally-made-up-model-xyz'+ )++ def test_unverified_bare_models_use_litellm_fallback(self, monkeypatch):+ """Unverified bare names reach the dropdown via LiteLLM's routing."""+ monkeypatch.setattr(llm_utils, '_BARE_OPENAI_MODELS', set())+ monkeypatch.setattr(llm_utils, '_BARE_ANTHROPIC_MODELS', set())+ monkeypatch.setattr(llm_utils, '_BARE_MISTRAL_MODELS', set())++ # gemini-* lives bare in litellm.model_cost; LiteLLM routes it to+ # vertex_ai. Without the fallback the frontend's provider filter+ # drops it entirely.+ assert _assign_provider('gemini-2.0-flash') == 'vertex_ai/gemini-2.0-flash'+ # cohere.<model>:<rev> is the Bedrock-style ID for Cohere models;+ # LiteLLM resolves it to bedrock.+ assert (+ _assign_provider('cohere.command-r-v1:0') == 'bedrock/cohere.command-r-v1:0'+ )++ def test_litellm_fallback_exception_is_swallowed(self, monkeypatch):+ """A raising get_llm_provider must not break the model list build."""+ monkeypatch.setattr(llm_utils, '_BARE_OPENAI_MODELS', set())+ monkeypatch.setattr(llm_utils, '_BARE_ANTHROPIC_MODELS', set())+ monkeypatch.setattr(llm_utils, '_BARE_MISTRAL_MODELS', set())++ def _boom(*_args, **_kwargs):+ raise RuntimeError('litellm exploded')++ monkeypatch.setattr(llm_utils, 'get_llm_provider', _boom)++ assert _assign_provider('whatever') == 'whatever'class TestDeriveVerifiedModels: