Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: more accurate connection checks and clearer errors (#14594)

Michael Hanc12ba613a595
diff --git a/studio/backend/core/inference/external_provider.py b/studio/backend/core/inference/external_provider.py
index 36d3afe3266..ad7e28b15ae 100644
--- a/studio/backend/core/inference/external_provider.py
+++ b/studio/backend/core/inference/external_provider.py
@@ -7672,7 +7672,11 @@ class ExternalProviderClient:
async def _models_payload(self, timeout: Any) -> tuple[Any, list[dict[str, Any]]]:
response = await _client().get(
- f"{self.base_url}/models", headers = self._auth_headers(), timeout = timeout
+ f"{self.base_url}/models",
+ # Anthropic pages 20 models by default; 1000 is its documented maximum.
+ params = {"limit": 1000} if self.provider_type == "anthropic" else None,
+ headers = self._auth_headers(),
+ timeout = timeout,
)
response.raise_for_status()
data = response.json()
@@ -7919,6 +7923,28 @@ class ExternalProviderClient:
)
raise
+ async def api_key_rejected(self) -> bool:
+ """True when an endpoint that needs auth refuses the key. OpenRouter and the Hugging Face
+ router answer /models without one, so that check alone passes any key. Errors other than
+ 401/403 count as not rejected."""
+ if self.provider_type == "openrouter":
+ url = f"{self.base_url}/key"
+ elif (
+ self.provider_type == "huggingface"
+ and (urlparse(self.base_url).hostname or "").lower() == "router.huggingface.co"
+ ):
+ url = "https://huggingface.co/api/whoami-v2"
+ else:
+ return False
+ if not self.api_key:
+ return False
+ try:
+ response = await _client().get(url, headers = self._auth_headers(), timeout = self._timeout)
+ except httpx.HTTPError as exc:
+ logger.debug("API key check for %s skipped: %s", self.provider_type, exc)
+ return False
+ return response.status_code in (401, 403)
+
def _container_headers(self) -> dict[str, str]:
"""Auth headers plus the required ``OpenAI-Beta: containers=v1`` opt-in; without it DELETE
silently no-ops (returns deleted:true but keeps the container, verified 2026-05-15)."""
diff --git a/studio/backend/core/inference/providers.py b/studio/backend/core/inference/providers.py
index 8118239207e..d5598edcca4 100644
--- a/studio/backend/core/inference/providers.py
+++ b/studio/backend/core/inference/providers.py
@@ -10,7 +10,7 @@ import re
import threading
import time
from typing import Any
-from urllib.parse import quote, urlsplit
+from urllib.parse import quote, urlsplit, urlunsplit
PROVIDER_REGISTRY: dict[str, dict[str, Any]] = {
"openai_codex": {
@@ -1098,6 +1098,12 @@ def provider_address_excluding_metadata(url: str) -> str:
return str(addresses[0])
+# The client appends these itself, so a pasted full endpoint would request /chat/completions/models.
+_PASTED_ENDPOINT_PATH = re.compile(
+ r"/(?:chat/completions|completions|responses|messages|models)$", re.IGNORECASE
+)
+
+
def validate_provider_base_url(base_url: str) -> str:
"""Return a normalized provider base URL, or raise ``ValueError``.
@@ -1113,8 +1119,9 @@ def validate_provider_base_url(base_url: str) -> str:
installation (``utils.managed_provider_url_settings``), which is how a team
sharing one LAN model server gets to use it from more than one account.
- Normalization is strip + trailing-slash removal only (what the client did
- before), so validating an already-validated URL returns it unchanged.
+ Normalization is strip, trailing-slash removal and dropping a pasted endpoint
+ path such as ``/chat/completions``, so validating an already-validated URL
+ returns it unchanged.
"""
if not isinstance(base_url, str) or not base_url.strip():
raise ValueError("Provider base URL is required.")
@@ -1158,6 +1165,10 @@ def validate_provider_base_url(base_url: str) -> str:
# Caller-controlled egress: the owner's loopback and LAN, unless the owner opened them.
_reject_non_public(hostname, port, scheme, managed_public_only_reason())
+ path = parts.path.rstrip("/")
+ endpoint = _PASTED_ENDPOINT_PATH.search(path)
+ if endpoint:
+ raw = urlunsplit(parts._replace(path = path[: endpoint.start()]))
return raw.rstrip("/")
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 68fb33f6bb7..1863e7fea00 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -30383,7 +30383,7 @@ async def _proxy_to_external_provider(
# refused target never sees a credential. A managed upstream is Unsloth's own loopback.
if managed is None:
try:
- base_url = validate_provider_base_url(base_url)
+ base_url = await asyncio.to_thread(validate_provider_base_url, base_url)
except ValueError as exc:
raise HTTPException(status_code = 400, detail = str(exc)) from None
@@ -30936,7 +30936,8 @@ async def _proxy_to_external_provider(
subject = current_subject,
)
- client = ExternalProviderClient(
+ client = await asyncio.to_thread(
+ ExternalProviderClient,
provider_type = provider_type,
base_url = base_url,
api_key = api_key,
diff --git a/studio/backend/routes/providers.py b/studio/backend/routes/providers.py
index ef1ff6f6cec..ee4b39d90dd 100644
--- a/studio/backend/routes/providers.py
+++ b/studio/backend/routes/providers.py
@@ -8,10 +8,13 @@ listing.
import asyncio
import hashlib
+import ipaddress
import json
+import re
import time
import uuid
from typing import Optional
+from urllib.parse import urlsplit
import httpx
import structlog
@@ -77,6 +80,67 @@ router = APIRouter(dependencies = [Depends(get_current_subject)])
_MAX_RESPONSES_CONNECTIVITY_MODELS = 5
_PROVIDER_CONNECTIVITY_TIMEOUT_SECONDS = 15.0
+_RESPONSES_PROBE_BUDGET_SECONDS = 20.0
+_API_KEY_REJECTED = "The server rejected the API key."
+# Their /models answers without auth, so only a second request can tell a bad key apart.
+_KEY_CHECKED_PROVIDERS = frozenset({"openrouter", "huggingface"})
+_URL_USERINFO = re.compile(r"(\b[a-z][a-z0-9+.-]*://)[^/\s@'\"]+@", re.IGNORECASE)
+
+
+def _redact_url_userinfo(text: str) -> str:
+ return _URL_USERINFO.sub(r"\1", text)
+
+
+def _unreachable_detail(base_url: str) -> str:
+ try:
+ parts = urlsplit(base_url)
+ host = parts.hostname or ""
+ port = parts.port or (443 if parts.scheme == "https" else 80)
+ except ValueError:
+ return "Couldn't reach the server. Is it running?"
+ try:
+ loopback = host == "localhost" or ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ loopback = False
+ shown = f"[{host}]" if ":" in host else host
+ detail = f"Couldn't reach {shown}:{port}. Is the server running?"
+ if loopback:
+ detail += " If Studio runs in Docker, try host.docker.internal."
+ return detail
+
+
+def _known_provider_error(
+ exc: Exception,
+ base_url: str,
+ *,
+ listing_models: bool = False,
+) -> str | None:
+ if isinstance(exc, httpx.HTTPStatusError):
+ status = exc.response.status_code
+ if status in (401, 403):
+ return _API_KEY_REJECTED
+ if listing_models and status in (404, 405):
+ return "No /models endpoint at this URL (check that it ends in /v1)."
+ elif isinstance(exc, (httpx.ConnectError, httpx.ConnectTimeout)):
+ return _unreachable_detail(base_url)
+ return None
+
+
+def _provider_error_detail(
+ exc: Exception,
+ base_url: str,
+ *,
+ listing_models: bool = False,
+) -> str:
+ return _known_provider_error(
+ exc, base_url, listing_models = listing_models
+ ) or _redact_url_userinfo(safe_curated_detail(exc))
+
+
+def _log_provider_failure(event: str, exc: Exception, **fields) -> None:
+ error = _redact_url_userinfo(str(exc))
+ # A traceback repeats the raw message, so it is kept only when there was nothing to redact.
+ logger.error(event, error = error, exc_info = error == str(exc), **fields)
_SHARED_READ_ONLY = "This connection is shared by the installation owner. Only they can change it."
@@ -294,7 +358,7 @@ async def create_provider_config(
# user fills one in); anything present is checked before a key is decrypted.
if base_url:
try:
- base_url = validate_provider_base_url(base_url)
+ base_url = await asyncio.to_thread(validate_provider_base_url, base_url)
except ValueError as exc:
raise HTTPException(status_code = 400, detail = str(exc)) from None
@@ -432,7 +496,7 @@ async def update_provider_config(
base_url = payload.base_url
if base_url and base_url != existing["base_url"]:
try:
- base_url = validate_provider_base_url(base_url)
+ base_url = await asyncio.to_thread(validate_provider_base_url, base_url)
except ValueError as exc:
raise HTTPException(status_code = 400, detail = str(exc)) from None
@@ -683,19 +747,38 @@ def _bind_saved_provider_target(payload):
status_code = 400,
detail = f"Provider '{config['display_name']}' is disabled.",
)
- return payload.model_copy(
- update = {
- "provider_type": config["provider_type"],
- "base_url": config["base_url"],
- "api_type": config.get("api_type", "chat_completions"),
- }
- )
+ update = {
+ "provider_type": config["provider_type"],
+ "api_type": config.get("api_type", "chat_completions"),
+ }
+ # Pinning the saved endpoint keeps a saved credential off other hosts; a keyless row has none to protect, so an
+ # edited base URL is used as sent.
+ if (
+ not payload.base_url
+ or config["provider_type"] == "openai_codex"
+ or credential_secrets.provider_api_key_saved(config["id"])
+ ):
+ update["base_url"] = config["base_url"]
+ return payload.model_copy(update = update)
+
+
+def _unlisted_model_message(model_id: str, models: list) -> str | None:
+ ids = [m["id"] for m in models if isinstance(m, dict) and isinstance(m.get("id"), str)]
+ if not ids or model_id in ids:
+ return None
+ message = f"Connected, but model '{model_id}' is not listed by this server."
+ near = next((i for i in ids if i.lower() == model_id.lower()), None)
+ if near:
+ return f"{message} Did you mean '{near}'?"
+ shown = ", ".join(ids[:3]) + (", ..." if len(ids) > 3 else "")
+ return f"{message} Available: {shown}"
async def _test_custom_provider_connectivity(
client,
model_id: str,
api_type: str = "chat_completions",
+ base_url: str = "",
) -> ProviderTestResult:
"""Probe a custom OpenAI-compatible endpoint without assuming /chat/completions. TTS-only gateways such as
Kokoro expose ``/models`` and ``/audio/speech`` but not ``/chat/completions``. Try those first, then fall
@@ -711,7 +794,8 @@ async def _test_custom_provider_connectivity(
if models is not None and api_type != "responses":
return ProviderTestResult(
success = True,
- message = f"Connected successfully. Found {len(models)} model(s).",
+ message = (model_id and _unlisted_model_message(model_id, models))
+ or f"Connected successfully. Found {len(models)} model(s).",
models_count = len(models),
)
@@ -748,14 +832,22 @@ async def _test_custom_provider_connectivity(
success = False,
message = (
"Connection failed: could not reach /models and no model ID was "
- f"provided to test further. {safe_curated_detail(models_error)}"
+ f"provided to test further. {_provider_error_detail(models_error, base_url)}"
),
models_count = None,
)
if api_type == "responses":
last_failure = "Responses endpoint returned no completion."
+ # Several unanswered models would otherwise keep the dialog waiting for over a minute.
+ deadline = asyncio.get_running_loop().time() + _RESPONSES_PROBE_BUDGET_SECONDS
for response_model_id in responses_model_ids:
+ probe_timeout = min(
+ _PROVIDER_CONNECTIVITY_TIMEOUT_SECONDS,
+ deadline - asyncio.get_running_loop().time(),
+ )
+ if probe_timeout <= 0:
+ break
try:
received_response = False
model_failure: str | None = None
@@ -779,10 +871,7 @@ async def _test_custom_provider_connectivity(
)
try:
- await asyncio.wait_for(
- consume_response_stream(),
- timeout = _PROVIDER_CONNECTIVITY_TIMEOUT_SECONDS,
- )
+ await asyncio.wait_for(consume_response_stream(), timeout = probe_timeout)
finally:
await response_stream.aclose()
if received_response and model_failure is None:
@@ -792,12 +881,9 @@ async def _test_custom_provider_connectivity(
)
last_failure = model_failure or "Responses endpoint returned no completion."
except asyncio.TimeoutError:
- last_failure = (
- "Responses endpoint timed out after "
- f"{_PROVIDER_CONNECTIVITY_TIMEOUT_SECONDS:g} seconds."
- )
+ last_failure = f"Responses endpoint timed out after {probe_timeout:.0f} seconds."
except Exception as exc:
- last_failure = safe_curated_detail(exc)
+ last_failure = _provider_error_detail(exc, base_url)
return ProviderTestResult(
success = False,
message = f"Connection failed: {last_failure}",
@@ -834,7 +920,7 @@ async def _test_custom_provider_connectivity(
except Exception as exc:
return ProviderTestResult(
success = False,
- message = f"Connection failed: {safe_curated_detail(exc)}",
+ message = f"Connection failed: {_provider_error_detail(exc, base_url)}",
models_count = None,
)
@@ -913,7 +999,7 @@ async def test_provider(
models_count = None,
)
try:
- base_url = validate_provider_base_url(base_url)
+ base_url = await asyncio.to_thread(validate_provider_base_url, base_url)
except ValueError as exc:
return ProviderTestResult(
success = False,
@@ -921,7 +1007,8 @@ async def test_provider(
models_count = None,
)
- client = ExternalProviderClient(
+ client = await asyncio.to_thread(
+ ExternalProviderClient,
provider_type = payload.provider_type,
base_url = base_url,
api_key = api_key,
@@ -953,10 +1040,14 @@ async def test_provider(
return await _test_decision_connectivity(client, (payload.model_id or "").strip())
if payload.provider_type == "custom":
return await _test_custom_provider_connectivity(
- client, payload.model_id or "", payload.api_type
+ client, payload.model_id or "", payload.api_type, base_url
)
if info.get("model_list_mode") == "curated":
await client.verify_models_endpoint_lightweight()
+ if payload.provider_type in _KEY_CHECKED_PROVIDERS and await client.api_key_rejected():
+ return ProviderTestResult(
+ success = False, message = f"Connection failed: {_API_KEY_REJECTED}"
+ )
return ProviderTestResult(
success = True,
message = (
@@ -966,21 +1057,22 @@ async def test_provider(
models_count = None,
)
models = await client.list_models()
+ if payload.provider_type in _KEY_CHECKED_PROVIDERS and await client.api_key_rejected():
+ return ProviderTestResult(
+ success = False, message = f"Connection failed: {_API_KEY_REJECTED}"
+ )
return ProviderTestResult(
success = True,
message = f"Connected successfully. Found {len(models)} model(s).",
models_count = len(models),
)
except Exception as exc:
- logger.error(
- "providers.test_failed",
- provider_type = payload.provider_type,
- error = str(exc),
- exc_info = True,
- )
+ _log_provider_failure("providers.test_failed", exc, provider_type = payload.provider_type)
return ProviderTestResult(
success = False,
- message = f"Connection failed: {safe_curated_detail(exc)}",
+ message = (
+ f"Connection failed: {_provider_error_detail(exc, base_url, listing_models = True)}"
+ ),
models_count = None,
)
finally:
@@ -1093,7 +1185,7 @@ async def _list_model_capabilities(payload: ProviderModelsRequest, via_api_key:
)
base_url = payload.base_url or info["base_url"]
try:
- base_url = validate_provider_base_url(base_url)
+ base_url = await asyncio.to_thread(validate_provider_base_url, base_url)
except ValueError as exc:
raise HTTPException(status_code = 400, detail = str(exc)) from None
@@ -1104,7 +1196,8 @@ async def _list_model_capabilities(payload: ProviderModelsRequest, via_api_key:
if cached is not None and time.monotonic() - cached[0] < _MODEL_CAPABILITY_CACHE_TTL_SECONDS:
return cached[1]
- client = ExternalProviderClient(
+ client = await asyncio.to_thread(
+ ExternalProviderClient,
provider_type = payload.provider_type,
base_url = base_url,
api_key = api_key,
@@ -1125,7 +1218,14 @@ async def _list_model_capabilities(payload: ProviderModelsRequest, via_api_key:
await client.close()
capabilities = provider_model_capabilities(payload.provider_type, models)
if capabilities:
- _model_capability_cache[cache_key] = (time.monotonic(), capabilities)
+ now = time.monotonic()
+ for key in [
+ key
+ for key, (stored_at, _) in _model_capability_cache.items()
+ if now - stored_at >= _MODEL_CAPABILITY_CACHE_TTL_SECONDS
+ ]:
+ del _model_capability_cache[key]
+ _model_capability_cache[cache_key] = (now, capabilities)
return capabilities
@@ -1178,11 +1278,12 @@ async def list_provider_models(
base_url = payload.base_url or info["base_url"]
try:
- base_url = validate_provider_base_url(base_url)
+ base_url = await asyncio.to_thread(validate_provider_base_url, base_url)
except ValueError as exc:
raise HTTPException(status_code = 400, detail = str(exc)) from None
- client = ExternalProviderClient(
+ client = await asyncio.to_thread(
+ ExternalProviderClient,
provider_type = payload.provider_type,
base_url = base_url,
api_key = api_key,
@@ -1272,12 +1373,13 @@ async def list_provider_models(
for m in models
]
except Exception as exc:
- raise log_and_http_error(
- exc,
- 502,
- f"Failed to list models from {payload.provider_type}.",
- event = "providers.list_models_failed",
- log = logger,
+ _log_provider_failure(
+ "providers.list_models_failed", exc, provider_type = payload.provider_type
)
+ detail = _known_provider_error(exc, base_url, listing_models = True)
+ raise HTTPException(
+ status_code = 502,
+ detail = detail or f"Failed to list models from {payload.provider_type}.",
+ ) from None
finally:
await client.close()
diff --git a/studio/backend/tests/test_external_provider_usage_chunk.py b/studio/backend/tests/test_external_provider_usage_chunk.py
index e8b9d8687d1..d99d85b8ce9 100644
--- a/studio/backend/tests/test_external_provider_usage_chunk.py
+++ b/studio/backend/tests/test_external_provider_usage_chunk.py
@@ -348,7 +348,7 @@ def test_custom_provider_test_endpoint_probes_models_before_chat(monkeypatch):
providers_route.ProviderTestRequest(
provider_type = "custom",
base_url = "http://custom.example/v1",
- model_id = "Qwen/Qwen3-0.6B",
+ model_id = "kokoro",
),
_current_subject = "unsloth",
via_api_key = False,
diff --git a/studio/backend/tests/test_provider_connection_checks.py b/studio/backend/tests/test_provider_connection_checks.py
new file mode 100644
index 00000000000..4a5cf5d0a93
--- /dev/null
+++ b/studio/backend/tests/test_provider_connection_checks.py
@@ -0,0 +1,253 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Settings > Connections checks: rejected keys, edited base URLs, readable errors."""
+
+import asyncio
+import threading
+import time
+
+import httpx
+import pytest
+from fastapi import HTTPException
+
+from core.inference import external_provider as ep
+from core.inference.external_provider import ExternalProviderClient
+from core.inference.providers import validate_provider_base_url
+from models.providers import ProviderModelsRequest, ProviderTestRequest
+from routes import providers as providers_route
+from storage import providers_db
+
+
+def _run(coro):
+ return asyncio.new_event_loop().run_until_complete(coro)
+
+
+class _Sent(list):
+ state: dict
+
+
+def mock_http(monkeypatch):
+ """Route the shared client through a handler; returns the list of sent requests."""
+ sent = _Sent()
+ state = {"handler": lambda request: httpx.Response(200, json = {"data": []})}
+
+ def handle(request):
+ sent.append(request)
+ return state["handler"](request)
+
+ monkeypatch.setattr(
+ ep, "_http_client", httpx.AsyncClient(transport = httpx.MockTransport(handle))
+ )
+ monkeypatch.setattr(providers_route, "validate_provider_base_url", lambda url: url.rstrip("/"))
+ monkeypatch.setattr(
+ providers_route, "resolve_provider_api_key_or_400", lambda *a, **k: "sk-bad"
+ )
+ sent.state = state
+ return sent
+
+
+def _test(payload: ProviderTestRequest):
+ return _run(providers_route.test_provider(payload, "tester", False))
+
+
+ "provider_type,check_url",
+ [
+ ("openrouter", "https://openrouter.ai/api/v1/key"),
+ ("huggingface", "https://huggingface.co/api/whoami-v2"),
+ ],
+)
+def test_rejected_key_fails_even_though_models_answers(mock_http, provider_type, check_url):
+ def handle(request):
+ if str(request.url) == check_url:
+ return httpx.Response(401, json = {"error": "invalid"})
+ return httpx.Response(200, json = {"data": [{"id": "m"}]})
+
+ mock_http.state["handler"] = handle
+ result = _test(ProviderTestRequest(provider_type = provider_type))
+ assert not result.success
+ assert "rejected the API key" in result.message
+ assert any(str(r.url) == check_url for r in mock_http)
+ key_request = next(r for r in mock_http if str(r.url) == check_url)
+ assert key_request.headers["authorization"] == "Bearer sk-bad"
+
+
+def test_key_check_network_error_keeps_the_models_result(mock_http):
+ def handle(request):
+ if request.url.path.endswith("/key"):
+ raise httpx.ConnectError("down", request = request)
+ return httpx.Response(200, json = {"data": [{"id": "m"}]})
+
+ mock_http.state["handler"] = handle
+ assert _test(ProviderTestRequest(provider_type = "openrouter")).success
+
+
+def test_huggingface_key_check_stays_on_the_hub_host():
+ client = ExternalProviderClient("huggingface", "https://my-proxy.example/v1", "hf_x")
+ assert _run(client.api_key_rejected()) is False
+
+
+def test_anthropic_lists_past_the_default_page(mock_http):
+ client = ExternalProviderClient("anthropic", "https://api.anthropic.com/v1", "sk-ant")
+ _run(client.list_models())
+ assert mock_http[-1].url.params["limit"] == "1000"
+ _run(ExternalProviderClient("openai", "https://api.openai.com/v1", "sk").list_models())
+ assert "limit" not in mock_http[-1].url.params
+
+
+ "status,expected",
+ [
+ (401, "The server rejected the API key."),
+ (403, "The server rejected the API key."),
+ (404, "No /models endpoint at this URL (check that it ends in /v1)."),
+ (405, "No /models endpoint at this URL (check that it ends in /v1)."),
+ (500, "Failed to list models from custom."),
+ ],
+)
+def test_list_models_errors_say_what_went_wrong(mock_http, status, expected):
+ mock_http.state["handler"] = lambda request: httpx.Response(status)
+ payload = ProviderModelsRequest(provider_type = "custom", base_url = "http://gw.example/v1")
+ with pytest.raises(HTTPException) as caught:
+ _run(providers_route.list_provider_models(payload, "tester", False))
+ assert caught.value.detail == expected
+
+
+def test_unreachable_server_is_named_with_a_docker_hint(mock_http):
+ def refuse(request):
+ raise httpx.ConnectError("All connection attempts failed", request = request)
+
+ mock_http.state["handler"] = refuse
+ payload = ProviderModelsRequest(provider_type = "custom", base_url = "http://localhost:11434/v1")
+ with pytest.raises(HTTPException) as caught:
+ _run(providers_route.list_provider_models(payload, "tester", False))
+ assert caught.value.detail == (
+ "Couldn't reach localhost:11434. Is the server running? "
+ "If Studio runs in Docker, try host.docker.internal."
+ )
+ result = _test(ProviderTestRequest(provider_type = "custom", base_url = "http://10.0.0.9:8000/v1"))
+ assert not result.success
+ assert "Couldn't reach 10.0.0.9:8000. Is the server running?" in result.message
+ assert "Docker" not in result.message
+ assert "All connection attempts failed" not in result.message
+
+
+def test_unlisted_model_still_connects_with_a_hint(mock_http):
+ mock_http.state["handler"] = lambda request: httpx.Response(
+ 200, json = {"data": [{"id": "Qwen3-8B"}, {"id": "llama3"}]}
+ )
+ request = ProviderTestRequest(
+ provider_type = "custom", base_url = "http://gw.example/v1", model_id = "qwen3-8b"
+ )
+ result = _test(request)
+ assert result.success
+ assert result.message == (
+ "Connected, but model 'qwen3-8b' is not listed by this server. Did you mean 'Qwen3-8B'?"
+ )
+ result = _test(request.model_copy(update = {"model_id": "gpt-x"}))
+ assert result.success
+ assert result.message.endswith("Available: Qwen3-8B, llama3")
+ result = _test(request.model_copy(update = {"model_id": "llama3"}))
+ assert result.message == "Connected successfully. Found 2 model(s)."
+
+
+def test_basic_auth_credentials_are_not_echoed(mock_http, monkeypatch):
+ mock_http.state["handler"] = lambda request: httpx.Response(500)
+ logged = []
+ monkeypatch.setattr(providers_route.logger, "error", lambda *a, **k: logged.append((a, k)))
+ result = _test(
+ ProviderTestRequest(provider_type = "openai", base_url = "http://user:[email protected]/v1")
+ )
+ assert not result.success
+ assert "hunter2" not in result.message and "gw.example" in result.message
+ assert logged and "hunter2" not in repr(logged)
+
+
+def test_responses_probe_stops_at_the_total_budget(monkeypatch):
+ monkeypatch.setattr(providers_route, "_RESPONSES_PROBE_BUDGET_SECONDS", 0.3)
+ monkeypatch.setattr(providers_route, "_PROVIDER_CONNECTIVITY_TIMEOUT_SECONDS", 0.2)
+ tried = []
+
+ class _SlowClient:
+ async def list_models(self):
+ return [{"id": f"m{i}"} for i in range(5)]
+
+ def stream_chat_completion(self, *, model, **_kwargs):
+ tried.append(model)
+
+ async def stream():
+ await asyncio.sleep(10)
+ yield ""
+
+ return stream()
+
+ started = time.monotonic()
+ result = _run(
+ providers_route._test_custom_provider_connectivity(_SlowClient(), "", "responses")
+ )
+ assert not result.success and "timed out" in result.message
+ # Coarse timers (Windows) can leave a sliver of budget for one more short probe.
+ assert tried[:2] == ["m0", "m1"] and len(tried) <= 3
+ assert time.monotonic() - started < 1.0
+
+
+def _saved_row(monkeypatch, tmp_path, *, has_key: bool):
+ monkeypatch.setattr(providers_db, "studio_db_path", lambda: tmp_path / "studio.db")
+ providers_db.reset_schema_state_for_tests()
+ providers_db.create_provider(
+ id = "row", provider_type = "custom", display_name = "Local", base_url = "http://a.example/v1"
+ )
+ monkeypatch.setattr(
+ providers_route.credential_secrets, "provider_api_key_saved", lambda _id: has_key
+ )
+
+
+def test_keyless_row_lists_models_from_the_edited_base_url(monkeypatch, tmp_path):
+ _saved_row(monkeypatch, tmp_path, has_key = False)
+ payload = ProviderModelsRequest(
+ provider_type = "custom", provider_id = "row", base_url = "http://b.example/v1"
+ )
+ assert providers_route._bind_saved_provider_target(payload).base_url == "http://b.example/v1"
+ unset = payload.model_copy(update = {"base_url": None})
+ assert providers_route._bind_saved_provider_target(unset).base_url == "http://a.example/v1"
+
+
+def test_a_saved_key_stays_pinned_to_the_saved_base_url(monkeypatch, tmp_path):
+ _saved_row(monkeypatch, tmp_path, has_key = True)
+ payload = ProviderModelsRequest(
+ provider_type = "custom", provider_id = "row", base_url = "http://b.example/v1"
+ )
+ assert providers_route._bind_saved_provider_target(payload).base_url == "http://a.example/v1"
+
+
+def test_base_url_validation_runs_off_the_event_loop(mock_http, monkeypatch):
+ threads = []
+
+ def record(url):
+ threads.append(threading.current_thread())
+ return url
+
+ monkeypatch.setattr(providers_route, "validate_provider_base_url", record)
+ _test(ProviderTestRequest(provider_type = "custom", base_url = "http://gw.example/v1"))
+ payload = ProviderModelsRequest(provider_type = "custom", base_url = "http://gw.example/v1")
+ _run(providers_route.list_provider_models(payload, "tester", False))
+ assert len(threads) == 2
+ assert all(thread is not threading.main_thread() for thread in threads)
+
+
+ "pasted,base",
+ [
+ ("http://127.0.0.1:11434/v1/chat/completions", "http://127.0.0.1:11434/v1"),
+ ("http://127.0.0.1:11434/v1/completions/", "http://127.0.0.1:11434/v1"),
+ ("http://127.0.0.1:8000/v1/responses", "http://127.0.0.1:8000/v1"),
+ ("http://127.0.0.1:8000/v1/Models", "http://127.0.0.1:8000/v1"),
+ ("http://127.0.0.1:8000/v1/messages?x=1", "http://127.0.0.1:8000/v1?x=1"),
+ ("http://127.0.0.1:8000/v1", "http://127.0.0.1:8000/v1"),
+ ],
+)
+def test_pasted_endpoint_paths_normalise_to_the_base(pasted, base):
+ assert validate_provider_base_url(pasted) == base
+ assert validate_provider_base_url(base) == base
diff --git a/studio/frontend/src/features/chat/chat-providers-dialog.tsx b/studio/frontend/src/features/chat/chat-providers-dialog.tsx
index 6b4c9adfcbb..a481864c696 100644
--- a/studio/frontend/src/features/chat/chat-providers-dialog.tsx
+++ b/studio/frontend/src/features/chat/chat-providers-dialog.tsx
@@ -1,6 +1,16 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+import {
+ AlertDialog,
+ AlertDialogAction,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import {
@@ -64,6 +74,7 @@ import {
type CustomReasoningConfig,
normalizeCustomReasoningConfig,
} from "./custom-reasoning";
+import { normalizeProviderBaseUrl } from "./provider-base-url";
import { resolveProviderCredentialEdit } from "./provider-credential-edit";
import { getExternalMinOutputTokens } from "./provider-capabilities";
import type { ExternalProviderConfig } from "./external-providers";
@@ -313,6 +324,11 @@ export function ChatProvidersSettings({
// Only the newest Codex catalog request may write to the form.
const codexCatalogRequestRef = useRef(0);
const [mutatingProvider, setMutatingProvider] = useState(false);
+ const [testingProviderIds, setTestingProviderIds] = useState<
+ ReadonlySet<string>
+ >(new Set());
+ const [pendingDeleteProvider, setPendingDeleteProvider] =
+ useState<ExternalProviderConfig | null>(null);
const [manualModelIds, setManualModelIds] = useState("");
const [modelSearchQuery, setModelSearchQuery] = useState("");
const [customProviderName, setCustomProviderName] = useState(
@@ -669,30 +685,6 @@ export function ChatProvidersSettings({
setSelectedModelIds((prev) => prev.filter((id) => !visible.has(id)));
}
- function parseOptionalBaseUrl(
- input: string,
- options: { appendOpenAiVersionPath?: boolean } = {},
- ): string | null {
- const trimmed = input.trim();
- if (!trimmed) return null;
- let parsed: URL;
- try {
- parsed = new URL(trimmed);
- } catch {
- throw new Error("Base URL must be a valid URL.");
- }
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
- throw new Error("Base URL must use http or https.");
- }
- if (options.appendOpenAiVersionPath) {
- const pathname = parsed.pathname.replace(/\/+$/, "");
- if (!pathname) {
- parsed.pathname = "/v1";
- }
- }
- return parsed.toString().replace(/\/+$/, "");
- }
-
function parseBaseUrlForProvider(
input: string,
required: boolean,
@@ -705,7 +697,7 @@ export function ChatProvidersSettings({
}
return null;
}
- const baseUrl = parseOptionalBaseUrl(trimmed, {
+ const baseUrl = normalizeProviderBaseUrl(trimmed, {
appendOpenAiVersionPath: shouldAppendOpenAiVersionPath(providerTypeForUrl),
});
return apiType === "systemone"
@@ -780,6 +772,15 @@ export function ChatProvidersSettings({
toast.error("Add an API key first.");
return;
}
+ if (
+ editingProvider?.hasApiKey &&
+ !apiKey.trim() &&
+ baseUrlDraft.trim() !== editingProvider.baseUrl
+ ) {
+ // The saved key only goes to the saved server, so the backend would list that server's models.
+ toast.info("Save the new Base URL first, then load its models.");
+ return;
+ }
setModelsLoading(true);
try {
const baseUrl = parseBaseUrlForProvider(
@@ -1449,7 +1450,20 @@ export function ChatProvidersSettings({
}
async function testProvider(provider: ExternalProviderConfig) {
+ if (testingProviderIds.has(provider.id)) return;
+ setTestingProviderIds((prev) => new Set(prev).add(provider.id));
+ try {
+ await runProviderTest(provider);
+ } finally {
+ setTestingProviderIds((prev) => {
+ const next = new Set(prev);
+ next.delete(provider.id);
+ return next;
+ });
+ }
+ }
+ async function runProviderTest(provider: ExternalProviderConfig) {
if (provider.authKind === "chatgpt_oauth") {
if (provider.authStatus === "connected") {
toast.success("ChatGPT subscription is connected.");
@@ -1496,24 +1510,10 @@ export function ChatProvidersSettings({
if (result.success) {
toast.success(result.message);
} else {
- if (
- isCustomProviderType(provider.providerType) &&
- result.message.includes("Illegal header value b'Bearer '")
- ) {
- toast.error(CUSTOM_PROVIDER_MISSING_KEY_MESSAGE);
- return;
- }
toast.error(result.message);
}
} catch (error) {
const message = error instanceof Error ? error.message : "Unknown error";
- if (
- isCustomProviderType(provider.providerType) &&
- message.includes("Illegal header value b'Bearer '")
- ) {
- toast.error(CUSTOM_PROVIDER_MISSING_KEY_MESSAGE);
- return;
- }
toast.error(`Test failed: ${message}`);
}
}
@@ -2491,12 +2491,20 @@ export function ChatProvidersSettings({
size="icon-sm"
variant="ghost"
className="size-7 hover:text-foreground"
- disabled={mutatingProvider}
+ disabled={
+ mutatingProvider ||
+ testingProviderIds.has(provider.id)
+ }
+ aria-busy={testingProviderIds.has(provider.id)}
onClick={() => void testProvider(provider)}
title="Check connection"
aria-label={`Check ${provider.name}`}
>
- <HugeiconsIcon icon={Wifi02Icon} className="size-4" />
+ {testingProviderIds.has(provider.id) ? (
+ <Spinner className="size-4" />
+ ) : (
+ <HugeiconsIcon icon={Wifi02Icon} className="size-4" />
+ )}
</Button>
{provider.sharedByOwner ? null : (
<Button
@@ -2505,7 +2513,7 @@ export function ChatProvidersSettings({
variant="ghost"
className="size-7 hover:text-destructive"
disabled={mutatingProvider}
- onClick={() => void deleteProvider(provider.id)}
+ onClick={() => setPendingDeleteProvider(provider)}
title="Delete connection"
aria-label={`Delete ${provider.name}`}
>
@@ -2520,6 +2528,36 @@ export function ChatProvidersSettings({
)}
</div>
</section>
+ <AlertDialog
+ open={pendingDeleteProvider !== null}
+ onOpenChange={(open) => {
+ if (!open) setPendingDeleteProvider(null);
+ }}
+ >
+ <AlertDialogContent>
+ <AlertDialogHeader>
+ <AlertDialogTitle>
+ Delete {pendingDeleteProvider?.name ?? "connection"}?
+ </AlertDialogTitle>
+ <AlertDialogDescription>
+ This removes the connection and its saved API key.
+ </AlertDialogDescription>
+ </AlertDialogHeader>
+ <AlertDialogFooter>
+ <AlertDialogCancel>Cancel</AlertDialogCancel>
+ <AlertDialogAction
+ variant="destructive"
+ onClick={() => {
+ const provider = pendingDeleteProvider;
+ setPendingDeleteProvider(null);
+ if (provider) void deleteProvider(provider.id);
+ }}
+ >
+ Delete
+ </AlertDialogAction>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
</div>
);
}
diff --git a/studio/frontend/src/features/chat/provider-base-url.ts b/studio/frontend/src/features/chat/provider-base-url.ts
new file mode 100644
index 00000000000..aa21eb6b9bc
--- /dev/null
+++ b/studio/frontend/src/features/chat/provider-base-url.ts
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+const SCHEMELESS_HOST = /^(\[[0-9a-f:.]+\]|[a-z0-9.-]+)(:\d+)?([/?#].*)?$/i;
+// Pasted full endpoints: the client appends these itself, so keeping one requests /chat/completions/models.
+const ENDPOINT_SUFFIX =
+ /\/(chat\/completions|completions|responses|messages|models)\/*$/i;
+
+export function normalizeProviderBaseUrl(
+ input: string,
+ options: { appendOpenAiVersionPath?: boolean } = {},
+): string | null {
+ let trimmed = input.trim();
+ if (!trimmed) return null;
+ if (!trimmed.includes("://") && SCHEMELESS_HOST.test(trimmed)) {
+ trimmed = `http://${trimmed}`;
+ }
+ let parsed: URL;
+ try {
+ parsed = new URL(trimmed);
+ } catch {
+ throw new Error("Base URL must be a valid URL.");
+ }
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
+ throw new Error("Base URL must use http or https.");
+ }
+ parsed.pathname = parsed.pathname.replace(ENDPOINT_SUFFIX, "");
+ if (options.appendOpenAiVersionPath) {
+ const pathname = parsed.pathname.replace(/\/+$/, "");
+ if (!pathname) {
+ parsed.pathname = "/v1";
+ }
+ }
+ return parsed.toString().replace(/\/+$/, "");
+}
diff --git a/studio/frontend/tests/provider-base-url-normalize.test.ts b/studio/frontend/tests/provider-base-url-normalize.test.ts
new file mode 100644
index 00000000000..cbf9113dc3a
--- /dev/null
+++ b/studio/frontend/tests/provider-base-url-normalize.test.ts
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+import { normalizeProviderBaseUrl } from "../src/features/chat/provider-base-url.ts";
+
+const local = { appendOpenAiVersionPath: true };
+
+test("a host without a scheme is read as http", () => {
+ assert.equal(
+ normalizeProviderBaseUrl("localhost:11434", local),
+ "http://localhost:11434/v1",
+ );
+ assert.equal(
+ normalizeProviderBaseUrl("192.168.1.5:8000/v1"),
+ "http://192.168.1.5:8000/v1",
+ );
+ assert.equal(
+ normalizeProviderBaseUrl("[::1]:8080", local),
+ "http://[::1]:8080/v1",
+ );
+ assert.equal(normalizeProviderBaseUrl("gpu-box", local), "http://gpu-box/v1");
+ assert.equal(
+ normalizeProviderBaseUrl("https://api.example.com/v1/"),
+ "https://api.example.com/v1",
+ );
+});
+
+test("other schemes still fail", () => {
+ assert.throws(
+ () => normalizeProviderBaseUrl("ftp://host/v1"),
+ /http or https/,
+ );
+ assert.throws(() => normalizeProviderBaseUrl("http://"), /valid URL/);
+});
+
+test("a pasted endpoint is trimmed back to its base", () => {
+ for (const suffix of [
+ "chat/completions",
+ "completions",
+ "responses",
+ "messages",
+ "models",
+ "chat/completions/",
+ ]) {
+ assert.equal(
+ normalizeProviderBaseUrl(`http://localhost:11434/v1/${suffix}`, local),
+ "http://localhost:11434/v1",
+ );
+ }
+ assert.equal(
+ normalizeProviderBaseUrl("localhost:8080/chat/completions", local),
+ "http://localhost:8080/v1",
+ );
+ assert.equal(
+ normalizeProviderBaseUrl("https://gw.example/openai/v1"),
+ "https://gw.example/openai/v1",
+ );
+ assert.equal(normalizeProviderBaseUrl(" "), null);
+});