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
Studio: stop normal chat from opening the API monitor (#12305)
Lee Jacksonde4930c7b95e
diff --git a/studio/backend/core/inference/chat_generation_runs.py b/studio/backend/core/inference/chat_generation_runs.pyindex d2bde61dead..2dcd6253c4f 100644--- a/studio/backend/core/inference/chat_generation_runs.py+++ b/studio/backend/core/inference/chat_generation_runs.py@@ -70,6 +70,8 @@ def _background_request(run_id: str,cancel_event: threading.Event,timezone_headers: dict[str, str] | None = None,+ *,+ via_api_key: bool | None = None,) -> Request:scope = {"type": "http",@@ -92,7 +94,10 @@ def _background_request("client": ("127.0.0.1", 0),"server": ("127.0.0.1", 0),"app": app,- "state": {"generation_cancel_event": cancel_event},+ "state": {+ "generation_cancel_event": cancel_event,+ "api_monitor_via_api_key": via_api_key,+ },}async def receive():@@ -751,13 +756,16 @@ class ChatGenerationSupervisor:request_payload = dict(run["requestPayload"])timezone_headers = request_payload.pop(db.TIMEZONE_HEADERS_FIELD, None)+ via_api_key = request_payload.pop(db.API_MONITOR_ORIGIN_FIELD, None)payload = ChatCompletionRequest.model_validate(request_payload)# Switching, idle reload and auto-download all happen in the call below, and llama.cpp's first-token# budget only starts after it. One touch afterwards cannot cover a preparation longer than the lease# itself.response = await produce_openai_chat_completions(payload,- _background_request(self.app, run_id, cancel_event, timezone_headers),+ _background_request(+ self.app, run_id, cancel_event, timezone_headers, via_api_key = via_api_key+ ),owner,cancel_on_disconnect = False,)diff --git a/studio/backend/routes/chat_generation_runs.py b/studio/backend/routes/chat_generation_runs.pyindex 775f74a50b4..99e1e7f5a45 100644--- a/studio/backend/routes/chat_generation_runs.py+++ b/studio/backend/routes/chat_generation_runs.py@@ -311,6 +311,9 @@ async def create_chat_generation_run(current_subject: str = Depends(get_current_subject),):sanitized = _sanitize_request(payload)+ from routes.inference import _request_used_api_key++ sanitized[db.API_MONITOR_ORIGIN_FIELD] = _request_used_api_key(request)if timezone_headers := _timezone_headers(request):sanitized[db.TIMEZONE_HEADERS_FIELD] = timezone_headers# Serialize the off-loop commit with model lifecycle work, so a run is registered either before the gate opens ordiff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 060a80855d2..8e31eaf4c8e 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -3801,6 +3801,12 @@ def _request_used_api_key(request: Any) -> bool:# workflow traffic to an external caller. Saved-secret authorization uses# _request_has_api_key instead, narrowed by _request_is_internal_workflow where a# Unsloth workflow needs its own connection.++ # A durable run's synthetic request carries no caller credentials; use the origin+ # recorded when the run was created.+ recorded = getattr(getattr(request, "state", None), "api_monitor_via_api_key", None)+ if isinstance(recorded, bool):+ return recordedtoken = _request_api_key_token(request)if token is None:# keyless traffic is someone using Unsloth as an API server toodiff --git a/studio/backend/storage/chat_generation_runs_db.py b/studio/backend/storage/chat_generation_runs_db.pyindex caf940e56af..0244c10561c 100644--- a/studio/backend/storage/chat_generation_runs_db.py+++ b/studio/backend/storage/chat_generation_runs_db.py@@ -352,6 +352,8 @@ def _loads(value: str | None, fallback: Any) -> Any:# Kept with the run for the producer but outside its identity, so a retried create from a browser whose offset moved# (DST) still matches the committed run.TIMEZONE_HEADERS_FIELD = "timezone_headers"+# Server-derived API monitor and usage-receipt attribution; not a credential or part of retry identity.+API_MONITOR_ORIGIN_FIELD = "api_monitor_via_api_key"def canonical_request(@@ -375,7 +377,7 @@ def canonical_request("requestPayload": {key: valuefor key, value in request_payload.items()- if key != TIMEZONE_HEADERS_FIELD+ if key not in (TIMEZONE_HEADERS_FIELD, API_MONITOR_ORIGIN_FIELD)},},sort_keys = True,diff --git a/studio/backend/tests/test_chat_generation_supervisor.py b/studio/backend/tests/test_chat_generation_supervisor.pyindex afd2afb01ef..d13517504ed 100644--- a/studio/backend/tests/test_chat_generation_supervisor.py+++ b/studio/backend/tests/test_chat_generation_supervisor.py@@ -79,6 +79,97 @@ def _route_request(supervisor):)[email protected]("scope", ["off", "full"])+ "caller,expected",+ [("session", False), ("keyless", True), ("api-key", True), ("workflow", False)],+)+async def test_durable_producer_preserves_monitor_origin(monkeypatch, scope, caller, expected):+ from auth import policy, storage+ from utils import keyless_api_access as keyless++ studio_db.upsert_chat_thread(+ {"id": "thread-1", "title": "Chat", "modelType": "base", "modelId": "local", "createdAt": 1}+ )+ studio_db.upsert_chat_message(+ {"id": "user-1", "threadId": "thread-1", "role": "user", "content": [], "createdAt": 2}+ )+ app = SimpleNamespace(state = SimpleNamespace(bind_host = "127.0.0.1"))+ headers = (+ []+ if caller == "keyless"+ else [+ (+ b"authorization",+ b"Bearer session-jwt" if caller == "session" else b"Bearer sk-unsloth-test",+ )+ ]+ )+ request = Request(+ {+ "type": "http",+ "method": "POST",+ "path": "/api/inference/chat-runs",+ "headers": headers,+ "app": app,+ "client": ("127.0.0.1", 5000),+ "server": ("127.0.0.1", 8000),+ }+ )+ # Origin is fixed at run creation; the settings change below must not alter it.+ keyless.mark_keyless_admission(request, caller == "keyless")+ monkeypatch.setattr(policy, "installation_has_managed_accounts", lambda: False)+ monkeypatch.setattr(storage, "is_internal_api_key", lambda _token: caller == "workflow")+ run = await run_routes.create_chat_generation_run(_create_payload(), request, "alice")+ assert run["requestPayload"][runs_db.API_MONITOR_ORIGIN_FIELD] is expected+ request.state.api_monitor_via_api_key = not expected+ retry = await run_routes.create_chat_generation_run(_create_payload(), request, "alice")+ assert retry["created"] is False+ assert retry["requestPayload"][runs_db.API_MONITOR_ORIGIN_FIELD] is expected+ monkeypatch.setattr(keyless, "get_keyless_api_access_scope", lambda: scope)+ observed = []++ async def body():+ yield "data: [DONE]\n\n"++ async def fake(payload, background, _subject, *, cancel_on_disconnect):+ observed.append(+ (+ inference._request_used_api_key(background),+ runs_db.API_MONITOR_ORIGIN_FIELD in payload.model_extra,+ "authorization" in background.headers,+ )+ )+ return SimpleNamespace(status_code = 200, body_iterator = body())++ monkeypatch.setattr(inference, "produce_openai_chat_completions", fake)+ await ChatGenerationSupervisor(app)._produce("run-1")+ assert observed == [(expected, False, False)]+++def test_monitor_origin_is_not_part_of_retry_identity():+ args = dict(thread_id = "t", user_message_id = "u", assistant_message_id = "a")+ payload = {"model": "local"}+ _, original = runs_db.canonical_request(**args, request_payload = payload)+ for origin in (False, True):+ encoded, identity = runs_db.canonical_request(+ **args, request_payload = {**payload, runs_db.API_MONITOR_ORIGIN_FIELD: origin}+ )+ assert identity == original+ assert json.loads(encoded)[runs_db.API_MONITOR_ORIGIN_FIELD] is origin+++def test_client_cannot_supply_monitor_origin():+ from fastapi import HTTPException++ payload = _create_payload()+ payload.requestPayload[runs_db.API_MONITOR_ORIGIN_FIELD] = False+ with pytest.raises(HTTPException) as exc:+ run_routes._sanitize_request(payload)+ assert exc.value.status_code == 400++@pytest.mark.asyncioasync def test_public_chat_wrapper_keeps_cancel_on_disconnect(monkeypatch):observed = []