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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Engine probes answer JSON or 404, never SPA HTML: /props, /v1/props, /version, owner-only GET
/metrics, GET /slots, POST /tokenize, POST /detokenize, GET /models, POST /models/load,
/models/unload; other llama-server routes 404; Ollama /api/tags and /api/show not served."""
from __future__ import annotations
import asyncio
import functools
import threading
from typing import Any, Optional
import httpx
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import JSONResponse, Response
from pydantic import BaseModel, Field
from auth.authentication import get_current_subject
from loggers import get_logger
logger = get_logger(__name__)
router = APIRouter()
# llama-server route table minus /props; bare paths only, so it cannot shadow /api/ or /v1/
_ENGINE_PROBE_PATHS = frozenset(
{
"apply-template",
"audio/transcriptions",
"chat/completions",
"chat/completions/input_tokens",
"completion",
"completions",
"cors-proxy",
"detokenize",
"embedding",
"embeddings",
"health",
"infill",
"lora-adapters",
"metrics",
"models",
"models/load",
"models/sse",
"models/unload",
"rerank",
"reranking",
"responses",
"responses/input_tokens",
"slots",
"tokenize",
"tools",
}
)
# /slots/:id_slot is the one dynamic entry in that table, and Studio calls it itself.
_ENGINE_PROBE_PREFIXES = ("slots/",)
# Forwarded to the resident llama-server (#11705). Slot save / restore stays denied: Studio owns slot persistence.
_PASSTHROUGH_ROUTES = frozenset(
{
("GET", "metrics"),
("GET", "slots"),
("POST", "tokenize"),
("POST", "detokenize"),
}
)
_PASSTHROUGH_TIMEOUT_S = 30.0
# Open WebUI's llama.cpp model management (#10610). Download, delete and /models/sse stay denied.
_MANAGEMENT_ROUTES = frozenset(
{
("GET", "models"),
("POST", "models/load"),
("POST", "models/unload"),
}
)
_SERVED_ROUTES = _PASSTHROUGH_ROUTES | _MANAGEMENT_ROUTES
# The /v1 entries Studio does not implement. A test asserts each is really absent from
# the assembled app, so implementing one fails CI instead of being shadowed by a 404.
_UNSERVED_V1_PROBE_PATHS = frozenset(
{
"v1/chat/completions/control",
"v1/chat/completions/input_tokens",
"v1/health",
"v1/rerank",
"v1/reranking",
"v1/responses/input_tokens",
"v1/stream",
"v1/streams/lookup",
}
)
def is_engine_probe_path(full_path: str) -> bool:
"""True for an engine endpoint that must 404 rather than render the app shell."""
normalized = full_path.strip("/").lower()
return normalized in _ENGINE_PROBE_PATHS or normalized.startswith(_ENGINE_PROBE_PREFIXES)
def _inference():
"""``routes.inference``, deferred because it pulls the whole inference stack. One
indirection, so a test replaces this instead of mutating sys.modules."""
from routes import inference
return inference
@functools.lru_cache(maxsize = 1)
def _studio_version() -> str:
"""The version string, resolved once: get_studio_version() shells out to git twice
on a source checkout, and uncached that lands on the event loop per probe."""
try:
# Inside the guard: an ImportError here would 500 a probe about something else.
from utils.studio_version import get_studio_version
return get_studio_version()
except Exception: # noqa: BLE001 -- discovery must not 500 on a version lookup
return "dev"
def _loaded_public_model_id() -> Optional[str]:
"""The id ``/v1/models`` publishes for whatever is resident, or None."""
inf = _inference()
llama_backend = inf.get_llama_cpp_backend()
if getattr(llama_backend, "is_loaded", False):
return inf._llama_public_model_id(llama_backend)
# peek, not get: the orchestrator's cold build waits on hardware detection.
orchestrator = inf._peek_inference_backend()
if orchestrator is not None and getattr(orchestrator, "active_model_name", None):
return inf._orchestrator_public_model_id(orchestrator)
return None
def _server_props() -> dict:
"""llama-server's /props, with model_path mapped to the public id like every other
Studio response: upstream reports the absolute .gguf path and this is LAN reachable."""
llama_backend = _inference().get_llama_cpp_backend()
public_id = _loaded_public_model_id()
llama_loaded = bool(getattr(llama_backend, "is_loaded", False))
props: dict[str, Any] = {}
if llama_loaded:
# Documented not to raise, but a probe is the wrong place to find out it does.
try:
upstream = llama_backend._query_server_props()
except Exception: # noqa: BLE001
upstream = None
if isinstance(upstream, dict):
props = dict(upstream)
props.pop("model_path", None)
if public_id:
props["model_path"] = public_id
# These describe the child's routes/UI, not ours (Studio passes --metrics).
for _child_only in ("ui", "ui_settings", "cors_proxy_enabled"):
props.pop(_child_only, None)
props["endpoint_props"] = False
passthrough = llama_loaded and not _caller_is_managed_account()
props["endpoint_slots"] = passthrough and props.get("endpoint_slots") is True
props["endpoint_metrics"] = passthrough and props.get("endpoint_metrics") is True
# With MLX loaded, the unloaded llama backend would report no context or slots.
if llama_loaded:
props.setdefault("default_generation_settings", {})
settings = props["default_generation_settings"]
if isinstance(settings, dict) and "n_ctx" not in settings:
n_ctx = getattr(llama_backend, "context_length", None)
if n_ctx:
settings["n_ctx"] = int(n_ctx)
if "total_slots" not in props:
try:
slots = int(getattr(llama_backend, "effective_parallel_slots", 0) or 0)
except Exception: # noqa: BLE001
slots = 0
if slots > 0:
props["total_slots"] = slots
props.setdefault("chat_template", getattr(llama_backend, "chat_template", "") or "")
props["build_info"] = f"unsloth-studio/{_studio_version()}"
return props
# Slash forms too: the catch-all would serve index.html for "/props/".
@router.get("/props", include_in_schema = False)
@router.get("/props/", include_in_schema = False)
@router.get("/v1/props", include_in_schema = False)
@router.get("/v1/props/", include_in_schema = False)
async def llama_props(current_subject: str = Depends(get_current_subject)):
"""llama-server-compatible ``GET /props``. The body reveals the resident model, so another account gets the same answer ``/api/inference/status`` gives it."""
if await asyncio.to_thread(_resident_hidden_from_caller):
from hub.services.models.account_access import hidden_resident_response
return hidden_resident_response()
return await asyncio.to_thread(_server_props)
def _resident_hidden_from_caller() -> bool:
from hub.services.models import account_access
if not account_access.managed_account():
return False
if account_access.resident_hidden("chat"):
return True
try:
slot = _inference()._loaded_slot_ident()
except Exception: # noqa: BLE001 - a backend that cannot say has nothing to hide
return False
return bool(slot) and account_access.resident_hidden("chat", slot)
def _caller_is_managed_account() -> bool:
from hub.services.models import account_access
return account_access.managed_account()
def _upstream_client() -> httpx.AsyncClient:
# verify=False: plain HTTP to loopback, skips loading the CA bundle per call (~35 ms).
return httpx.AsyncClient(trust_env = False, verify = False)
def _engine_error(status_code: int, message: str) -> JSONResponse:
# llama-server: not_found_error is 404 only; a rejected body is invalid_request_error.
if status_code == 400:
error_type = "invalid_request_error"
else:
error_type = "unavailable_error" if status_code == 503 else "not_found_error"
return JSONResponse(
status_code = status_code,
content = {"error": {"code": status_code, "message": message, "type": error_type}},
)
async def _forward_to_llama_server(request: Request, path: str) -> Response:
# Owner only: /slots and /metrics are server-wide, not scoped per account.
if await asyncio.to_thread(_caller_is_managed_account):
return _engine_error(404, "API endpoint not found")
llama_backend = _inference().get_llama_cpp_backend()
# Unload flips is_loaded before clearing _port; "127.0.0.1:None" raises InvalidURL, not HTTPError.
port = getattr(llama_backend, "_port", None)
if not getattr(llama_backend, "is_loaded", False) or not port:
mlx_backend = _inference()._active_mlx_backend() if path == "tokenize" else None
if mlx_backend is not None:
return await _mlx_tokenize(request, mlx_backend)
return _engine_error(503, "No llama.cpp model is loaded")
url = f"http://127.0.0.1:{port}/{path}"
headers = dict(llama_backend._auth_headers or {})
body = await request.body() if request.method == "POST" else None
if body is not None:
headers["Content-Type"] = request.headers.get("content-type", "application/json")
try:
async with _upstream_client() as client:
upstream = await client.request(
request.method,
url,
params = request.query_params,
content = body,
headers = headers,
timeout = _PASSTHROUGH_TIMEOUT_S,
)
except httpx.HTTPError as exc:
logger.info("llama-server passthrough %s /%s failed: %s", request.method, path, exc)
return _engine_error(503, "llama.cpp server is not reachable")
return Response(
content = upstream.content,
status_code = upstream.status_code,
media_type = upstream.headers.get("content-type"),
)
async def _mlx_tokenize(request: Request, backend) -> Response:
"""llama-server's ``POST /tokenize`` for a resident MLX model, which lm-eval's gguf backend
calls before scoring with ``/v1/completions`` logprobs: specials parsed, BOS only with
``add_special``."""
try:
body = await request.json()
except ValueError:
body = None
content = body.get("content") if isinstance(body, dict) else None
# llama-server's tokenize_mixed accepts mixed arrays of strings and ids.
if not isinstance(content, (str, list)):
return _engine_error(400, "'content' must be a string or an array")
if body.get("with_pieces"):
return _engine_error(400, "'with_pieces' is not supported for MLX models")
# Specials are always parsed, so refuse rather than ignore: GGUF would tokenize differently.
if body.get("parse_special") is False:
return _engine_error(400, "'parse_special': false is not supported for MLX models")
add_special = body.get("add_special") is True
pieces = [content] if isinstance(content, str) else list(content)
# Resolution polls this event; without it an abandoned request pins a worker thread.
cancel = threading.Event()
def tokenize():
# Only a leading string gets specials, so a leading id (dropped after) tokenizes without.
prompt = pieces if add_special else [0, *pieces]
ids = backend.resolve_completion_prompt(prompt, cancel_event = cancel)["ids"]
return ids if add_special else ids[1:]
watcher = asyncio.create_task(_inference()._await_disconnect_then_cancel(request, cancel))
try:
tokens = await asyncio.to_thread(tokenize)
except ValueError as exc:
if any(piece != "" for piece in pieces):
return _engine_error(400, str(exc))
tokens = [] # llama-server answers empty content with an empty list
except RuntimeError:
# Unloaded since selection: an unavailable engine, not a server fault.
return _engine_error(503, "No llama.cpp model is loaded")
except InterruptedError:
return _engine_error(499, "Request cancelled.")
finally:
await _inference()._stop_local_disconnect_cancel_watcher(watcher)
return JSONResponse({"tokens": tokens})
def _make_passthrough(path: str):
async def passthrough(request: Request, current_subject: str = Depends(get_current_subject)):
return await _forward_to_llama_server(request, path)
passthrough.__name__ = f"llama_passthrough_{path}"
return passthrough
for _method, _path in sorted(_PASSTHROUGH_ROUTES):
for _form in (f"/{_path}", f"/{_path}/"):
router.add_api_route(
_form,
_make_passthrough(_path),
methods = [_method],
include_in_schema = False,
)
@router.get("/version", include_in_schema = False)
@router.get("/version/", include_in_schema = False)
async def studio_version(current_subject: str = Depends(get_current_subject)):
"""Threaded like /props because the first call resolves the version."""
return {"version": await asyncio.to_thread(_studio_version)}
class _ModelManageBody(BaseModel):
model: str = Field(..., min_length = 1)
def _catalog_match(catalog: list[dict], model_id: str) -> Optional[tuple[str, dict, bool]]:
"""``(id, row, pinned)``: the catalog's own id, or ``model_id`` as ``<listed id>:<quant>`` (pinned)."""
wanted = model_id.strip()
rows = {e["id"].lower(): e for e in catalog if isinstance(e.get("id"), str)}
if wanted.lower() in rows:
row = rows[wanted.lower()]
return row["id"], row, False
from core.inference.openai_auto_download import looks_like_quant
from core.inference.local_model_resolver import local_gguf_pinned_variant
base, sep, suffix = wanted.rpartition(":")
# only a quant or an indexed build key: any other suffix (":latest") means the bare repo
named = sep and (
looks_like_quant(suffix) or local_gguf_pinned_variant(wanted, allow_scan = False) is not None
)
row = rows.get(base.strip().lower()) if named else None
return (wanted, row, True) if row is not None else None
async def _chat_catalog() -> list[dict]:
# A task row (image, video, speech) loads through its own engine, not /load and /unload.
rows = await _inference()._openai_catalog_objects(quant_pins = True)
return [e for e in rows if e.get("task") is None]
async def _catalog_row(model_id: str) -> tuple[str, dict, bool]:
# The caller's own catalog, so a managed account only reaches models listed to it.
catalog = await _chat_catalog()
matched = _catalog_match(catalog, model_id)
if matched is None and ":" in model_id:
from core.inference.local_model_resolver import local_gguf_pinned_variant
# a cold index cannot vouch for a build key yet: scan once, off the loop, then retry
if await asyncio.to_thread(local_gguf_pinned_variant, model_id) is not None:
matched = _catalog_match(catalog, model_id)
if matched is None:
raise HTTPException(status_code = 404, detail = "Model not found")
return matched
async def _resolve_local(public_id: str) -> Optional[tuple]:
from core.inference.local_model_resolver import (
resolve_local_gguf,
resolve_trusted_cached_local_gguf,
)
return resolve_trusted_cached_local_gguf(public_id) or await asyncio.to_thread(
resolve_local_gguf, public_id
)
def _same_path(a: Optional[str], b: Optional[str]) -> bool:
import os
return bool(a and b) and os.path.normpath(a) == os.path.normpath(b)
async def _unload_target(model_id: str, row: dict, pinned: bool) -> Optional[str]:
"""The name /unload must use: the resident or loading id, not an alias; never an unrelated row."""
from core.inference.openai_auto_download import looks_like_quant, split_model_ref
# Another account's resident is not this caller's to name: answering for it would reveal it.
if not row.get("loaded") and await asyncio.to_thread(_resident_hidden_from_caller):
return None
inf = _inference()
resolved = await _resolve_local(model_id)
path = resolved[0] if resolved else None
base, suffix = split_model_ref(model_id)
# Only a quant the caller named: a bare id resolves to the preferred quant, not the resident one.
# The resolver's spelling of it, which maps a legacy label (BF16) onto the current quant.
from core.inference.local_model_resolver import (
local_gguf_pinned_variant,
local_gguf_same_build,
)
# not gated on suffix: split_model_ref cannot parse a directory-qualified build key
wanted_variant = local_gguf_pinned_variant(model_id, allow_scan = False)
if wanted_variant is None and suffix and looks_like_quant(suffix):
wanted_variant = (resolved[1] if resolved else None) or suffix
llama = inf.get_llama_cpp_backend()
orchestrator = inf._peek_inference_backend()
if getattr(llama, "is_loaded", False) or getattr(llama, "is_active", False):
candidates = [(llama, llama.model_identifier, getattr(llama, "hf_variant", None))]
else:
# Loading and active apart: a switch keeps the old model active while the new one loads.
loading = getattr(orchestrator, "get_loading_model", lambda: None)()
active = getattr(orchestrator, "active_model_name", None)
candidates = [(orchestrator, loading, None), (orchestrator, active, None)]
for backend, running, running_variant in candidates:
if not running:
continue
advertised = getattr(backend, "_openai_advertised_id", None)
# The full id first: an Ollama tag such as "llama3:latest" is the advertised id, not a quant pin.
named = _same_path(running, path) or any(
advertised and name.strip().lower() == advertised.strip().lower()
for name in (model_id, base)
)
if not named:
continue
# A pin names one quant: another one, or a backend with none, may sit in a kept slot instead.
if wanted_variant and not local_gguf_same_build(
model_id.rpartition(":")[0], running_variant or "", wanted_variant
):
continue
return running
filling = inf.model_slots.visible_loading()
if filling and _same_path(filling[1], path):
return path
# A kept slot routes by the listed id inside /unload. A pin through its base row has no
# residency of its own: the base row's flag describes whatever quant is resident.
return model_id if row.get("loaded") and not pinned else None
async def _resolve_load_target(model_id: str):
"""``(LoadRequest, listed id)``, from the on-disk copy and saved launch settings auto-switch would use."""
from core.inference.local_model_resolver import local_target_is_gguf
from core.inference.openai_auto_download import split_model_ref
from models.inference import LoadRequest
from utils.openai_auto_switch_settings import (
model_override_load_kwargs,
resolve_override_for_load,
stored_gpu_index_kind,
)
public_id, _, pinned = await _catalog_row(model_id)
resolved = await _resolve_local(public_id)
if resolved is None:
if pinned:
# A quant this machine does not have: no remote download from a catalog-gated route.
raise HTTPException(status_code = 404, detail = "Model not found")
# The row's own id, colon and all (an NPU "lemonade:..." id is not a quant pin).
return LoadRequest(model_path = public_id), public_id
model_path, variant, loader_id = resolved[:3]
alias = loader_id or split_model_ref(public_id)[0]
_, override = resolve_override_for_load(model_path, alias, variant)
# Not from the variant: a single-file GGUF or an Ollama tag resolves with no quant to pin.
is_gguf = await asyncio.to_thread(local_target_is_gguf, model_path, alias)
kwargs = model_override_load_kwargs(override, is_gguf = is_gguf)
# A stale pin (GPU removed or renumbered) is dropped, as auto-switch does, rather than 400 the load.
if kwargs.get("gpu_ids") and not await _inference()._override_gpu_ids_still_resolve(
kwargs["gpu_ids"], stored_gpu_index_kind(override)
):
kwargs.pop("gpu_ids", None)
kwargs.pop("tensor_split", None)
request = LoadRequest(model_path = model_path, gguf_variant = variant, **kwargs)
request._override_alias_id = alias
request._advertised_id = alias
return request, public_id
async def _lifecycle_answer(work, model_id: str, *, label: str):
"""Studio's /load and /unload wrapping (GPU-busy 409, tunnel padding), answered as llama-server does."""
from hub.services.models.account_access import gpu_busy_route
async def _answer():
await work
# The listed id, never the resident's path; a padded reply serializes this after the stream starts.
return {"success": True, "model": model_id}
@gpu_busy_route
async def _padded():
return await _inference()._tunnel_safe_json(_answer(), label = label)
return await _padded()
# Outside /v1: Open WebUI strips it for management calls (MODEL_MANAGEMENT_ENDPOINTS).
@router.get("/models", include_in_schema = False)
@router.get("/models/", include_in_schema = False)
async def manage_list_models(current_subject: str = Depends(get_current_subject)):
return {"object": "list", "data": await _chat_catalog()}
@router.post("/models/load", include_in_schema = False)
@router.post("/models/load/", include_in_schema = False)
async def manage_load_model(
body: _ModelManageBody,
fastapi_request: Request,
current_subject: str = Depends(get_current_subject),
):
request, public_id = await _resolve_load_target(body.model)
work = _inference().load_model_gated(
request, fastapi_request, current_subject, user_initiated = True
)
return await _lifecycle_answer(work, public_id, label = "Model load")
@router.post("/models/unload", include_in_schema = False)
@router.post("/models/unload/", include_in_schema = False)
async def manage_unload_model(
body: _ModelManageBody, current_subject: str = Depends(get_current_subject)
):
from models.inference import UnloadRequest
model_id, row, pinned = await _catalog_row(body.model)
if pinned and await _resolve_local(model_id) is None:
# A quant this machine does not have names nothing running; never the bare model.
raise HTTPException(status_code = 404, detail = "Model not found")
target = await _unload_target(model_id, row, pinned)
if target is None:
return {"success": True, "model": model_id}
request = UnloadRequest(model_path = target)
work = _inference()._unload_model_impl(request, current_subject)
return await _lifecycle_answer(work, model_id, label = "Model unload")
async def _probe_not_found():
raise HTTPException(status_code = 404, detail = "API endpoint not found")
# Else POST hits the GET-only catch-all and 405s. GET stays out so assets win; HEAD is not admitted
_PROBE_DENIED_METHODS = ["HEAD", "POST", "PUT", "PATCH", "DELETE"]
# Both forms of every path: no redirect rescues "POST /completion/"
def _both_forms(path: str) -> tuple:
return (path, path + "/")
def _denied_methods(path: str) -> list:
return [m for m in _PROBE_DENIED_METHODS if (m, path) not in _SERVED_ROUTES]
for _probe_path in sorted(_ENGINE_PROBE_PATHS):
for _form in _both_forms(f"/{_probe_path}"):
router.add_api_route(
_form,
_probe_not_found,
methods = _denied_methods(_probe_path),
include_in_schema = False,
)
# main.py already 404s an unknown GET under /v1/, so only the other methods need these.
for _v1_path in sorted(_UNSERVED_V1_PROBE_PATHS):
for _form in _both_forms(f"/{_v1_path}"):
router.add_api_route(
_form,
_probe_not_found,
methods = _PROBE_DENIED_METHODS,
include_in_schema = False,
)
for _slots_form in _both_forms("/slots/{id_slot}"):
router.add_api_route(
_slots_form,
_probe_not_found,
methods = _PROBE_DENIED_METHODS,
include_in_schema = False,
)
def add_get_denials(app) -> None:
"""404 the engine paths on GET as well, for an app with no frontend mounted. The GET denial normally comes
from main.py's SPA catch-all, registered only when setup_frontend() finds a build. In API-only mode there is
none, so these paths matched on method alone and answered 405, which a client reads as "endpoint exists".
"""
for path in sorted(_ENGINE_PROBE_PATHS) + sorted(_UNSERVED_V1_PROBE_PATHS):
if ("GET", path) in _SERVED_ROUTES:
continue
for form in _both_forms(f"/{path}"):
app.add_api_route(form, _probe_not_found, methods = ["GET"], include_in_schema = False)
for form in _both_forms("/slots/{id_slot}"):
app.add_api_route(form, _probe_not_found, methods = ["GET"], include_in_schema = False)