Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions openhands-sdk/openhands/sdk/llm/utils/model_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from litellm.utils import get_model_info
from pydantic import SecretStr

from openhands.sdk.llm.utils.openhands_provider import litellm_call_kwargs
from openhands.sdk.llm.utils.openhands_provider import (
is_openhands_proxy_base_url,
litellm_call_kwargs,
)


logger = getLogger(__name__)
Expand Down Expand Up @@ -93,10 +96,18 @@ def _get_model_info_from_litellm_proxy(
cache_key: int | None = None,
):
logger.debug(f"Get model_info_from_litellm_proxy:{cache_key}")
if isinstance(secret_api_key, SecretStr):
secret_api_key = secret_api_key.get_secret_value()
has_key = bool(secret_api_key and secret_api_key.strip())
if not has_key and is_openhands_proxy_base_url(base_url):
# The managed proxy always requires auth: without credentials the
# lookup cannot succeed, so skip it instead of collecting a 401.
# Callers fall back to static LiteLLM metadata. Generic proxies are
# untouched: an open proxy may legitimately serve model info without
# credentials.
return None
try:
headers = {}
if isinstance(secret_api_key, SecretStr):
secret_api_key = secret_api_key.get_secret_value()
if secret_api_key:
headers["Authorization"] = f"Bearer {secret_api_key}"

Expand Down
79 changes: 79 additions & 0 deletions tests/sdk/llm/test_model_info_keyless.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""Managed-proxy model-info lookups without credentials emit no request."""

from unittest.mock import MagicMock, patch

from openhands.sdk.llm.utils.model_info import _get_model_info_from_litellm_proxy
from openhands.sdk.llm.utils.openhands_provider import (
OPENHANDS_LLM_PROXY_BASE_URL,
)


def _proxy_response(payload):
response = MagicMock()
response.json.return_value = payload
return response


def _lookup(secret_api_key, base_url, cache_key):
return _get_model_info_from_litellm_proxy(
secret_api_key=secret_api_key,
base_url=base_url,
model="litellm_proxy/some-model",
cache_key=cache_key,
)


def test_managed_keyless_lookup_skips_request():
with patch("openhands.sdk.llm.utils.model_info.httpx.get") as mock_get:
result = _lookup(None, OPENHANDS_LLM_PROXY_BASE_URL, 201)
mock_get.assert_not_called()
assert result is None


def test_managed_empty_key_lookup_skips_request():
with patch("openhands.sdk.llm.utils.model_info.httpx.get") as mock_get:
result = _lookup("", OPENHANDS_LLM_PROXY_BASE_URL, 202)
mock_get.assert_not_called()
assert result is None


def test_managed_blank_key_lookup_skips_request():
with patch("openhands.sdk.llm.utils.model_info.httpx.get") as mock_get:
result = _lookup(" ", OPENHANDS_LLM_PROXY_BASE_URL, 203)
mock_get.assert_not_called()
assert result is None


def test_managed_v1_suffixed_url_lookup_skips_request():
with patch("openhands.sdk.llm.utils.model_info.httpx.get") as mock_get:
result = _lookup(None, f"{OPENHANDS_LLM_PROXY_BASE_URL}/v1/", 204)
mock_get.assert_not_called()
assert result is None


def test_managed_keyed_lookup_sends_authorization_header():
payload = {"data": [{"model_name": "some-model", "model_info": {"key": "abc"}}]}
with patch(
"openhands.sdk.llm.utils.model_info.httpx.get",
return_value=_proxy_response(payload),
) as mock_get:
result = _lookup("secret", OPENHANDS_LLM_PROXY_BASE_URL, 205)
mock_get.assert_called_once_with(
f"{OPENHANDS_LLM_PROXY_BASE_URL}/v1/model/info",
headers={"Authorization": "Bearer secret"},
)
assert result == {"key": "abc"}


def test_generic_proxy_keyless_lookup_still_emits_request():
"""Generic proxies are untouched: an open proxy may serve model info
without credentials, so the lookup must still be attempted."""
with patch(
"openhands.sdk.llm.utils.model_info.httpx.get",
return_value=_proxy_response({"data": []}),
) as mock_get:
result = _lookup(None, "http://proxy.local:4000", 206)
mock_get.assert_called_once_with(
"http://proxy.local:4000/v1/model/info", headers={}
)
assert result is None
Loading