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: OpenAI-compatible fine-tuning jobs API (/v1/files, /v1/fine_tuning/jobs) (#14496)
Daniel Han6bbe268aeb9e
diff --git a/studio/backend/main.py b/studio/backend/main.pyindex 08fc03a3db6..99d5db827b9 100644--- a/studio/backend/main.py+++ b/studio/backend/main.py@@ -356,6 +356,7 @@ from routes import (inference_router,inference_studio_router,mcp_servers_router,+ openai_fine_tuning_router,skills_router,models_router,providers_router,@@ -1502,6 +1503,8 @@ _DATASET_UPLOAD_PASSTHROUGH_PREFIXES = ("/api/datasets/upload","/api/hub/datasets/upload",)+# OpenAI-compatible fine-tuning upload: the same dataset upload and cap, matched by EXACT path.+_OPENAI_FILES_UPLOAD_PATH = "/v1/files"_DATA_RECIPE_UNSTRUCTURED_UPLOAD_PASSTHROUGH_PREFIX = ("/api/data-recipe/seed/upload-unstructured-file")@@ -1542,6 +1545,7 @@ _BODY_UPLOAD_PASSTHROUGH_EXACT_PATHS = (*_STT_MULTIPART_UPLOAD_PATHS,*_VIDEO_MULTIPART_UPLOAD_PATHS,_LIBRARY_UPLOAD_PATH,+ _OPENAI_FILES_UPLOAD_PATH,)# Which of those may arrive with no Content-Length and be counted instead of refused. Deliberately NOT the# whole set above: this middleware runs before authentication, and a counted body is a held body, so the dataset@@ -1575,9 +1579,9 @@ def _get_upload_passthrough_request_max_bytes(path: str) -> int:return upload_request_limit_bytes(_rag_config.MAX_UPLOAD_BYTES)# The trailing-slash variant reaches this middleware BEFORE the router's redirect_slashes# 307, so it must resolve to the same cap. JSON sub-routes keep extra path components.- if (- path.startswith(_DATASET_UPLOAD_PASSTHROUGH_PREFIXES)- or path.rstrip("/") == _DIFFUSION_DATASET_UPLOAD_PATH+ if path.startswith(_DATASET_UPLOAD_PASSTHROUGH_PREFIXES) or path.rstrip("/") in (+ _DIFFUSION_DATASET_UPLOAD_PATH,+ _OPENAI_FILES_UPLOAD_PATH,):return upload_request_limit_bytes()return default_request_body_limit_bytes()@@ -1904,6 +1908,7 @@ app.include_router(video_openai_router, prefix = "/api/inference", tags = ["infeapp.include_router(video_openai_router, prefix = "/v1", tags = ["openai-compat"])app.include_router(inference_router, prefix = "/v1", tags = ["openai-compat"])+app.include_router(openai_fine_tuning_router, prefix = "/v1", tags = ["openai-compat"])app.include_router(systemone_router, prefix = "/v1", tags = ["systemone"])# llama-server / Ollama discovery probes. Declares its own full paths (/props, /version, /api/tags, ...) so it# needs no prefix, and must be registered ahead of the SPA catch-all in serve_frontend() or /props and /versiondiff --git a/studio/backend/routes/__init__.py b/studio/backend/routes/__init__.pyindex 372a571fec8..48170879160 100644--- a/studio/backend/routes/__init__.py+++ b/studio/backend/routes/__init__.py@@ -25,6 +25,7 @@ from routes.rag import router as rag_routerfrom routes.research_runs import router as research_runs_routerfrom routes.chat_generation_runs import router as chat_generation_runs_routerfrom routes.youtube import router as youtube_router+from routes.openai_fine_tuning import router as openai_fine_tuning_router__all__ = ["training_router",@@ -48,6 +49,7 @@ __all__ = ["research_runs_router","chat_generation_runs_router","youtube_router",+ "openai_fine_tuning_router",]# Bind the re-export so the import-hoist verifier counts it as used.@@ -58,4 +60,5 @@ _ = (research_runs_router,chat_generation_runs_router,youtube_router,+ openai_fine_tuning_router,)diff --git a/studio/backend/routes/openai_fine_tuning.py b/studio/backend/routes/openai_fine_tuning.pynew file mode 100644index 00000000000..78a104b6799--- /dev/null+++ b/studio/backend/routes/openai_fine_tuning.py@@ -0,0 +1,676 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""OpenAI-compatible fine-tuning API (``/v1/files`` and ``/v1/fine_tuning/jobs``) over Studio's own training.++A thin translation layer: uploads land where ``/api/hub/datasets/upload`` puts them, jobs start through the+same ``start_training`` handler as ``/api/train/start`` (every guard and refusal included), and job state is+read back from the training history, so a job is just a Studio training run under another name. Nothing here+grants a capability an API key does not already have through ``/api/train``.+"""++from __future__ import annotations++import asyncio+import json+import re+from datetime import datetime, timezone+from pathlib import Path+from typing import Any, Literal, Optional++from fastapi import APIRouter, Depends, File, Form, HTTPException, Query, UploadFile+from pydantic import BaseModel, ConfigDict++from auth.authentication import authenticated_via_api_key, get_current_subject+from hub.services.datasets import local as local_datasets+from hub.utils.host_paths import host_paths_visible, redact_host_paths, redact_paths_in_text+from loggers import get_logger+from models.training import TrainingStartRequest+from storage.studio_db import get_connection, get_run, list_runs+from utils.training_runs import drop_non_finite++logger = get_logger(__name__)++router = APIRouter()++FILE_PREFIX = "file-"+JOB_PREFIX = "ft" # "job_20261021_101500_ab12cd34" -> "ftjob_20261021_101500_ab12cd34"+# OpenAI scales its own default; Studio's default learning rate is the base the multiplier scales.+BASE_LEARNING_RATE = 2e-4+_UPLOAD_ID = re.compile(r"^[0-9a-f]{32}$")+# Set by this layer from the OpenAI fields; extra_body may not override them.+_MANAGED_FIELDS = frozenset(+ {"model_name", "local_datasets", "local_eval_datasets", "start_request_id", "project_name"}+)+_RUN_STATUS = {+ "running": "running",+ "completed": "succeeded",+ "stopped": "cancelled",+ "error": "failed",+}+_PHASE_STATUS = {+ "idle": "validating_files",+ "loading_model": "validating_files",+ "loading_dataset": "validating_files",+ "configuring": "queued",+ "training": "running",+ "training_drafter": "running",+ "finalizing": "running",+ "completed": "succeeded",+ "stopped": "cancelled",+ "error": "failed",+}+++class FineTuningJobCreate(BaseModel):+ # Unknown keys are Studio training settings sent through the SDK's extra_body.+ model_config = ConfigDict(extra = "allow")++ model: str+ training_file: str+ validation_file: Optional[str] = None+ hyperparameters: Optional[dict[str, Any]] = None+ method: Optional[dict[str, Any]] = None+ suffix: Optional[str] = None+ seed: Optional[int] = None+ integrations: Optional[list[Any]] = None+ metadata: Optional[dict[str, Any]] = None+++def _bad_request(message: str, status_code: int = 400) -> HTTPException:+ return HTTPException(status_code = status_code, detail = message)+++def _unix(value: Any) -> Optional[int]:+ if not value:+ return None+ try:+ parsed = datetime.fromisoformat(str(value).replace("Z", "+00:00"))+ except ValueError:+ return None+ if parsed.tzinfo is None:+ parsed = parsed.replace(tzinfo = timezone.utc)+ return int(parsed.timestamp())+++# Files+++def _upload_id(name: str) -> Optional[str]:+ prefix, sep, _rest = name.partition("_")+ return prefix if sep and _UPLOAD_ID.match(prefix) else None+++def _file_object(path: Path) -> dict[str, Any]:+ stat = path.stat()+ return {+ "id": f"{FILE_PREFIX}{_upload_id(path.name)}",+ "object": "file",+ "bytes": stat.st_size,+ "created_at": int(stat.st_mtime),+ "filename": local_datasets._display_uploaded_dataset_name(path),+ "purpose": "fine-tune",+ "status": "processed",+ "status_details": None,+ }+++def _uploaded_files() -> list[Path]:+ """This account's dataset uploads, newest first, as the Studio dataset picker lists them."""+ items = local_datasets.list_local_datasets_response().datasets+ paths = [Path(item.path) for item in items if item.source == "upload"]+ return [path for path in paths if _upload_id(path.name)]+++def _find_file(file_id: str) -> Path:+ upload_id = file_id[len(FILE_PREFIX) :] if file_id.startswith(FILE_PREFIX) else ""+ if _UPLOAD_ID.match(upload_id):+ for path in _uploaded_files():+ if _upload_id(path.name) == upload_id:+ return path+ raise _bad_request(f"No such File object: {file_id}", 404)+++def _file_id_for(path: Any) -> Optional[str]:+ upload_id = _upload_id(Path(str(path)).name) if path else None+ return f"{FILE_PREFIX}{upload_id}" if upload_id else None++[email protected]("/files")+async def create_file(+ file: UploadFile = File(...),+ purpose: str = Form(...),+ current_subject: str = Depends(get_current_subject),+):+ if purpose != "fine-tune":+ raise _bad_request(+ f"Unsloth Studio stores files for fine-tuning only; got purpose '{purpose}'."+ )+ saved = await local_datasets.upload_dataset_response(file)+ return await asyncio.to_thread(_file_object, Path(saved.stored_path))++[email protected]("/files")+async def list_files(+ after: Optional[str] = None,+ limit: int = Query(10000, ge = 1, le = 10000),+ order: Literal["asc", "desc"] = "desc",+ current_subject: str = Depends(get_current_subject),+):+ files = await asyncio.to_thread(lambda: [_file_object(path) for path in _uploaded_files()])+ return _paged(files if order == "desc" else files[::-1], after, limit)++[email protected]("/files/{file_id}")+async def retrieve_file(file_id: str, current_subject: str = Depends(get_current_subject)):+ return await asyncio.to_thread(lambda: _file_object(_find_file(file_id)))+++# Jobs+++def _job_id(run_id: str) -> str:+ return f"{JOB_PREFIX}{run_id}"+++def _run_id(job_id: str) -> str:+ return job_id[len(JOB_PREFIX) :] if job_id.startswith(f"{JOB_PREFIX}job_") else job_id+++def _hyperparameters(config: dict[str, Any]) -> dict[str, Any]:+ try:+ multiplier = round(float(config.get("learning_rate")) / BASE_LEARNING_RATE, 6)+ except (TypeError, ValueError):+ multiplier = "auto"+ return {+ "n_epochs": config.get("num_epochs") or "auto",+ "batch_size": config.get("batch_size") or "auto",+ "learning_rate_multiplier": multiplier,+ }+++def _job_object(+ run_id: str,+ *,+ status: str,+ config: dict[str, Any],+ created_at: Optional[int],+ finished_at: Optional[int] = None,+ error: Optional[str] = None,+ fine_tuned_model: Optional[str] = None,+ trained_tokens: Optional[int] = None,+) -> dict[str, Any]:+ hyperparameters = _hyperparameters(config)+ datasets = config.get("local_datasets") or []+ eval_datasets = config.get("local_eval_datasets") or []+ return {+ "id": _job_id(run_id),+ "object": "fine_tuning.job",+ "created_at": created_at or int(datetime.now(timezone.utc).timestamp()),+ "error": {"code": "training_failed", "message": error, "param": None} if error else None,+ "fine_tuned_model": fine_tuned_model,+ "finished_at": finished_at,+ "hyperparameters": hyperparameters,+ "model": config.get("model_name"),+ "organization_id": "unsloth-studio",+ "result_files": [],+ "seed": config.get("random_seed"),+ "status": status,+ "trained_tokens": trained_tokens,+ "training_file": _file_id_for(datasets[0]) if datasets else config.get("hf_dataset"),+ "validation_file": _file_id_for(eval_datasets[0]) if eval_datasets else None,+ "method": {"type": "supervised", "supervised": {"hyperparameters": hyperparameters}},+ "suffix": config.get("project_name"),+ "metadata": None,+ }+++def _run_config(run: dict[str, Any]) -> dict[str, Any]:+ try:+ # An older install may have stored Infinity / NaN, which the JSON response cannot carry.+ config = drop_non_finite(json.loads(run.get("config_json") or "{}"))+ except (TypeError, ValueError):+ config = {}+ return config if isinstance(config, dict) else {}+++def _trained_tokens(run_id: str) -> Optional[int]:+ conn = get_connection()+ try:+ row = conn.execute(+ "SELECT MAX(num_tokens) FROM training_metrics WHERE run_id = ?", (run_id,)+ ).fetchone()+ finally:+ conn.close()+ return row[0] if row and row[0] is not None else None+++def _job_from_run(run: dict[str, Any]) -> dict[str, Any]:+ status = _RUN_STATUS.get(run.get("status"), "running")+ if status != "running":+ _CANCELLING.discard(run["id"])+ elif run["id"] in _CANCELLING:+ status = "cancelled"+ output_dir = run.get("output_dir")+ return _job_object(+ run["id"],+ status = status,+ config = _run_config(run),+ created_at = _unix(run.get("started_at")),+ finished_at = _unix(run.get("ended_at")) if status != "running" else None,+ error = (run.get("error_message") or "Training failed") if status == "failed" else None,+ # The run folder: the name Studio's chat, export and checkpoint lists show for this model.+ fine_tuned_model = Path(output_dir).name if status == "succeeded" and output_dir else None,+ trained_tokens = _trained_tokens(run["id"]),+ )+++# Jobs started here, until their history row exists: a retrieve right after create keeps the job's fields.+_STARTED: dict[str, tuple[dict[str, Any], int]] = {}+_STARTED_LIMIT = 16+# Cancelled here while the worker finishes its step: the run row reads "running" until it stops.+_CANCELLING: set[str] = set()+++def _remember_start(run_id: str, config: dict[str, Any], created_at: int) -> None:+ _STARTED[run_id] = (config, created_at)+ while len(_STARTED) > _STARTED_LIMIT:+ _STARTED.pop(next(iter(_STARTED)))+++def _for_caller(job: dict[str, Any], via_api_key: bool) -> dict[str, Any]:+ """An API key never sees host paths: a local model becomes its handle, an error loses its paths."""+ if host_paths_visible(via_api_key):+ return job+ model = redact_host_paths({"model_name": job["model"]}, via_api_key = via_api_key)+ job = {**job, "model": model["model_name"]}+ if job["error"]:+ job["error"] = {**job["error"], "message": redact_paths_in_text(job["error"]["message"])}+ return job+++def _status_field(status: Any, name: str) -> Any:+ # The status route hands back a model, or its redacted dump for an API key.+ return status.get(name) if isinstance(status, dict) else getattr(status, name, None)+++async def _current_status(current_subject: str, via_api_key: bool) -> Any:+ from routes import training as training_routes+ try:+ return await training_routes.get_training_status(+ current_subject = current_subject, via_api_key = via_api_key+ )+ except HTTPException:+ return None+++async def _job(run_id: str, current_subject: str, via_api_key: bool) -> dict[str, Any]:+ run = await asyncio.to_thread(get_run, run_id)+ if run is not None:+ return _for_caller(await asyncio.to_thread(_job_from_run, run), via_api_key)+ # A started job has no history row until its worker is up; the live status still knows it.+ status = await _current_status(current_subject, via_api_key)+ if status is not None and _status_field(status, "job_id") == run_id:+ phase = _status_field(status, "phase")+ failed = _status_field(status, "start_request_state") == "rejected" or phase == "error"+ message = _status_field(status, "error") or _status_field(status, "message")+ config, created_at = _STARTED.get(run_id, ({}, None))+ state = _PHASE_STATUS.get(phase, "queued")+ if failed:+ state = "failed"+ elif run_id in _CANCELLING:+ state = "cancelled"+ return _for_caller(+ _job_object(+ run_id,+ status = state,+ config = config,+ created_at = created_at,+ error = (message or "Training failed") if failed else None,+ ),+ via_api_key,+ )+ raise _bad_request(f"No such fine-tuning job: {_job_id(run_id)}", 404)+++def _object(value: Any, name: str) -> dict[str, Any]:+ if value is None:+ return {}+ if not isinstance(value, dict):+ raise _bad_request(f"{name} must be an object.")+ return value+++def _training_config(body: FineTuningJobCreate) -> dict[str, Any]:+ method = _object(body.method, "method")+ method_type = method.get("type", "supervised")+ if method_type != "supervised":+ raise _bad_request(+ f"Fine-tuning method '{method_type}' is not supported yet; use 'supervised'."+ )+ supervised = _object(method.get("supervised"), "method.supervised")+ hyperparameters = dict(_object(body.hyperparameters, "hyperparameters"))+ hyperparameters.update(+ _object(supervised.get("hyperparameters"), "method.supervised.hyperparameters")+ )++ config: dict[str, Any] = {"training_type": "LoRA/QLoRA", "format_type": "auto"}+ extra = body.model_extra or {}+ unknown = sorted(+ key+ for key in extra+ if key not in TrainingStartRequest.model_fields or key in _MANAGED_FIELDS+ )+ if unknown:+ raise _bad_request(f"Unknown parameter(s): {', '.join(unknown)}")+ config.update(extra)++ config["model_name"] = body.model+ config["local_datasets"] = [str(_find_file(body.training_file))]+ if body.validation_file:+ config["local_eval_datasets"] = [str(_find_file(body.validation_file))]+ # Studio evaluates only when eval_steps > 0; OpenAI always validates a given validation_file.+ config.setdefault("eval_steps", 0.1)+ if body.suffix:+ config["project_name"] = body.suffix+ for integration in body.integrations or []:+ integration = _object(integration, "integrations[]")+ if integration.get("type") != "wandb":+ raise _bad_request(+ f"Integration '{integration.get('type')}' is not supported; use 'wandb'."+ )+ wandb = _object(integration.get("wandb"), "integrations[].wandb")+ # The W&B key is the server's WANDB_API_KEY, or wandb_token through extra_body.+ config.setdefault("enable_wandb", True)+ if wandb.get("project"):+ config.setdefault("wandb_project", wandb["project"])+ if body.seed is not None:+ config["random_seed"] = body.seed+ try:+ n_epochs = hyperparameters.get("n_epochs", "auto")+ if n_epochs != "auto":+ config["num_epochs"] = int(n_epochs)+ batch_size = hyperparameters.get("batch_size", "auto")+ if batch_size != "auto":+ config["batch_size"] = int(batch_size)+ multiplier = hyperparameters.get("learning_rate_multiplier", "auto")+ if multiplier != "auto":+ config["learning_rate"] = f"{BASE_LEARNING_RATE * float(multiplier):g}"+ except (TypeError, ValueError):+ raise _bad_request(+ "n_epochs and batch_size take an integer or 'auto', learning_rate_multiplier a number or 'auto'."+ ) from None+ return config++[email protected]("/fine_tuning/jobs")+async def create_fine_tuning_job(+ body: FineTuningJobCreate,+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):+ from pydantic import ValidationError++ from routes import training as training_routes++ config = await asyncio.to_thread(_training_config, body)+ try:+ request = TrainingStartRequest.model_validate(config)+ except ValidationError as exc:+ first = exc.errors()[0]+ field = ".".join(str(part) for part in first.get("loc", ()))+ raise _bad_request(f"Invalid parameter {field}: {first.get('msg')}") from None+ started = await training_routes.start_training(+ request, current_subject = current_subject, via_api_key = via_api_key+ )+ if started.status == "error" or not started.job_id:+ message = started.message or started.error or "Training did not start"+ raise _bad_request(+ message if host_paths_visible(via_api_key) else redact_paths_in_text(message), 409+ )+ created_at = int(datetime.now(timezone.utc).timestamp())+ _remember_start(started.job_id, request.model_dump(), created_at)+ return _for_caller(+ _job_object(+ started.job_id,+ status = "validating_files",+ config = request.model_dump(),+ created_at = created_at,+ ),+ via_api_key,+ )+++def _paged(rows: list[dict[str, Any]], after: Optional[str], limit: int) -> dict[str, Any]:+ if after:+ ids = [row["id"] for row in rows]+ rows = rows[ids.index(after) + 1 :] if after in ids else []+ return {"object": "list", "data": rows[:limit], "has_more": len(rows) > limit}+++def _runs_after(after: Optional[str], count: int) -> list[dict[str, Any]]:+ """Up to ``count`` runs older than ``after`` (newest first), paging through the whole history."""+ runs: list[dict[str, Any]] = []+ found, offset = after is None, 0+ while len(runs) < count:+ batch = list_runs(200, offset)["runs"]+ if not batch:+ break+ for run in batch:+ if found:+ runs.append(run)+ found = found or run["id"] == after+ offset += len(batch)+ return runs[:count]++[email protected]("/fine_tuning/jobs")+async def list_fine_tuning_jobs(+ after: Optional[str] = None,+ limit: int = Query(20, ge = 1, le = 100),+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):+ after_run = _run_id(after) if after else None+ runs = await asyncio.to_thread(_runs_after, after_run, limit + 1)+ if after_run and not runs:+ # The live job has no history row yet and is listed first, so the history follows it.+ status = await _current_status(current_subject, via_api_key)+ if status is not None and _status_field(status, "job_id") == after_run:+ runs = await asyncio.to_thread(_runs_after, None, limit + 1)+ jobs = [_for_caller(await asyncio.to_thread(_job_from_run, run), via_api_key) for run in runs]+ if after is None:+ status = await _current_status(current_subject, via_api_key)+ live_id = _status_field(status, "job_id") if status is not None else None+ live_phase = _status_field(status, "phase") if status is not None else None+ if live_id and live_phase != "idle" and all(run["id"] != live_id for run in runs):+ jobs.insert(0, await _job(live_id, current_subject, via_api_key))+ return {"object": "list", "data": jobs[:limit], "has_more": len(jobs) > limit}++[email protected]("/fine_tuning/jobs/{job_id}")+async def retrieve_fine_tuning_job(+ job_id: str,+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):+ return await _job(_run_id(job_id), current_subject, via_api_key)+++def _events(run: dict[str, Any], via_api_key: bool) -> list[dict[str, Any]]:+ """Newest first: one metrics event per logged step, framed by the start and the outcome."""+ run_id = run["id"]+ started = _unix(run.get("started_at")) or 0+ conn = get_connection()+ try:+ rows = conn.execute(+ "SELECT step, loss, learning_rate, grad_norm, eval_loss, epoch, elapsed_seconds "+ "FROM training_metrics WHERE run_id = ? AND step > 0 ORDER BY step",+ (run_id,),+ ).fetchall()+ finally:+ conn.close()+ total = run.get("total_steps")++ def event(+ key: str,+ created_at: int,+ message: str,+ level: str = "info",+ data: Any = None,+ ):+ return {+ "id": f"ftevent_{run_id}_{key}",+ "object": "fine_tuning.job.event",+ "created_at": created_at,+ "level": level,+ "message": message,+ "data": data,+ "type": "metrics" if data else "message",+ }++ events = [event("start", started, "Fine-tuning job started")]+ # An fp16 overflow logs an inf grad norm, which the JSON response cannot carry.+ for step, loss, lr, grad_norm, eval_loss, epoch, elapsed in drop_non_finite(+ [tuple(row) for row in rows]+ ):+ data = {+ "step": step,+ "total_steps": total,+ "train_loss": loss,+ "learning_rate": lr,+ "grad_norm": grad_norm,+ "valid_loss": eval_loss,+ "epoch": epoch,+ }+ loss_text = f"training loss={loss:.4f}" if loss is not None else "no loss logged"+ events.append(+ event(+ f"step{step}",+ started + int(elapsed or 0),+ f"Step {step}/{total or '?'}: {loss_text}",+ data = data,+ )+ )+ status = _RUN_STATUS.get(run.get("status"), "running")+ ended = _unix(run.get("ended_at")) or started+ if status == "succeeded":+ events.append(event("end", ended, "The job has successfully completed"))+ elif status == "cancelled":+ events.append(event("end", ended, "The job was cancelled"))+ elif status == "failed":+ message = run.get("error_message") or "Training failed"+ if not host_paths_visible(via_api_key):+ message = redact_paths_in_text(message)+ events.append(event("end", ended, message, level = "error"))+ events.reverse()+ return events++[email protected]("/fine_tuning/jobs/{job_id}/events")+async def list_fine_tuning_events(+ job_id: str,+ after: Optional[str] = None,+ limit: int = Query(20, ge = 1, le = 1000),+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):+ run_id = _run_id(job_id)+ run = await asyncio.to_thread(get_run, run_id)+ if run is None:+ # Known but not started yet: no events, rather than a 404 a polling client would give up on.+ await _job(run_id, current_subject, via_api_key)+ return {"object": "list", "data": [], "has_more": False}+ return _paged(await asyncio.to_thread(_events, run, via_api_key), after, limit)+++def _checkpoints(run: dict[str, Any]) -> list[dict[str, Any]]:+ output_dir = run.get("output_dir")+ if not output_dir or not Path(output_dir).is_dir():+ return []+ run_id = run["id"]+ started = _unix(run.get("started_at")) or 0+ conn = get_connection()+ try:+ metrics = {+ step: (loss, eval_loss)+ for step, loss, eval_loss in drop_non_finite(+ [+ tuple(row)+ for row in conn.execute(+ "SELECT step, loss, eval_loss FROM training_metrics WHERE run_id = ?",+ (run_id,),+ ).fetchall()+ ]+ )+ }+ finally:+ conn.close()+ checkpoints = []+ for path in Path(output_dir).glob("checkpoint-*"):+ step_text = path.name.rpartition("-")[2]+ if not path.is_dir() or not step_text.isdigit():+ continue+ step = int(step_text)+ loss, eval_loss = metrics.get(step, (None, None))+ checkpoints.append(+ {+ "id": f"ftckpt_{run_id}_{step}",+ "object": "fine_tuning.job.checkpoint",+ "created_at": int(path.stat().st_mtime) or started,+ # The "<run folder>/<checkpoint>" name Studio loads and exports a checkpoint by.+ "fine_tuned_model_checkpoint": f"{Path(output_dir).name}/{path.name}",+ "fine_tuning_job_id": _job_id(run_id),+ "metrics": {"step": step, "train_loss": loss, "valid_loss": eval_loss},+ "step_number": step,+ }+ )+ checkpoints.sort(key = lambda checkpoint: checkpoint["step_number"], reverse = True)+ return checkpoints++[email protected]("/fine_tuning/jobs/{job_id}/checkpoints")+async def list_fine_tuning_checkpoints(+ job_id: str,+ after: Optional[str] = None,+ limit: int = Query(10, ge = 1, le = 100),+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):+ run_id = _run_id(job_id)+ run = await asyncio.to_thread(get_run, run_id)+ if run is None:+ await _job(run_id, current_subject, via_api_key)+ return {"object": "list", "data": [], "has_more": False}+ return _paged(await asyncio.to_thread(_checkpoints, run), after, limit)++[email protected]("/fine_tuning/jobs/{job_id}/cancel")+async def cancel_fine_tuning_job(+ job_id: str,+ current_subject: str = Depends(get_current_subject),+ via_api_key: bool = Depends(authenticated_via_api_key),+):+ from routes import training as training_routes++ run_id = _run_id(job_id)+ job = await _job(run_id, current_subject, via_api_key)+ if job["status"] in ("succeeded", "failed", "cancelled"):+ raise _bad_request(f"Job {job['id']} has already {job['status']}.")+ stopped = await training_routes.stop_training(+ training_routes.TrainingStopRequest(save = True, expected_job_id = run_id),+ current_subject = current_subject,+ )+ if stopped.status == "idle":+ raise _bad_request(f"Job {job['id']} is not running.", 409)+ # The worker stops at its next safe step; the job reads "cancelled" from then on.+ _CANCELLING.add(run_id)+ return {**job, "status": "cancelled"}++[email protected]("/fine_tuning/jobs/{job_id}/pause")[email protected]("/fine_tuning/jobs/{job_id}/resume")+async def pause_or_resume_fine_tuning_job(+ job_id: str, current_subject: str = Depends(get_current_subject)+):+ raise _bad_request("Pausing and resuming fine-tuning jobs is not supported; use cancel.")diff --git a/studio/backend/tests/multi_account/factories_training.py b/studio/backend/tests/multi_account/factories_training.pyindex 5afb504d9b1..633a8366f69 100644--- a/studio/backend/tests/multi_account/factories_training.py+++ b/studio/backend/tests/multi_account/factories_training.py@@ -263,8 +263,88 @@ def seed_unstructured_upload(account) -> dict[str, str]:return {"block_id": BLOCK_ID, "file_id": FILE_ID}+OPENAI_RUN_ID = "training-matrix-openai-run"+OPENAI_UPLOAD_ID = "0f1e2d3c4b5a69788796a5b4c3d2e1f0"+++@seeder("training-openai-job")+def seed_openai_job(account) -> dict[str, str]:+ """A finished run in this account, which the OpenAI fine-tuning routes read as a job."""+ import json++ from storage import studio_db+ from utils.account_context import run_as++ def install() -> None:+ studio_db.create_run(+ id = OPENAI_RUN_ID,+ model_name = "local/model",+ dataset_name = "local/dataset",+ config_json = json.dumps({"model_name": "local/model", "project_name": SENTINEL}),+ started_at = "2026-01-01T00:00:00+00:00",+ total_steps = 1,+ )+ studio_db.finish_run(+ id = OPENAI_RUN_ID,+ status = "completed",+ ended_at = "2026-01-01T00:01:00+00:00",+ final_step = 1,+ final_loss = 1.0,+ duration_seconds = 60.0,+ )++ run_as(account, install)+ return {"job_id": OPENAI_RUN_ID}+++@seeder("training-openai-file")+def seed_openai_file(account) -> dict[str, str]:+ from utils.account_context import run_as+ from utils.paths.storage_roots import dataset_uploads_root++ def install() -> None:+ root = dataset_uploads_root()+ root.mkdir(parents = True, exist_ok = True)+ (root / f"{OPENAI_UPLOAD_ID}_matrix.jsonl").write_text(+ '{"messages": []}\n', encoding = "utf-8"+ )++ run_as(account, install)+ return {"file_id": f"file-{OPENAI_UPLOAD_ID}"}++FACTORIES = {"routes.training_history:DELETE:/runs/{run_id}": Factory("training", fragment = "deleted"),+ "routes.openai_fine_tuning:GET:/files/{file_id}": Factory(+ "training-openai-file", fragment = "matrix.jsonl"+ ),+ "routes.openai_fine_tuning:GET:/fine_tuning/jobs/{job_id}": Factory(+ "training-openai-job", fragment = SENTINEL+ ),+ "routes.openai_fine_tuning:GET:/fine_tuning/jobs/{job_id}/events": Factory(+ "training-openai-job", fragment = "successfully completed"+ ),+ "routes.openai_fine_tuning:GET:/fine_tuning/jobs/{job_id}/checkpoints": Factory(+ "training-openai-job", fragment = "list"+ ),+ # A finished job cannot be cancelled: its own account gets 400, every other account 404.+ "routes.openai_fine_tuning:POST:/fine_tuning/jobs/{job_id}/cancel": Factory(+ "training-openai-job", success = 400+ ),+ "routes.openai_fine_tuning:POST:/fine_tuning/jobs/{job_id}/pause": Factory(+ "training-openai-job",+ success = 400,+ owner = (400,),+ wrong = (400,),+ reason = "unsupported for every job: refused before any lookup, so nothing is revealed",+ ),+ "routes.openai_fine_tuning:POST:/fine_tuning/jobs/{job_id}/resume": Factory(+ "training-openai-job",+ success = 400,+ owner = (400,),+ wrong = (400,),+ reason = "unsupported for every job: refused before any lookup, so nothing is revealed",+ ),"routes.training:GET:/start-requests/{start_request_id}": Factory("training-start-request", fragment = SENTINEL),diff --git a/studio/backend/tests/test_openai_fine_tuning.py b/studio/backend/tests/test_openai_fine_tuning.pynew file mode 100644index 00000000000..919a2483342--- /dev/null+++ b/studio/backend/tests/test_openai_fine_tuning.py@@ -0,0 +1,374 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The OpenAI fine-tuning API, driven by the real ``openai`` SDK against the router in-process.++Training itself is stubbed at the ``/api/train`` handlers the router calls; the upload directory and the+training history are the real ones, in the per-test Studio home."""++import asyncio+import json++import httpx+import pytest+from fastapi import FastAPI++openai = pytest.importorskip("openai")++from auth.authentication import authenticated_via_api_key, get_current_subject # noqa: E402+from models.training import TrainingJobResponse # noqa: E402+from routes import openai_fine_tuning, training as training_routes # noqa: E402+from storage import studio_db # noqa: E402++CHATS = (+ b'{"messages": [{"role": "user", "content": "Hi"}, {"role": "assistant", "content": "Hello!"}]}\n'+ b'{"messages": [{"role": "user", "content": "Bye"}, {"role": "assistant", "content": "See you"}]}\n'+)+++class _Status:+ def __init__(+ self,+ job_id = "",+ phase = "idle",+ ):+ self.job_id = job_id+ self.phase = phase+ self.start_request_state = None+ self.error = None+ self.message = ""+++def studio(monkeypatch):+ calls = {"start": [], "stop": []}+ live = {"status": _Status()}++ async def start_training(request, current_subject, via_api_key):+ calls["start"].append(request)+ live["status"] = _Status("job_20261021_101500_ab12cd34", "loading_model")+ return TrainingJobResponse(+ job_id = "job_20261021_101500_ab12cd34", status = "queued", message = "queued"+ )++ async def stop_training(body, current_subject):+ calls["stop"].append(body)+ return training_routes.TrainingStopResponse(status = "stopped", message = "Stop requested")++ async def get_training_status(current_subject, via_api_key):+ return live["status"]++ monkeypatch.setattr(training_routes, "start_training", start_training)+ monkeypatch.setattr(training_routes, "stop_training", stop_training)+ monkeypatch.setattr(training_routes, "get_training_status", get_training_status)++ app = FastAPI()+ app.include_router(openai_fine_tuning.router, prefix = "/v1")+ app.dependency_overrides[get_current_subject] = lambda: "unsloth"+ app.dependency_overrides[authenticated_via_api_key] = lambda: True+ client = openai.AsyncOpenAI(+ api_key = "sk-unsloth-test",+ base_url = "http://studio.test/v1",+ http_client = httpx.AsyncClient(transport = httpx.ASGITransport(app = app)),+ max_retries = 0,+ )+ return client, calls, live+++def _run(coro):+ return asyncio.run(coro)+++def _finished_run(+ tmp_path,+ training_file,+ status = "completed",+ error = None,+):+ output_dir = tmp_path / "unsloth_Qwen3-0.6B_1761041700"+ (output_dir / "checkpoint-2").mkdir(parents = True)+ config = {+ "model_name": "unsloth/Qwen3-0.6B",+ "local_datasets": [str(training_file)],+ "num_epochs": 2,+ "batch_size": 4,+ "learning_rate": "0.0001",+ "random_seed": 7,+ "project_name": "support-bot",+ }+ run_id = "job_20261021_101500_ab12cd34"+ studio_db.create_run(+ id = run_id,+ model_name = config["model_name"],+ dataset_name = str(training_file),+ config_json = json.dumps(config),+ started_at = "2026-10-21T10:15:00+00:00",+ total_steps = 4,+ output_dir = str(output_dir),+ )+ studio_db.insert_metrics_batch(+ run_id,+ [+ {+ "step": step,+ "loss": 2.0 - step / 4,+ "num_tokens": 100 * step,+ "elapsed_seconds": step,+ }+ for step in range(1, 5)+ ],+ )+ studio_db.finish_run(+ id = run_id,+ status = status,+ ended_at = "2026-10-21T10:20:00+00:00",+ final_step = 4,+ final_loss = 1.0,+ duration_seconds = 300.0,+ output_dir = str(output_dir),+ error_message = error,+ )+ return run_id+++def test_upload_then_create_starts_a_studio_run(studio):+ client, calls, _live = studio++ async def flow():+ uploaded = await client.files.create(file = ("chats.jsonl", CHATS), purpose = "fine-tune")+ second = await client.files.create(file = ("more.jsonl", CHATS), purpose = "fine-tune")+ listed = await client.files.list()+ first_page = await client.files.list(limit = 1)+ oldest_first = await client.files.list(order = "asc")+ job = await client.fine_tuning.jobs.create(+ model = "unsloth/Qwen3-0.6B",+ training_file = uploaded.id,+ validation_file = uploaded.id,+ suffix = "support-bot",+ seed = 7,+ method = {+ "type": "supervised",+ "supervised": {+ "hyperparameters": {+ "n_epochs": 2,+ "batch_size": 4,+ "learning_rate_multiplier": 0.5,+ }+ },+ },+ integrations = [{"type": "wandb", "wandb": {"project": "support"}}],+ extra_body = {"lora_r": 32, "max_seq_length": 4096},+ )+ retrieved = await client.fine_tuning.jobs.retrieve(job.id)+ return uploaded, second, listed, first_page, oldest_first, job, retrieved++ uploaded, second, listed, first_page, oldest_first, job, retrieved = _run(flow())+ assert uploaded.id.startswith("file-") and uploaded.filename == "chats.jsonl"+ assert uploaded.bytes == len(CHATS)+ assert {f.id for f in listed.data} == {uploaded.id, second.id}+ assert len(first_page.data) == 1 and first_page.has_more+ assert [f.id for f in oldest_first.data] == [f.id for f in listed.data][::-1]++ [request] = calls["start"]+ assert request.model_name == "unsloth/Qwen3-0.6B"+ assert request.training_type == "LoRA/QLoRA" and request.format_type == "auto"+ assert request.local_datasets[0].endswith("_chats.jsonl")+ assert open(request.local_datasets[0], "rb").read() == CHATS+ assert (request.num_epochs, request.batch_size, request.random_seed) == (2, 4, 7)+ assert float(request.learning_rate) == 1e-4+ assert request.local_eval_datasets == request.local_datasets and request.eval_steps > 0+ assert request.enable_wandb and request.wandb_project == "support"+ assert (request.lora_r, request.max_seq_length, request.project_name) == (+ 32,+ 4096,+ "support-bot",+ )++ assert job.id == "ftjob_20261021_101500_ab12cd34"+ assert job.training_file == uploaded.id+ # No history row until the worker is up: the live training status answers instead.+ assert retrieved.status == "validating_files"+ assert (retrieved.model, retrieved.training_file) == ("unsloth/Qwen3-0.6B", uploaded.id)+++def test_finished_run_reads_as_a_succeeded_job(studio, tmp_path):+ client, _calls, _live = studio+ training_file = tmp_path / ("0123456789abcdef0123456789abcdef" + "_chats.jsonl")+ _finished_run(tmp_path, training_file)++ async def flow():+ job = await client.fine_tuning.jobs.retrieve("ftjob_20261021_101500_ab12cd34")+ jobs = await client.fine_tuning.jobs.list()+ events = await client.fine_tuning.jobs.list_events(job.id, limit = 100)+ checkpoints = await client.fine_tuning.jobs.checkpoints.list(job.id)+ return job, jobs, events, checkpoints++ job, jobs, events, checkpoints = _run(flow())+ assert job.status == "succeeded"+ assert job.fine_tuned_model == "unsloth_Qwen3-0.6B_1761041700"+ assert job.training_file == "file-0123456789abcdef0123456789abcdef"+ assert job.trained_tokens == 400 and job.seed == 7+ assert job.hyperparameters.n_epochs == 2 and job.hyperparameters.learning_rate_multiplier == 0.5+ assert job.finished_at - job.created_at == 300+ assert [j.id for j in jobs.data] == [job.id]++ messages = [e.message for e in events.data]+ assert messages[0] == "The job has successfully completed"+ assert messages[-1] == "Fine-tuning job started"+ assert "Step 4/4: training loss=1.0000" in messages+ metrics = [e for e in events.data if e.type == "metrics"]+ assert [e.data["step"] for e in metrics] == [4, 3, 2, 1]++ [checkpoint] = checkpoints.data+ assert checkpoint.step_number == 2+ assert checkpoint.fine_tuned_model_checkpoint == "unsloth_Qwen3-0.6B_1761041700/checkpoint-2"+ assert checkpoint.metrics.train_loss == 1.5+++def test_failed_run_keeps_host_paths_from_an_api_key(studio, tmp_path):+ client, _calls, _live = studio+ training_file = tmp_path / ("0123456789abcdef0123456789abcdef" + "_chats.jsonl")+ _finished_run(+ tmp_path,+ training_file,+ status = "error",+ error = f"No such file: {tmp_path}/secret/model.safetensors",+ )++ job = _run(client.fine_tuning.jobs.retrieve("ftjob_20261021_101500_ab12cd34"))+ assert job.status == "failed" and job.fine_tuned_model is None+ assert str(tmp_path) not in job.error.message+++def test_cancel_stops_the_running_job(studio):+ client, calls, live = studio+ live["status"] = _Status("job_20261021_101500_ab12cd34", "training")+ studio_db.create_run(+ id = "job_20261021_101500_ab12cd34",+ model_name = "unsloth/Qwen3-0.6B",+ dataset_name = "chats.jsonl",+ config_json = "{}",+ started_at = "2026-10-21T10:15:00+00:00",+ total_steps = 4,+ )++ async def flow():+ cancelled = await client.fine_tuning.jobs.cancel("ftjob_20261021_101500_ab12cd34")+ # The worker is still finishing its step, so the row still reads running.+ return cancelled, await client.fine_tuning.jobs.retrieve(cancelled.id)++ job, retrieved = _run(flow())+ assert job.status == "cancelled" and retrieved.status == "cancelled"+ [stop] = calls["stop"]+ assert stop.expected_job_id == "job_20261021_101500_ab12cd34" and stop.save is True+++def test_refusals_are_openai_errors(studio):+ client, calls, _live = studio++ async def flow():+ uploaded = await client.files.create(file = ("chats.jsonl", CHATS), purpose = "fine-tune")+ errors = []+ for kwargs in (+ {"training_file": "file-missing"},+ {"training_file": uploaded.id, "method": {"type": "dpo"}},+ {"training_file": uploaded.id, "method": {"type": "supervised", "supervised": "x"}},+ {"training_file": uploaded.id, "extra_body": {"local_datasets": ["/etc/passwd"]}},+ ):+ try:+ await client.fine_tuning.jobs.create(model = "unsloth/Qwen3-0.6B", **kwargs)+ except openai.APIStatusError as exc:+ errors.append(exc.status_code)+ try:+ await client.files.create(file = ("notes.txt", b"x"), purpose = "assistants")+ except openai.BadRequestError:+ errors.append(400)+ try:+ await client.fine_tuning.jobs.retrieve("ftjob_unknown")+ except openai.NotFoundError:+ errors.append(404)+ try:+ await client.fine_tuning.jobs.pause("ftjob_unknown")+ except openai.BadRequestError:+ errors.append(400)+ return errors++ assert _run(flow()) == [404, 400, 400, 400, 400, 404, 400]+ assert calls["start"] == []+++def test_list_pages_through_the_whole_history(studio, monkeypatch):+ client, _calls, _live = studio+ runs = [{"id": f"job_{i:03d}", "status": "completed", "config_json": "{}"} for i in range(450)]+ monkeypatch.setattr(+ openai_fine_tuning,+ "list_runs",+ lambda limit, offset: {"runs": runs[offset : offset + limit], "total": len(runs)},+ )+ monkeypatch.setattr(openai_fine_tuning, "_trained_tokens", lambda run_id: None)++ async def every_id():+ return [job.id async for job in client.fine_tuning.jobs.list(limit = 100)]++ assert _run(every_id()) == [f"ftjob_{i:03d}" for i in range(450)]+++def test_a_live_job_cursor_still_reaches_the_history(studio, tmp_path):+ client, _calls, live = studio+ _finished_run(tmp_path, tmp_path / ("0123456789abcdef0123456789abcdef" + "_chats.jsonl"))+ live["status"] = _Status("job_20261021_111500_cd34ef56", "loading_model")++ async def every_id():+ return [job.id async for job in client.fine_tuning.jobs.list(limit = 1)]++ assert _run(every_id()) == ["ftjob_20261021_111500_cd34ef56", "ftjob_20261021_101500_ab12cd34"]+++def test_a_legacy_non_finite_config_still_lists(studio, tmp_path):+ client, _calls, _live = studio+ run_id = _finished_run(+ tmp_path, tmp_path / ("0123456789abcdef0123456789abcdef" + "_chats.jsonl")+ )+ conn = studio_db.get_connection()+ conn.execute(+ "UPDATE training_runs SET config_json = ? WHERE id = ?",+ ('{"model_name": "unsloth/Qwen3-0.6B", "learning_rate": Infinity}', run_id),+ )+ conn.commit()+ conn.close()++ async def listed():+ return await client.fine_tuning.jobs.list()++ assert [j.status for j in _run(listed()).data] == ["succeeded"]+++def test_cancel_before_the_run_row_sticks(studio):+ client, _calls, live = studio+ live["status"] = _Status("job_20261021_101500_ab12cd34", "loading_model")++ async def flow():+ await client.fine_tuning.jobs.cancel("ftjob_20261021_101500_ab12cd34")+ return await client.fine_tuning.jobs.retrieve("ftjob_20261021_101500_ab12cd34")++ assert _run(flow()).status == "cancelled"+++def test_an_overflowed_grad_norm_keeps_events_readable(studio, tmp_path):+ client, _calls, _live = studio+ run_id = _finished_run(+ tmp_path, tmp_path / ("0123456789abcdef0123456789abcdef" + "_chats.jsonl")+ )+ studio_db.insert_metrics_batch(+ run_id, [{"step": 2, "loss": float("nan"), "grad_norm": float("inf")}]+ )++ async def flow():+ events = await client.fine_tuning.jobs.list_events(f"ft{run_id}", limit = 100)+ checkpoints = await client.fine_tuning.jobs.checkpoints.list(f"ft{run_id}")+ return events, checkpoints++ events, checkpoints = _run(flow())+ [step2] = [e for e in events.data if e.type == "metrics" and e.data["step"] == 2]+ assert step2.data["grad_norm"] is None+ assert checkpoints.data[0].step_number == 2