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: add Download Dataset button for Data Recipes (#10708)

Sourav Rajvid58469217729
diff --git a/studio/backend/auth/authentication.py b/studio/backend/auth/authentication.py
index cc7f63f9ca9..c2e4436176a 100644
--- a/studio/backend/auth/authentication.py
+++ b/studio/backend/auth/authentication.py
@@ -365,6 +365,21 @@ async def credentials_for_token(
return _KEYLESS_CREDENTIALS if keyless else None
+async def subject_for_header_or_query_token(request: Any, token: Optional[str]) -> str:
+ """The subject of the bearer in ``Authorization``, or failing that in a ``?token=`` the route
+ read for itself. An ``<img src>`` and the native save command fetch without a header."""
+ header = request.headers.get("authorization") or ""
+ header_token = header[7:] if header.lower().startswith("bearer ") else ""
+ # A blank header is the absent header, so the `?token=` such a caller sends is still owed.
+ credentials = await credentials_for_token(request, header_token.strip() or token or None)
+ if credentials is None:
+ raise HTTPException(
+ status_code = status.HTTP_401_UNAUTHORIZED,
+ detail = "Missing authentication token",
+ )
+ return await get_current_subject(credentials)
+
+
async def authenticated_without_credential(
credentials: HTTPAuthorizationCredentials = Depends(security),
) -> bool:
diff --git a/studio/backend/core/data_recipe/export.py b/studio/backend/core/data_recipe/export.py
new file mode 100644
index 00000000000..30bfd8b9781
--- /dev/null
+++ b/studio/backend/core/data_recipe/export.py
@@ -0,0 +1,219 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Export recipe dataset artifacts to downloadable files."""
+
+from __future__ import annotations
+
+import base64
+import json
+import math
+import tempfile
+import zipfile
+from decimal import Decimal
+from pathlib import Path
+from typing import Any, Literal
+
+from core.data_recipe.huggingface import (
+ RecipeDatasetPublishError,
+ _resolve_recipe_artifact_path,
+)
+from core.data_recipe.jsonable import to_jsonable
+from utils.paths.path_utils import drop_appledouble_metadata
+
+ExportFormat = Literal["jsonl", "parquet"]
+
+_JSONL_EXPORT_BATCH_ROWS = 8192
+
+
+class RecipeDatasetExportError(ValueError):
+ """Raised when a recipe dataset cannot be exported."""
+
+
+class _JsonNumber(str):
+ """A validated JSON number that must be emitted without string quotes."""
+
+
+def _parquet_dir(dataset_path: Path) -> Path:
+ parquet_dir = dataset_path / "parquet-files"
+ if not parquet_dir.exists():
+ raise RecipeDatasetExportError(f"Dataset parquet files missing: {parquet_dir}")
+ if not _parquet_files(parquet_dir):
+ raise RecipeDatasetExportError(f"No parquet files found in {parquet_dir}")
+ return parquet_dir
+
+
+def _parquet_files(parquet_dir: Path) -> list[Path]:
+ """The real shards, without the ``._batch.parquet`` companions a macOS volume leaves beside
+ them: the glob matches those but no reader can parse one. Same call the worker makes."""
+ return drop_appledouble_metadata(sorted(parquet_dir.glob("*.parquet")))
+
+
+def _sanitize_json_value(value: Any) -> Any:
+ if isinstance(value, float):
+ return None if not math.isfinite(value) else value
+ if isinstance(value, Decimal):
+ return None if not value.is_finite() else _JsonNumber(str(value))
+ if isinstance(value, (bytes, bytearray)):
+ return base64.b64encode(bytes(value)).decode("ascii")
+ if isinstance(value, dict):
+ return {str(key): _sanitize_json_value(item) for key, item in value.items()}
+ if isinstance(value, (list, tuple, set)):
+ return [_sanitize_json_value(item) for item in value]
+ converted = to_jsonable(value)
+ if converted is not value:
+ return _sanitize_json_value(converted)
+ if isinstance(converted, float):
+ return None if not math.isfinite(converted) else converted
+ if converted is None or isinstance(converted, (str, int, bool)):
+ return converted
+ return str(converted)
+
+
+def _json_dumps_value(value: Any) -> str:
+ if isinstance(value, _JsonNumber):
+ return str(value)
+ if isinstance(value, dict):
+ return (
+ "{"
+ + ", ".join(
+ f"{json.dumps(str(key), ensure_ascii = False)}: {_json_dumps_value(item)}"
+ for key, item in value.items()
+ )
+ + "}"
+ )
+ if isinstance(value, list):
+ return "[" + ", ".join(_json_dumps_value(item) for item in value) + "]"
+ return json.dumps(value, ensure_ascii = False, allow_nan = False)
+
+
+def _json_dumps_row(row: dict[str, Any]) -> str:
+ return _json_dumps_value(_sanitize_json_value(row))
+
+
+def write_jsonl_rows(handle, rows: list[dict[str, Any]]) -> None:
+ """Write JSONL rows with the same durable value encoding used for parquet artifacts."""
+ for row in rows:
+ handle.write(_json_dumps_row(row))
+ handle.write("\n")
+
+
+def _write_jsonl_from_parquet(parquet_dir: Path, destination: Path) -> None:
+ """Stream sorted parquet shards through the one supported JSONL reader."""
+ try:
+ import pyarrow.parquet as pyarrow_parquet # type: ignore
+ except ImportError as exc:
+ raise RecipeDatasetExportError("PyArrow is required to export recipe datasets.") from exc
+
+ parquet_files = _parquet_files(parquet_dir)
+ try:
+ with destination.open("w", encoding = "utf-8") as handle:
+ for path in parquet_files:
+ parquet_file = pyarrow_parquet.ParquetFile(path)
+ for batch in parquet_file.iter_batches(batch_size = _JSONL_EXPORT_BATCH_ROWS):
+ write_jsonl_rows(
+ handle,
+ batch.to_pylist(),
+ )
+ except Exception as exc:
+ raise RecipeDatasetExportError(f"Could not export recipe parquet data: {exc}") from exc
+
+
+def safe_filename_stem(value: str) -> str:
+ cleaned = "".join(ch if ch.isalnum() or ch in {"-", "_"} else "-" for ch in value.strip())
+ cleaned = cleaned.strip("-_")
+ return cleaned or "recipe-dataset"
+
+
+def _artifact_image_files(dataset_path: Path) -> list[Path]:
+ images_dir = dataset_path / "images"
+ if not images_dir.is_dir():
+ return []
+ return [
+ path for path in drop_appledouble_metadata(sorted(images_dir.rglob("*"))) if path.is_file()
+ ]
+
+
+def download_filename(*, artifact_path: str, export_format: ExportFormat, stem: str) -> str:
+ """The name the export will have, and the checks ``build_dataset_download`` will make, so a
+ run whose shards are gone is refused while the caller can still be told."""
+ try:
+ dataset_path = _resolve_recipe_artifact_path(artifact_path)
+ except RecipeDatasetPublishError as exc:
+ raise RecipeDatasetExportError(str(exc)) from exc
+ _parquet_dir(dataset_path)
+ if export_format == "parquet":
+ return f"{stem}.parquet.zip"
+ return f"{stem}.jsonl.zip" if _artifact_image_files(dataset_path) else f"{stem}.jsonl"
+
+
+def _add_images_to_archive(archive: zipfile.ZipFile, dataset_path: Path) -> None:
+ images_dir = dataset_path / "images"
+ if not images_dir.is_dir():
+ return
+ for image_file in drop_appledouble_metadata(sorted(images_dir.rglob("*"))):
+ if not image_file.is_file():
+ continue
+ relative_path = image_file.relative_to(images_dir)
+ # Stored, not deflated: deflating already-compressed images bought nothing and cost 7x.
+ archive.write(
+ image_file,
+ arcname = str(Path("images") / relative_path),
+ compress_type = zipfile.ZIP_STORED,
+ )
+
+
+def build_dataset_download(
+ *, artifact_path: str, export_format: ExportFormat, filename_stem: str
+) -> tuple[Path, str, str]:
+ """Return ``(temp_path, media_type, download_filename)``.
+
+ The caller is responsible for deleting ``temp_path`` after the response is sent.
+ """
+ try:
+ dataset_path = _resolve_recipe_artifact_path(artifact_path)
+ except RecipeDatasetPublishError as exc:
+ raise RecipeDatasetExportError(str(exc)) from exc
+
+ stem = safe_filename_stem(filename_stem)
+ parquet_dir = _parquet_dir(dataset_path)
+
+ if export_format == "parquet":
+ tmp = tempfile.NamedTemporaryFile(delete = False, suffix = ".zip")
+ tmp.close()
+ zip_path = Path(tmp.name)
+ # Only a successful build's caller unlinks the temp file, so a failed one takes its own.
+ try:
+ with zipfile.ZipFile(zip_path, "w", compression = zipfile.ZIP_DEFLATED) as archive:
+ for parquet_file in _parquet_files(parquet_dir):
+ archive.write(parquet_file, arcname = parquet_file.name)
+ _add_images_to_archive(archive, dataset_path)
+ except BaseException:
+ zip_path.unlink(missing_ok = True)
+ raise
+ return zip_path, "application/zip", f"{stem}.parquet.zip"
+
+ tmp = tempfile.NamedTemporaryFile(delete = False, suffix = ".jsonl")
+ tmp.close()
+ jsonl_path = Path(tmp.name)
+ try:
+ _write_jsonl_from_parquet(parquet_dir, jsonl_path)
+ image_files = _artifact_image_files(dataset_path)
+ if not image_files:
+ return jsonl_path, "application/x-ndjson", f"{stem}.jsonl"
+ # Rows reference these by relative path, so a bare JSONL loses every image.
+ zip_tmp = tempfile.NamedTemporaryFile(delete = False, suffix = ".zip")
+ zip_tmp.close()
+ zip_path = Path(zip_tmp.name)
+ try:
+ with zipfile.ZipFile(zip_path, "w", compression = zipfile.ZIP_DEFLATED) as archive:
+ archive.write(jsonl_path, arcname = f"{stem}.jsonl")
+ _add_images_to_archive(archive, dataset_path)
+ except BaseException:
+ zip_path.unlink(missing_ok = True)
+ raise
+ except BaseException:
+ jsonl_path.unlink(missing_ok = True)
+ raise
+ jsonl_path.unlink(missing_ok = True)
+ return zip_path, "application/zip", f"{stem}.jsonl.zip"
diff --git a/studio/backend/core/data_recipe/huggingface.py b/studio/backend/core/data_recipe/huggingface.py
index e02f06875f3..395ae69bae8 100644
--- a/studio/backend/core/data_recipe/huggingface.py
+++ b/studio/backend/core/data_recipe/huggingface.py
@@ -24,7 +24,13 @@ class RecipeDatasetPublishError(ValueError):
def _resolve_recipe_artifact_path(artifact_path: str) -> Path:
root = recipe_datasets_root().expanduser().resolve()
- candidate = resolve_dataset_path(artifact_path).expanduser()
+ try:
+ candidate = resolve_dataset_path(artifact_path).expanduser()
+ except ValueError as exc:
+ # Outside every dataset root, so it never reaches the check below: a 500, not a refusal.
+ raise RecipeDatasetPublishError(
+ "This execution artifact is outside the Recipe Studio dataset storage."
+ ) from exc
resolved = candidate.resolve(strict = False)
try:
diff --git a/studio/backend/core/data_recipe/jobs/manager.py b/studio/backend/core/data_recipe/jobs/manager.py
index ff8cbab2501..15a827587cb 100644
--- a/studio/backend/core/data_recipe/jobs/manager.py
+++ b/studio/backend/core/data_recipe/jobs/manager.py
@@ -28,7 +28,7 @@ from typing import Any
import multiprocessing as mp
-from ..jsonable import to_preview_jsonable
+from ..jsonable import to_preview_jsonable_row
from .constants import (
EVENT_JOB_CANCELLING,
EVENT_JOB_CANCELLED,
@@ -414,7 +414,7 @@ class JobManager:
dataframe = dataframe.drop(columns = [helper_col])
rows = dataframe.to_dict(orient = "records")
- return {"dataset": to_preview_jsonable(rows), "total": total}
+ return {"dataset": to_preview_jsonable_row(rows), "total": total}
@staticmethod
def _load_dataset_page_with_data_designer(
@@ -425,7 +425,7 @@ class JobManager:
dataframe = read_parquet_dataset(parquet_dir)
total = int(len(dataframe.index))
rows = dataframe.iloc[offset : offset + limit].to_dict(orient = "records")
- return {"dataset": to_preview_jsonable(rows), "total": total}
+ return {"dataset": to_preview_jsonable_row(rows), "total": total}
@job_read(lambda self, *args, **kwargs: None)
def subscribe(
diff --git a/studio/backend/core/data_recipe/jobs/worker.py b/studio/backend/core/data_recipe/jobs/worker.py
index 5a6373d1b65..9b8b04d9d7f 100644
--- a/studio/backend/core/data_recipe/jobs/worker.py
+++ b/studio/backend/core/data_recipe/jobs/worker.py
@@ -15,7 +15,7 @@ import unicodedata
from pathlib import Path
from typing import Any
-from ..jsonable import to_jsonable, to_preview_jsonable
+from ..jsonable import to_jsonable, to_preview_jsonable_row
from .constants import EVENT_JOB_COMPLETED, EVENT_JOB_ERROR, EVENT_JOB_STARTED
from ..service import build_config_builder, create_data_designer, install_public_egress_guard
from utils.paths.lazy import LazyPath
@@ -149,7 +149,7 @@ def run_job_process(*, event_queue, recipe: dict[str, Any], run: dict[str, Any])
dataset = (
[]
if results.dataset is None
- else to_preview_jsonable(results.dataset.to_dict(orient = "records"))
+ else to_preview_jsonable_row(results.dataset.to_dict(orient = "records"))
)
processor_artifacts = (
None
diff --git a/studio/backend/core/data_recipe/jsonable.py b/studio/backend/core/data_recipe/jsonable.py
index 5a4fcd35c33..d8107341687 100644
--- a/studio/backend/core/data_recipe/jsonable.py
+++ b/studio/backend/core/data_recipe/jsonable.py
@@ -5,6 +5,7 @@ from __future__ import annotations
import base64
import io
+from decimal import Decimal
from pathlib import Path
from typing import Any
@@ -59,6 +60,28 @@ def _to_pil_from_hf_image_dict(value: Any) -> Any | None:
return None
+# Resolved once: to_jsonable runs per value, and importing pandas per value cost 30% of it. The
+# placeholder is a private object rather than None, which a real value can be.
+_NO_SENTINEL = object()
+_PANDAS_NA: Any = _NO_SENTINEL
+_PANDAS_NAT: Any = _NO_SENTINEL
+_PANDAS_SENTINELS_READY = False
+
+
+def _is_pandas_missing(value: Any) -> bool:
+ """pandas' own missing sentinels. Identity rather than ``pd.isna``, which answers element-wise
+ for a list or an array; these two are singletons."""
+ global _PANDAS_NA, _PANDAS_NAT, _PANDAS_SENTINELS_READY
+ if not _PANDAS_SENTINELS_READY:
+ try:
+ import pandas as pd # type: ignore
+ _PANDAS_NA, _PANDAS_NAT = pd.NA, pd.NaT
+ except ImportError: # pragma: no cover
+ _PANDAS_NA = _PANDAS_NAT = _NO_SENTINEL
+ _PANDAS_SENTINELS_READY = True
+ return value is _PANDAS_NA or value is _PANDAS_NAT
+
+
def to_jsonable(value: Any) -> Any:
"""Convert numpy/pandas-ish values into plain JSON-safe values."""
try:
@@ -66,6 +89,14 @@ def to_jsonable(value: Any) -> Any:
except ImportError: # pragma: no cover
np = None # type: ignore
+ # Ahead of everything below: NaT isoformat()s to "NaT" and NA hits the str() fallback.
+ if _is_pandas_missing(value):
+ return None
+
+ # DuckDB hands a DECIMAL back as a float and pyarrow as a Decimal: 1.2 against "1.20".
+ if isinstance(value, Decimal):
+ return float(value)
+
if np is not None:
if isinstance(value, np.ndarray):
return value.tolist()
@@ -117,3 +148,16 @@ def to_preview_jsonable(value: Any) -> Any:
if isinstance(converted, (bytes, bytearray)):
return base64.b64encode(bytes(converted)).decode("ascii")
return str(converted)
+
+
+def to_preview_jsonable_row(row: Any) -> Any:
+ """A dataset row, converted a column at a time.
+
+ ``to_preview_jsonable`` answers about a VALUE, and its Hugging Face image detection matches any
+ mapping carrying ``bytes`` or ``path`` -- which a row can be. Handing it a whole row replaced
+ every column, labels included, with one JPEG preview payload."""
+ if isinstance(row, list):
+ return [to_preview_jsonable_row(item) for item in row]
+ if not isinstance(row, dict):
+ return to_preview_jsonable(row)
+ return {str(key): to_preview_jsonable(value) for key, value in row.items()}
diff --git a/studio/backend/routes/data_recipe/__init__.py b/studio/backend/routes/data_recipe/__init__.py
index c596f189d4a..b7b346e0133 100644
--- a/studio/backend/routes/data_recipe/__init__.py
+++ b/studio/backend/routes/data_recipe/__init__.py
@@ -16,15 +16,22 @@ backend_path = Path(__file__).parent.parent.parent
if str(backend_path) not in sys.path:
sys.path.insert(0, str(backend_path))
+from .jobs import download_router as jobs_download_router
from .jobs import router as jobs_router
from .mcp import router as mcp_router
from .seed import router as seed_router
from .validate import router as validate_router
-router = APIRouter(dependencies = [Depends(get_current_subject)])
-router.include_router(seed_router)
-router.include_router(validate_router)
-router.include_router(jobs_router)
-router.include_router(mcp_router)
+_header_authenticated = APIRouter(dependencies = [Depends(get_current_subject)])
+_header_authenticated.include_router(seed_router)
+_header_authenticated.include_router(validate_router)
+_header_authenticated.include_router(jobs_router)
+_header_authenticated.include_router(mcp_router)
+
+router = APIRouter()
+# Kept out of the group above: the download link is fetched without a header, so it brings its
+# own guard.
+router.include_router(jobs_download_router)
+router.include_router(_header_authenticated)
__all__ = ["router"]
diff --git a/studio/backend/routes/data_recipe/jobs.py b/studio/backend/routes/data_recipe/jobs.py
index 7bfb1dac019..88197e866f4 100644
--- a/studio/backend/routes/data_recipe/jobs.py
+++ b/studio/backend/routes/data_recipe/jobs.py
@@ -8,23 +8,38 @@ from __future__ import annotations
from utils.account_context import current_account
from core.training.account_jobs import account_event_stream
import copy
+import hashlib
+import hmac
+import secrets
+import tempfile
import time
from datetime import datetime, timedelta, timezone
+from pathlib import Path
from typing import Annotated, Any, Optional
-from urllib.parse import urlparse
+from urllib.parse import urlencode, urlparse
-from fastapi import APIRouter, Depends, HTTPException, Query, Request
+from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Query, Request
from auth.authentication import (
allow_ambient_hf_token,
authenticated_via_api_key,
get_current_credential,
+ request_admitted_without_credential,
require_ui_session_for_local_commands,
+ subject_for_header_or_query_token,
)
from auth.storage import CredentialRotated
-from fastapi.responses import JSONResponse, StreamingResponse
+from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
from pydantic import ValidationError
+from core.data_recipe.export import (
+ ExportFormat,
+ RecipeDatasetExportError,
+ build_dataset_download,
+ download_filename,
+ safe_filename_stem,
+ write_jsonl_rows,
+)
from core.data_recipe.huggingface import (
RecipeDatasetPublishError,
publish_recipe_dataset,
@@ -44,6 +59,63 @@ from utils.utils import safe_error_detail, safe_curated_detail, log_and_http_err
logger = get_logger(__name__)
router = APIRouter()
+# Fetched without an Authorization header, so the link carries an HMAC capability rather than the
+# session token, which download history would keep. Same shape as the signed video links.
+# The native save dialog opens before the request, so this outlasts a user sitting on it.
+_DOWNLOAD_LINK_TTL = 30 * 60
+_DOWNLOAD_LINK_SECRET = secrets.token_bytes(32)
+
+
+def _download_link_payload(
+ *, job_id: str, export_format: str, artifact_path: str | None, filename: str | None
+) -> str:
+ # Every parameter the export reads, or the holder could swap artifact_path for another run's.
+ parts = [job_id, export_format, artifact_path or "", filename or ""]
+ return "\x1f".join(parts)
+
+
+def _sign_download_link(**parts: Any) -> str:
+ expires_at = int(time.time()) + _DOWNLOAD_LINK_TTL
+ payload = f"{_download_link_payload(**parts)}\x1f{expires_at}"
+ signature = hmac.new(_DOWNLOAD_LINK_SECRET, payload.encode(), hashlib.sha256).hexdigest()
+ return f"{expires_at}.{signature}"
+
+
+def _download_link_authorizes(token: str, **parts: Any) -> bool:
+ try:
+ expires_at, signature = token.rsplit(".", 1)
+ if int(expires_at) < int(time.time()):
+ return False
+ except ValueError:
+ return False
+ payload = f"{_download_link_payload(**parts)}\x1f{expires_at}"
+ expected = hmac.new(_DOWNLOAD_LINK_SECRET, payload.encode(), hashlib.sha256).hexdigest()
+ return hmac.compare_digest(signature, expected)
+
+
+async def _authorize_dataset_download(
+ request: Request,
+ job_id: str,
+ export_format: ExportFormat = Query(default = "jsonl", alias = "format"),
+ artifact_path: str | None = Query(default = None),
+ filename: str | None = Query(default = None),
+ token: str | None = Query(default = None),
+) -> None:
+ """A signed link for exactly this export, or the ordinary Authorization header for an API
+ client. The session bearer is deliberately not read from the query."""
+ if token and _download_link_authorizes(
+ token,
+ job_id = job_id,
+ export_format = export_format,
+ artifact_path = artifact_path,
+ filename = filename,
+ ):
+ return
+ await subject_for_header_or_query_token(request, None)
+
+
+download_router = APIRouter(dependencies = [Depends(_authorize_dataset_download)])
+
# Keepalive cadence, well inside the ~100s a quick tunnel allows between body bytes.
_KEEPALIVE_EVERY_S = 15.0
@@ -528,6 +600,208 @@ def job_dataset(
}
+def _content_disposition_attachment(filename: str) -> str:
+ ascii_name = "".join(
+ ch if 32 <= ord(ch) < 127 and ch not in {'"', "\\"} else "_" for ch in filename
+ )
+ if not ascii_name.strip("_"):
+ ascii_name = "dataset.jsonl"
+ from urllib.parse import quote
+
+ return f"attachment; filename=\"{ascii_name}\"; filename*=UTF-8''{quote(filename)}"
+
+
+_IN_MEMORY_DOWNLOAD_PAGE_SIZE = 10_000
+
+
+def _build_in_memory_job_dataset_download(
+ mgr, job_id: str, *, filename_stem: str
+) -> tuple[Path, str, str]:
+ tmp = tempfile.NamedTemporaryFile(delete = False, suffix = ".jsonl")
+ tmp.close()
+ jsonl_path = Path(tmp.name)
+ offset = 0
+ total: int | None = None
+ # Nothing has registered the temp file for cleanup yet, so an early return has to unlink it.
+ try:
+ with jsonl_path.open("w", encoding = "utf-8") as handle:
+ while True:
+ result = mgr.get_dataset(
+ job_id,
+ limit = _IN_MEMORY_DOWNLOAD_PAGE_SIZE,
+ offset = offset,
+ )
+ if result is None:
+ raise HTTPException(status_code = 404, detail = "dataset not ready")
+ if "error" in result:
+ raise HTTPException(status_code = 422, detail = result["error"])
+ rows = result.get("dataset")
+ if not isinstance(rows, list):
+ raise HTTPException(status_code = 404, detail = "dataset not ready")
+ if total is None:
+ total_value = result.get("total")
+ total = int(total_value) if isinstance(total_value, int) else len(rows)
+ if not rows:
+ break
+ write_jsonl_rows(handle, rows)
+ offset += len(rows)
+ if offset >= total:
+ break
+ if offset == 0:
+ raise HTTPException(status_code = 404, detail = "dataset not ready")
+ except BaseException:
+ jsonl_path.unlink(missing_ok = True)
+ raise
+ stem = safe_filename_stem(filename_stem.strip() or job_id)
+ return jsonl_path, "application/x-ndjson", f"{stem}.jsonl"
+
+
+def _resolve_download_artifact_path(*, job_id: str, artifact_path: str | None) -> str | None:
+ resolved = (
+ artifact_path.strip() if isinstance(artifact_path, str) and artifact_path.strip() else None
+ )
+ mgr = get_job_manager()
+ status = mgr.get_status(job_id)
+ if status is not None:
+ if status.get("status") != "completed":
+ raise HTTPException(
+ status_code = 409,
+ detail = "Only completed runs can be downloaded.",
+ )
+ status_artifact = status.get("artifact_path")
+ if isinstance(status_artifact, str) and status_artifact.strip():
+ resolved = status_artifact.strip()
+ return resolved
+
+
[email protected]("/jobs/{job_id}/download-url")
+def create_job_dataset_download_url(
+ job_id: str,
+ export_format: ExportFormat = Query(default = "jsonl", alias = "format"),
+ artifact_path: str | None = Query(default = None),
+ filename: str | None = Query(default = None),
+ no_credential: Annotated[bool, Depends(request_admitted_without_credential)] = False,
+):
+ """Mint the signed link the browser or the native downloader then fetches.
+
+ Bearer-gated like the rest of the package. It settles here whether the run can be exported at
+ all, and under what name, so a failure lands in the UI instead of after the save dialog has
+ opened. The URL is relative, so it survives whatever proxy the page itself came through."""
+ if no_credential:
+ # The capability outlives the setting that admitted this caller. As the video links do.
+ raise HTTPException(
+ status_code = 403,
+ detail = "Dataset download links can only be created from the Unsloth UI or with an API key.",
+ )
+ resolved = _resolve_download_artifact_path(job_id = job_id, artifact_path = artifact_path)
+ stem = safe_filename_stem(
+ filename.strip() if isinstance(filename, str) and filename.strip() else job_id
+ )
+ if resolved:
+ try:
+ name = download_filename(artifact_path = resolved, export_format = export_format, stem = stem)
+ except RecipeDatasetExportError as exc:
+ raise log_and_http_error(
+ exc,
+ 400,
+ safe_curated_detail(exc),
+ event = "data_recipe.jobs.download_url_failed",
+ log = logger,
+ ) from exc
+ else:
+ if export_format == "parquet":
+ raise HTTPException(
+ status_code = 400,
+ detail = "Parquet download requires persisted recipe artifacts.",
+ )
+ # An in-memory preview lives only while its job is current: ask before the chooser opens.
+ if get_job_manager().get_dataset(job_id, limit = 1, offset = 0) is None:
+ raise HTTPException(status_code = 404, detail = "dataset not ready")
+ name = f"{stem}.jsonl"
+
+ token = _sign_download_link(
+ job_id = job_id,
+ export_format = export_format,
+ artifact_path = artifact_path,
+ filename = filename,
+ )
+ query = {"format": export_format, "token": token}
+ if artifact_path:
+ query["artifact_path"] = artifact_path
+ if filename:
+ query["filename"] = filename
+ # Relative to this router: the frontend's data-recipe base is configurable.
+ return {"path": f"/jobs/{job_id}/download?{urlencode(query)}", "filename": name}
+
+
+@download_router.get("/jobs/{job_id}/download")
+def download_job_dataset(
+ job_id: str,
+ background_tasks: BackgroundTasks,
+ export_format: ExportFormat = Query(default = "jsonl", alias = "format"),
+ artifact_path: str | None = Query(default = None),
+ filename: str | None = Query(default = None),
+):
+ resolved_artifact = _resolve_download_artifact_path(
+ job_id = job_id,
+ artifact_path = artifact_path,
+ )
+ filename_stem = filename.strip() if isinstance(filename, str) and filename.strip() else job_id
+
+ try:
+ if resolved_artifact:
+ if export_format == "parquet":
+ file_path, media_type, download_name = build_dataset_download(
+ artifact_path = resolved_artifact,
+ export_format = "parquet",
+ filename_stem = filename_stem,
+ )
+ else:
+ file_path, media_type, download_name = build_dataset_download(
+ artifact_path = resolved_artifact,
+ export_format = "jsonl",
+ filename_stem = filename_stem,
+ )
+ else:
+ if export_format == "parquet":
+ raise HTTPException(
+ status_code = 400,
+ detail = "Parquet download requires persisted recipe artifacts.",
+ )
+ mgr = get_job_manager()
+ file_path, media_type, download_name = _build_in_memory_job_dataset_download(
+ mgr,
+ job_id,
+ filename_stem = filename_stem,
+ )
+ except RecipeDatasetExportError as exc:
+ raise log_and_http_error(
+ exc,
+ 400,
+ safe_curated_detail(exc),
+ event = "data_recipe.jobs.download_failed",
+ log = logger,
+ ) from exc
+ except HTTPException:
+ raise
+ except Exception as exc:
+ raise log_and_http_error(
+ exc,
+ 500,
+ safe_error_detail(exc),
+ event = "data_recipe.jobs.download_error",
+ log = logger,
+ ) from exc
+
+ background_tasks.add_task(file_path.unlink, missing_ok = True)
+ return FileResponse(
+ file_path,
+ media_type = media_type,
+ filename = download_name,
+ headers = {"Content-Disposition": _content_disposition_attachment(download_name)},
+ )
+
+
@router.post(
"/jobs/{job_id}/publish",
response_class = JSONResponse,
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index c8ad552757f..397d1a8359e 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -3454,19 +3454,8 @@ async def _authenticate_header_or_query(request: Request, token: Optional[str])
Routed through ``credentials_for_token`` so a scope that covers this path serves it
without a key, the way the routes behind ``security`` already do."""
- auth_header = request.headers.get("authorization") or ""
- header_token = auth_header[7:] if auth_header.lower().startswith("bearer ") else ""
- # A blank header is the absent header, so the `?token=` an <img src> sends is still owed.
- jwt_token = header_token.strip() or token or None
- from auth.authentication import credentials_for_token
-
- creds = await credentials_for_token(request, jwt_token)
- if creds is None:
- raise HTTPException(
- status_code = status.HTTP_401_UNAUTHORIZED,
- detail = "Missing authentication token",
- )
- return await get_current_subject(creds)
+ from auth.authentication import subject_for_header_or_query_token
+ return await subject_for_header_or_query_token(request, token)
@studio_router.get("/artifact-preview-frame", include_in_schema = False)
diff --git a/studio/backend/tests/multi_account/factories_training.py b/studio/backend/tests/multi_account/factories_training.py
index 1e77adaf096..9dceb470b4f 100644
--- a/studio/backend/tests/multi_account/factories_training.py
+++ b/studio/backend/tests/multi_account/factories_training.py
@@ -230,6 +230,12 @@ FACTORIES = {
"routes.data_recipe.jobs:GET:/jobs/{job_id}/dataset": Factory(
"training-recipe-job", fragment = SENTINEL
),
+ "routes.data_recipe.jobs:GET:/jobs/{job_id}/download-url": Factory(
+ "training-recipe-job", fragment = "/download?"
+ ),
+ "routes.data_recipe.jobs:GET:/jobs/{job_id}/download": Factory(
+ "training-recipe-job", fragment = SENTINEL
+ ),
"routes.data_recipe.jobs:POST:/jobs/{job_id}/cancel": Factory(
"training-recipe-job", fragment = RECIPE_JOB_ID
),
diff --git a/studio/backend/tests/test_data_recipe_dataset_download.py b/studio/backend/tests/test_data_recipe_dataset_download.py
new file mode 100644
index 00000000000..b39a724e194
--- /dev/null
+++ b/studio/backend/tests/test_data_recipe_dataset_download.py
@@ -0,0 +1,915 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+from __future__ import annotations
+
+import json
+import secrets
+import tempfile
+import zipfile
+from pathlib import Path
+
+import pytest
+from fastapi import HTTPException
+
+import math
+
+from core.data_recipe.export import (
+ RecipeDatasetExportError,
+ _json_dumps_row,
+ _sanitize_json_value,
+ build_dataset_download,
+)
+
+
+def _write_parquet_rows(parquet_dir: Path, rows: list[dict]) -> None:
+ pytest.importorskip("pyarrow")
+ pytest.importorskip("pandas")
+ import pandas as pd
+
+ parquet_dir.mkdir(parents = True, exist_ok = True)
+ pd.DataFrame(rows).to_parquet(parquet_dir / "batch_00000.parquet", index = False)
+
+
+def test_build_dataset_download_jsonl_from_artifact(tmp_path: Path, monkeypatch):
+ artifact_root = tmp_path / "recipe-datasets"
+ dataset_path = artifact_root / "job-123"
+ parquet_dir = dataset_path / "parquet-files"
+ _write_parquet_rows(parquet_dir, [{"question": "Q1", "answer": "A1"}])
+
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ file_path, media_type, download_name = build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "jsonl",
+ filename_stem = "my-run",
+ )
+ try:
+ assert media_type == "application/x-ndjson"
+ assert download_name == "my-run.jsonl"
+ lines = file_path.read_text(encoding = "utf-8").strip().splitlines()
+ assert json.loads(lines[0])["question"] == "Q1"
+ finally:
+ file_path.unlink(missing_ok = True)
+
+
+def test_json_dumps_row_replaces_non_finite_numbers_with_null():
+ row = {"score": float("nan"), "ratio": float("inf"), "label": "ok"}
+ payload = json.loads(_json_dumps_row(row))
+ assert payload == {"score": None, "ratio": None, "label": "ok"}
+
+
+def test_sanitize_json_value_handles_numpy_nan():
+ numpy = pytest.importorskip("numpy")
+ assert _sanitize_json_value(numpy.float64("nan")) is None
+ assert _sanitize_json_value(numpy.float64("inf")) is None
+ assert math.isclose(_sanitize_json_value(numpy.float64(1.5)), 1.5)
+
+
+def test_build_dataset_download_preserves_row_order_within_shard(tmp_path: Path, monkeypatch):
+ pytest.importorskip("pyarrow")
+ pytest.importorskip("pandas")
+ import pandas as pd
+
+ artifact_root = tmp_path / "recipe-datasets"
+ dataset_path = artifact_root / "job-order"
+ parquet_dir = dataset_path / "parquet-files"
+ parquet_dir.mkdir(parents = True, exist_ok = True)
+ pd.DataFrame(
+ [
+ {"sequence": "first"},
+ {"sequence": "second"},
+ {"sequence": "third"},
+ ]
+ ).to_parquet(parquet_dir / "batch_00000.parquet", index = False)
+
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ file_path, _, _ = build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "jsonl",
+ filename_stem = "ordered",
+ )
+ try:
+ lines = file_path.read_text(encoding = "utf-8").strip().splitlines()
+ sequences = [json.loads(line)["sequence"] for line in lines]
+ assert sequences == ["first", "second", "third"]
+ finally:
+ file_path.unlink(missing_ok = True)
+
+
+def test_build_dataset_download_exports_every_row_once_across_shards(tmp_path: Path, monkeypatch):
+ """Sorted shards and row-group iteration must emit every artifact row exactly once."""
+ pytest.importorskip("pyarrow")
+ pytest.importorskip("pandas")
+ import pandas as pd
+
+ dataset_path = tmp_path / "recipe-datasets" / "job-big"
+ parquet_dir = dataset_path / "parquet-files"
+ parquet_dir.mkdir(parents = True)
+ shard_size = 40_000
+ for shard, start in enumerate(range(0, 2 * shard_size, shard_size)):
+ pd.DataFrame({"i": range(start, start + shard_size)}).to_parquet(
+ parquet_dir / f"batch_{shard:05d}.parquet",
+ index = False,
+ row_group_size = 5_000,
+ )
+
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ file_path, _, _ = build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "jsonl",
+ filename_stem = "big",
+ )
+ try:
+ exported = [
+ json.loads(line)["i"] for line in file_path.read_text(encoding = "utf-8").splitlines()
+ ]
+ finally:
+ file_path.unlink(missing_ok = True)
+ assert exported == list(range(2 * shard_size))
+
+
+def test_build_dataset_download_leaves_no_temp_file_when_export_fails(tmp_path: Path, monkeypatch):
+ dataset_path = tmp_path / "recipe-datasets" / "job-fails"
+ _write_parquet_rows(dataset_path / "parquet-files", [{"col": "value"}])
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+ monkeypatch.setattr(
+ "core.data_recipe.export._write_jsonl_from_parquet",
+ lambda *args, **kwargs: (_ for _ in ()).throw(RuntimeError("export blew up")),
+ )
+
+ before = set(Path(tempfile.gettempdir()).glob("*.jsonl"))
+ with pytest.raises(RuntimeError):
+ build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "jsonl",
+ filename_stem = "leaky",
+ )
+ assert set(Path(tempfile.gettempdir()).glob("*.jsonl")) == before
+
+
+def test_build_dataset_download_parquet_zip_includes_images(tmp_path: Path, monkeypatch):
+ artifact_root = tmp_path / "recipe-datasets"
+ dataset_path = artifact_root / "job-images"
+ parquet_dir = dataset_path / "parquet-files"
+ images_dir = dataset_path / "images" / "nested"
+ _write_parquet_rows(parquet_dir, [{"image": "images/nested/pic.png"}])
+ images_dir.mkdir(parents = True, exist_ok = True)
+ (images_dir / "pic.png").write_bytes(b"png-bytes")
+
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ file_path, _, _ = build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "parquet",
+ filename_stem = "with-images",
+ )
+ try:
+ with zipfile.ZipFile(file_path) as archive:
+ names = archive.namelist()
+ image_bytes = archive.read("images/nested/pic.png")
+ assert "batch_00000.parquet" in names
+ assert "images/nested/pic.png" in names
+ assert image_bytes == b"png-bytes"
+ finally:
+ file_path.unlink(missing_ok = True)
+
+
+def test_build_dataset_download_parquet_zip_from_artifact(tmp_path: Path, monkeypatch):
+ artifact_root = tmp_path / "recipe-datasets"
+ dataset_path = artifact_root / "job-456"
+ parquet_dir = dataset_path / "parquet-files"
+ _write_parquet_rows(parquet_dir, [{"col": "value"}])
+
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ file_path, media_type, download_name = build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "parquet",
+ filename_stem = "parquet-run",
+ )
+ try:
+ assert media_type == "application/zip"
+ assert download_name == "parquet-run.parquet.zip"
+ with zipfile.ZipFile(file_path) as archive:
+ names = archive.namelist()
+ assert names == ["batch_00000.parquet"]
+ finally:
+ file_path.unlink(missing_ok = True)
+
+
+def test_build_dataset_download_missing_parquet_raises(tmp_path: Path, monkeypatch):
+ dataset_path = tmp_path / "empty-artifact"
+ dataset_path.mkdir()
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ with pytest.raises(RecipeDatasetExportError, match = "parquet"):
+ build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "jsonl",
+ filename_stem = "missing",
+ )
+
+
+def test_download_job_dataset_route_uses_artifact_path(monkeypatch, tmp_path: Path):
+ pytest.importorskip("fastapi")
+ from fastapi import BackgroundTasks
+
+ jobs_route = pytest.importorskip(
+ "routes.data_recipe.jobs",
+ reason = "studio backend routes unavailable",
+ )
+
+ captured: dict[str, str] = {}
+
+ def fake_build_dataset_download(**kwargs):
+ captured.update(kwargs)
+ jsonl_path = tmp_path / "out.jsonl"
+ jsonl_path.write_text('{"ok": true}\n', encoding = "utf-8")
+ return jsonl_path, "application/x-ndjson", "run.jsonl"
+
+ class _FakeManager:
+ def get_status(self, job_id: str):
+ return {
+ "status": "completed",
+ "artifact_path": "/tmp/artifacts/job-1",
+ }
+
+ monkeypatch.setattr(jobs_route, "build_dataset_download", fake_build_dataset_download)
+ monkeypatch.setattr(jobs_route, "get_job_manager", lambda: _FakeManager())
+ # These tests are about who may fetch the link, not about what the export is called.
+ monkeypatch.setattr(
+ jobs_route,
+ "download_filename",
+ lambda *, artifact_path, export_format, stem: f"{stem}.jsonl",
+ )
+
+ response = jobs_route.download_job_dataset(
+ "job-1",
+ background_tasks = BackgroundTasks(),
+ export_format = "jsonl",
+ artifact_path = None,
+ filename = "My Run",
+ )
+ assert captured["artifact_path"] == "/tmp/artifacts/job-1"
+ assert captured["filename_stem"] == "My Run"
+ assert response.filename == "run.jsonl"
+
+
+def test_download_job_dataset_route_rejects_incomplete_run(monkeypatch):
+ pytest.importorskip("fastapi")
+ from fastapi import BackgroundTasks
+
+ jobs_route = pytest.importorskip("routes.data_recipe.jobs")
+
+ class _FakeManager:
+ def get_status(self, job_id: str):
+ return {"status": "running", "artifact_path": "/tmp/artifacts/job-1"}
+
+ monkeypatch.setattr(jobs_route, "get_job_manager", lambda: _FakeManager())
+
+ with pytest.raises(HTTPException) as exc_info:
+ jobs_route.download_job_dataset(
+ "job-1",
+ background_tasks = BackgroundTasks(),
+ export_format = "jsonl",
+ artifact_path = None,
+ filename = None,
+ )
+ assert exc_info.value.status_code == 409
+
+
+def test_build_in_memory_job_dataset_download_pages_all_rows(monkeypatch, tmp_path: Path):
+ pytest.importorskip("fastapi")
+ from fastapi import BackgroundTasks
+
+ jobs_route = pytest.importorskip("routes.data_recipe.jobs")
+
+ class _FakeManager:
+ def __init__(self):
+ self.rows = [{"index": index} for index in range(12_500)]
+ self.calls: list[tuple[int, int]] = []
+
+ def get_status(self, job_id: str):
+ # A preview run: completed, but with nothing persisted to export from.
+ return {"status": "completed", "artifact_path": None}
+
+ def get_dataset(
+ self,
+ job_id: str,
+ *,
+ limit: int,
+ offset: int = 0,
+ ):
+ self.calls.append((limit, offset))
+ page = self.rows[offset : offset + limit]
+ return {"dataset": page, "total": len(self.rows)}
+
+ fake_manager = _FakeManager()
+ monkeypatch.setattr(jobs_route, "get_job_manager", lambda: fake_manager)
+
+ response = jobs_route.download_job_dataset(
+ "job-big",
+ background_tasks = BackgroundTasks(),
+ export_format = "jsonl",
+ artifact_path = None,
+ filename = "big-run",
+ )
+ # Called directly, so the response's background unlink never runs: take the file away here.
+ try:
+ assert response.filename == "big-run.jsonl"
+ assert fake_manager.calls == [
+ (jobs_route._IN_MEMORY_DOWNLOAD_PAGE_SIZE, 0),
+ (jobs_route._IN_MEMORY_DOWNLOAD_PAGE_SIZE, 10_000),
+ ]
+ lines = Path(response.path).read_text(encoding = "utf-8").strip().splitlines()
+ assert len(lines) == 12_500
+ assert json.loads(lines[-1]) == {"index": 12_499}
+ finally:
+ Path(response.path).unlink(missing_ok = True)
+
+
+def _download_app(monkeypatch, tmp_path: Path, jobs_route):
+ """The real /api/data-recipe mount, with an auth database of its own."""
+ from fastapi import FastAPI
+
+ from auth import storage
+
+ monkeypatch.setattr(storage, "DB_PATH", tmp_path / "auth.db")
+ monkeypatch.setattr(storage, "_BOOTSTRAP_PW_PATH", tmp_path / ".bootstrap_password")
+ monkeypatch.setattr(storage, "_bootstrap_password", None)
+ storage._reset_api_key_hash_cache()
+ storage.create_initial_user(
+ username = storage.DEFAULT_ADMIN_USERNAME,
+ password = "human-password-123",
+ jwt_secret = secrets.token_urlsafe(64),
+ )
+
+ from auth.authentication import create_access_token
+ from routes.data_recipe import router as data_recipe_router
+
+ def fake_build_dataset_download(**_kwargs):
+ jsonl_path = tmp_path / "out.jsonl"
+ jsonl_path.write_text('{"ok": true}\n', encoding = "utf-8")
+ return jsonl_path, "application/x-ndjson", "run.jsonl"
+
+ class _FakeManager:
+ def get_status(self, job_id: str):
+ return {"status": "completed", "artifact_path": "/tmp/artifacts/job-1"}
+
+ monkeypatch.setattr(jobs_route, "build_dataset_download", fake_build_dataset_download)
+ monkeypatch.setattr(jobs_route, "get_job_manager", lambda: _FakeManager())
+ # These tests are about who may fetch the link, not about what the export ends up called.
+ monkeypatch.setattr(
+ jobs_route,
+ "download_filename",
+ lambda *, artifact_path, export_format, stem: f"{stem}.jsonl",
+ )
+
+ app = FastAPI()
+ app.include_router(data_recipe_router, prefix = "/api/data-recipe")
+ return app, create_access_token(storage.DEFAULT_ADMIN_USERNAME)
+
+
+def test_download_link_is_minted_over_the_bearer_and_used_without_one(monkeypatch, tmp_path: Path):
+ """The URL is fetched without a header, so it carries a signed capability rather than the
+ session token, which download history would keep."""
+ pytest.importorskip("fastapi")
+ from fastapi.testclient import TestClient
+
+ jobs_route = pytest.importorskip("routes.data_recipe.jobs")
+ app, token = _download_app(monkeypatch, tmp_path, jobs_route)
+ client = TestClient(app)
+
+ minted = client.get(
+ "/api/data-recipe/jobs/job-1/download-url",
+ headers = {"Authorization": f"Bearer {token}"},
+ )
+ assert minted.status_code == 200
+ url = "/api/data-recipe" + minted.json()["path"]
+ assert token not in url
+
+ # No Authorization header at all, the way the browser fetches it.
+ assert client.get(url).status_code == 200
+ # And the header still works on its own, for an API client.
+ assert (
+ client.get(
+ "/api/data-recipe/jobs/job-1/download",
+ headers = {"Authorization": f"Bearer {token}"},
+ ).status_code
+ == 200
+ )
+
+
+def test_minting_a_download_link_needs_the_bearer(monkeypatch, tmp_path: Path):
+ pytest.importorskip("fastapi")
+ from fastapi.testclient import TestClient
+
+ jobs_route = pytest.importorskip("routes.data_recipe.jobs")
+ app, _token = _download_app(monkeypatch, tmp_path, jobs_route)
+
+ assert TestClient(app).get("/api/data-recipe/jobs/job-1/download-url").status_code == 401
+
+
+def test_download_route_refuses_an_unsigned_or_repointed_link(monkeypatch, tmp_path: Path):
+ pytest.importorskip("fastapi")
+ from fastapi.testclient import TestClient
+ from urllib.parse import parse_qs, urlparse
+
+ jobs_route = pytest.importorskip("routes.data_recipe.jobs")
+ app, token = _download_app(monkeypatch, tmp_path, jobs_route)
+ client = TestClient(app)
+
+ assert client.get("/api/data-recipe/jobs/job-1/download").status_code == 401
+ assert (
+ client.get(
+ "/api/data-recipe/jobs/job-1/download",
+ params = {"token": "not-a-real-token"},
+ ).status_code
+ == 401
+ )
+ # A session JWT is not a download link, however valid it is as a bearer.
+ assert (
+ client.get(
+ "/api/data-recipe/jobs/job-1/download",
+ params = {"token": token},
+ ).status_code
+ == 401
+ )
+
+ url = client.get(
+ "/api/data-recipe/jobs/job-1/download-url",
+ params = {"artifact_path": "/recipes/mine"},
+ headers = {"Authorization": f"Bearer {token}"},
+ ).json()["path"]
+ signed = parse_qs(urlparse(url).query)["token"][0]
+ # Every parameter the export reads is signed, so the artifact cannot be swapped for another.
+ assert (
+ client.get(
+ "/api/data-recipe/jobs/job-1/download",
+ params = {"artifact_path": "/recipes/someone-else", "token": signed},
+ ).status_code
+ == 401
+ )
+
+
+def test_download_link_expires(monkeypatch, tmp_path: Path):
+ pytest.importorskip("fastapi")
+ from fastapi.testclient import TestClient
+
+ jobs_route = pytest.importorskip("routes.data_recipe.jobs")
+ app, token = _download_app(monkeypatch, tmp_path, jobs_route)
+ client = TestClient(app)
+
+ url = (
+ "/api/data-recipe"
+ + client.get(
+ "/api/data-recipe/jobs/job-1/download-url",
+ headers = {"Authorization": f"Bearer {token}"},
+ ).json()["path"]
+ )
+ assert client.get(url).status_code == 200
+
+ real_time = jobs_route.time.time
+ monkeypatch.setattr(
+ jobs_route.time,
+ "time",
+ lambda: real_time() + jobs_route._DOWNLOAD_LINK_TTL + 1,
+ )
+ assert client.get(url).status_code == 401
+
+
+def _write_appledouble_companion(path: Path) -> None:
+ from utils.paths.path_utils import _MAGIC
+ path.write_bytes(_MAGIC + b"\x00" * 60)
+
+
+def test_build_dataset_download_ignores_appledouble_companions(tmp_path: Path, monkeypatch):
+ """AppleDouble companions are neither readable shards nor downloadable image files."""
+ dataset_path = tmp_path / "recipe-datasets" / "job-macos"
+ parquet_dir = dataset_path / "parquet-files"
+ _write_parquet_rows(parquet_dir, [{"i": 0}, {"i": 1}])
+ _write_appledouble_companion(parquet_dir / "._batch_00000.parquet")
+ images_dir = dataset_path / "images"
+ images_dir.mkdir(parents = True)
+ (images_dir / "pic.png").write_bytes(b"png-bytes")
+ _write_appledouble_companion(images_dir / "._pic.png")
+
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ from core.data_recipe.export import _write_jsonl_from_parquet
+
+ streamed = tmp_path / "streamed.jsonl"
+ _write_jsonl_from_parquet(
+ parquet_dir = parquet_dir,
+ destination = streamed,
+ )
+ assert [json.loads(line)["i"] for line in streamed.read_text().splitlines()] == [0, 1]
+
+ file_path, _, _ = build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "parquet",
+ filename_stem = "macos",
+ )
+ try:
+ with zipfile.ZipFile(file_path) as archive:
+ names = archive.namelist()
+ finally:
+ file_path.unlink(missing_ok = True)
+ assert names == ["batch_00000.parquet", "images/pic.png"]
+
+
+def test_build_dataset_download_keeps_a_real_file_named_like_a_companion(
+ tmp_path: Path, monkeypatch
+):
+ """The name alone does not decide it: a genuine shard called ._batch.parquet is still a shard."""
+ dataset_path = tmp_path / "recipe-datasets" / "job-dotunder"
+ parquet_dir = dataset_path / "parquet-files"
+ _write_parquet_rows(parquet_dir, [{"i": 0}])
+ (parquet_dir / "batch_00000.parquet").rename(parquet_dir / "._batch_00000.parquet")
+
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ file_path, _, _ = build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "jsonl",
+ filename_stem = "dotunder",
+ )
+ try:
+ assert [json.loads(line)["i"] for line in file_path.read_text().splitlines()] == [0]
+ finally:
+ file_path.unlink(missing_ok = True)
+
+
+def test_jsonl_reader_preserves_a_high_precision_decimal_number(tmp_path: Path, monkeypatch):
+ """A durable JSONL export must not round a parquet decimal through binary float."""
+ pyarrow = pytest.importorskip("pyarrow")
+ import pyarrow.parquet as pyarrow_parquet
+ from decimal import Decimal
+
+ from core.data_recipe.export import _write_jsonl_from_parquet
+
+ parquet_dir = tmp_path / "parquet-files"
+ parquet_dir.mkdir(parents = True)
+ expected = Decimal("0.1234567890123456789012345678")
+ pyarrow_parquet.write_table(
+ pyarrow.table({"price": pyarrow.array([expected], type = pyarrow.decimal128(28, 28))}),
+ parquet_dir / "batch_00000.parquet",
+ )
+
+ streamed = tmp_path / "streamed.jsonl"
+ _write_jsonl_from_parquet(
+ parquet_dir = parquet_dir,
+ destination = streamed,
+ )
+
+ assert json.loads(streamed.read_text().strip(), parse_float = Decimal) == {"price": expected}
+
+
+def test_to_jsonable_maps_pandas_missing_sentinels_to_none():
+ """NaT isoformat()s to "NaT" and NA hits the str() fallback as "<NA>", either of which writes
+ a real value where the dataset had none."""
+ pd = pytest.importorskip("pandas")
+ from core.data_recipe.jsonable import to_jsonable, to_preview_jsonable
+
+ for sentinel in (pd.NA, pd.NaT):
+ assert to_jsonable(sentinel) is None
+ assert to_preview_jsonable(sentinel) is None
+
+
+def test_build_dataset_download_writes_a_missing_timestamp_as_null(tmp_path: Path, monkeypatch):
+ pytest.importorskip("pyarrow")
+ pd = pytest.importorskip("pandas")
+
+ dataset_path = tmp_path / "recipe-datasets" / "job-nat"
+ parquet_dir = dataset_path / "parquet-files"
+ parquet_dir.mkdir(parents = True)
+ pd.DataFrame(
+ {
+ "seen_at": pd.to_datetime(["2020-01-01", None]),
+ "score": pd.array([1, None], dtype = "Int64"),
+ }
+ ).to_parquet(parquet_dir / "batch_00000.parquet", index = False)
+
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ file_path, _, _ = build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "jsonl",
+ filename_stem = "nat",
+ )
+ try:
+ rows = [json.loads(line) for line in file_path.read_text(encoding = "utf-8").splitlines()]
+ finally:
+ file_path.unlink(missing_ok = True)
+ assert rows[0]["seen_at"].startswith("2020-01-01")
+ assert rows[1] == {"seen_at": None, "score": None}
+
+
+def test_jsonl_export_ships_the_images_its_rows_reference(tmp_path: Path, monkeypatch):
+ """Rows carry relative image paths, so a bare JSONL loses every image."""
+ dataset_path = tmp_path / "recipe-datasets" / "job-multimodal"
+ _write_parquet_rows(dataset_path / "parquet-files", [{"image": "images/nested/pic.png"}])
+ nested = dataset_path / "images" / "nested"
+ nested.mkdir(parents = True)
+ (nested / "pic.png").write_bytes(b"png-bytes")
+
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ from core.data_recipe.export import download_filename
+
+ assert (
+ download_filename(artifact_path = str(dataset_path), export_format = "jsonl", stem = "run")
+ == "run.jsonl.zip"
+ )
+
+ file_path, media_type, download_name = build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "jsonl",
+ filename_stem = "run",
+ )
+ try:
+ assert media_type == "application/zip"
+ assert download_name == "run.jsonl.zip"
+ with zipfile.ZipFile(file_path) as archive:
+ names = sorted(archive.namelist())
+ rows = archive.read("run.jsonl").decode("utf-8")
+ assert archive.read("images/nested/pic.png") == b"png-bytes"
+ finally:
+ file_path.unlink(missing_ok = True)
+ assert names == ["images/nested/pic.png", "run.jsonl"]
+ assert json.loads(rows.strip()) == {"image": "images/nested/pic.png"}
+
+
+def test_jsonl_export_stays_a_plain_file_without_images(tmp_path: Path, monkeypatch):
+ dataset_path = tmp_path / "recipe-datasets" / "job-text-only"
+ _write_parquet_rows(dataset_path / "parquet-files", [{"text": "hello"}])
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ from core.data_recipe.export import download_filename
+
+ assert (
+ download_filename(artifact_path = str(dataset_path), export_format = "jsonl", stem = "run")
+ == "run.jsonl"
+ )
+ file_path, media_type, download_name = build_dataset_download(
+ artifact_path = str(dataset_path),
+ export_format = "jsonl",
+ filename_stem = "run",
+ )
+ try:
+ assert (media_type, download_name) == ("application/x-ndjson", "run.jsonl")
+ finally:
+ file_path.unlink(missing_ok = True)
+
+
+def test_jsonl_reader_preserves_columns_named_like_old_reader_helpers(tmp_path: Path):
+ from core.data_recipe.export import _write_jsonl_from_parquet
+
+ parquet_dir = tmp_path / "parquet-files"
+ _write_parquet_rows(parquet_dir, [{"filename": "a.png", "text": "x"}])
+
+ destination = tmp_path / "out.jsonl"
+ _write_jsonl_from_parquet(parquet_dir, destination)
+ assert json.loads(destination.read_text().strip()) == {"filename": "a.png", "text": "x"}
+
+
+def test_minting_refuses_a_run_whose_shards_are_gone(tmp_path: Path, monkeypatch):
+ """A historical run's artifact path comes from the client, and nothing confirmed it still held
+ anything: the link minted fine and the browser then failed invisibly against it."""
+ dataset_path = tmp_path / "recipe-datasets" / "job-swept"
+ (dataset_path / "parquet-files").mkdir(parents = True)
+ monkeypatch.setattr(
+ "core.data_recipe.export._resolve_recipe_artifact_path",
+ lambda artifact_path: dataset_path,
+ )
+
+ from core.data_recipe.export import download_filename
+
+ with pytest.raises(RecipeDatasetExportError, match = "parquet"):
+ download_filename(artifact_path = str(dataset_path), export_format = "jsonl", stem = "swept")
+
+
+def test_minting_is_refused_for_a_keyless_caller(monkeypatch, tmp_path: Path):
+ """The capability outlives the setting that admitted the caller, so a keyless request may not
+ mint one. Same refusal the signed gallery-video links make."""
+ pytest.importorskip("fastapi")
+ from fastapi.testclient import TestClient
+
+ jobs_route = pytest.importorskip("routes.data_recipe.jobs")
+ app, token = _download_app(monkeypatch, tmp_path, jobs_route)
+
+ app.dependency_overrides[jobs_route.request_admitted_without_credential] = lambda: True
+ refused = TestClient(app).get(
+ "/api/data-recipe/jobs/job-1/download-url",
+ headers = {"Authorization": f"Bearer {token}"},
+ )
+ assert refused.status_code == 403
+ app.dependency_overrides.clear()
+
+
+def test_download_link_outlasts_the_native_save_dialog():
+ """The chooser opens before the request, so a link expiring while it sits open 401s after the
+ destination was picked."""
+ jobs_route = pytest.importorskip("routes.data_recipe.jobs")
+
+ assert jobs_route._DOWNLOAD_LINK_TTL >= 15 * 60
+
+
+def test_jsonl_reader_streams_a_merged_shard_by_row_group(tmp_path: Path):
+ """merge_batches collapses a whole run into one file, so a shard is not a safe unit to read."""
+ pytest.importorskip("pandas")
+ pyarrow_parquet = pytest.importorskip("pyarrow.parquet")
+ import pandas as pd
+
+ from core.data_recipe.export import _JSONL_EXPORT_BATCH_ROWS, _write_jsonl_from_parquet
+
+ parquet_dir = tmp_path / "parquet-files"
+ parquet_dir.mkdir(parents = True)
+ rows = _JSONL_EXPORT_BATCH_ROWS * 2 + 5
+ pd.DataFrame({"i": range(rows)}).to_parquet(
+ parquet_dir / "batch_00000.parquet",
+ index = False,
+ row_group_size = 1_000,
+ )
+ assert pyarrow_parquet.ParquetFile(parquet_dir / "batch_00000.parquet").num_row_groups > 1
+
+ destination = tmp_path / "out.jsonl"
+ _write_jsonl_from_parquet(parquet_dir, destination)
+ exported = [json.loads(line)["i"] for line in destination.read_text().splitlines()]
+ assert exported == list(range(rows))
+
+
+def test_jsonl_reader_preserves_arrow_value_types(tmp_path: Path):
+ pyarrow = pytest.importorskip("pyarrow")
+ import pyarrow.parquet as pyarrow_parquet
+ from datetime import date, datetime
+ from decimal import Decimal
+
+ from core.data_recipe.export import _write_jsonl_from_parquet
+
+ parquet_dir = tmp_path / "parquet-files"
+ parquet_dir.mkdir(parents = True)
+ pyarrow_parquet.write_table(
+ pyarrow.table(
+ {
+ "i64": pyarrow.array([1, None], pyarrow.int64()),
+ "day": pyarrow.array([date(2020, 1, 1), None], pyarrow.date32()),
+ "at": pyarrow.array([datetime(2020, 1, 1, 12, 30), None], pyarrow.timestamp("us")),
+ "price": pyarrow.array([Decimal("1.20"), None], pyarrow.decimal128(10, 2)),
+ "tags": pyarrow.array([[1, 2], None], pyarrow.list_(pyarrow.int64())),
+ }
+ ),
+ parquet_dir / "batch_00000.parquet",
+ )
+
+ destination = tmp_path / "out.jsonl"
+ _write_jsonl_from_parquet(
+ parquet_dir = parquet_dir,
+ destination = destination,
+ )
+
+ first = json.loads(destination.read_text().splitlines()[0])
+ assert first["i64"] == 1
+ assert first["day"] == "2020-01-01"
+ assert first["price"] == 1.2
+
+
+def test_jsonl_reader_preserves_hugging_face_image_struct_bytes(tmp_path: Path):
+ """Image(decode=False) rows keep their reversible Arrow bytes/path representation."""
+ pyarrow = pytest.importorskip("pyarrow")
+ import base64
+ import io
+ import pyarrow.parquet as pyarrow_parquet
+ from PIL import Image
+
+ from core.data_recipe.export import _write_jsonl_from_parquet
+
+ buffer = io.BytesIO()
+ Image.new("RGBA", (2, 2), (10, 20, 30, 40)).save(buffer, format = "PNG")
+ original = buffer.getvalue()
+ image_type = pyarrow.struct([("bytes", pyarrow.binary()), ("path", pyarrow.string())])
+ parquet_dir = tmp_path / "parquet-files"
+ parquet_dir.mkdir(parents = True)
+ pyarrow_parquet.write_table(
+ pyarrow.table(
+ {
+ "image": pyarrow.array(
+ [{"bytes": original, "path": "images/source.png"}],
+ type = image_type,
+ ),
+ "label": pyarrow.array(["keep-me"]),
+ }
+ ),
+ parquet_dir / "batch_00000.parquet",
+ )
+
+ destination = tmp_path / "out.jsonl"
+ _write_jsonl_from_parquet(
+ parquet_dir = parquet_dir,
+ destination = destination,
+ )
+
+ row = json.loads(destination.read_text().strip())
+ assert row["label"] == "keep-me"
+ assert row["image"]["path"] == "images/source.png"
+ assert base64.b64decode(row["image"]["bytes"]) == original
+
+
+def test_a_row_is_not_mistaken_for_an_image(tmp_path: Path):
+ """to_preview_jsonable's Hugging Face image detection matches any mapping carrying `bytes` or
+ `path`, and a row is one. Passing whole rows in replaced every column, labels included, with a
+ single JPEG preview payload."""
+ pyarrow = pytest.importorskip("pyarrow")
+ import pyarrow.parquet as pyarrow_parquet
+ from PIL import Image
+
+ from core.data_recipe.export import _write_jsonl_from_parquet
+
+ picture = tmp_path / "pic.png"
+ Image.new("RGB", (8, 8), (255, 0, 0)).save(picture)
+
+ for columns in (
+ {"path": pyarrow.array([str(picture)]), "label": pyarrow.array(["keep-me"])},
+ {"bytes": pyarrow.array([picture.read_bytes()]), "label": pyarrow.array(["keep-me"])},
+ ):
+ parquet_dir = tmp_path / f"shard-{'path' if 'path' in columns else 'bytes'}"
+ parquet_dir.mkdir()
+ pyarrow_parquet.write_table(pyarrow.table(columns), parquet_dir / "batch_00000.parquet")
+
+ destination = tmp_path / "out.jsonl"
+ _write_jsonl_from_parquet(
+ parquet_dir = parquet_dir,
+ destination = destination,
+ )
+ row = json.loads(destination.read_text().splitlines()[0])
+ assert row["label"] == "keep-me"
+ assert sorted(row) == sorted(columns)
+
+
+def test_to_preview_jsonable_row_converts_each_column(tmp_path: Path):
+ from PIL import Image
+
+ from core.data_recipe.jsonable import to_preview_jsonable_row
+
+ picture = tmp_path / "pic.png"
+ Image.new("RGB", (4, 4), (0, 255, 0)).save(picture)
+
+ row = {"path": str(picture), "label": "keep-me"}
+ assert to_preview_jsonable_row(row) == row
+ assert to_preview_jsonable_row([row, row]) == [row, row]
+ # A column that really is an image dict is still rendered as a preview payload.
+ cell = to_preview_jsonable_row({"image": {"path": str(picture)}})["image"]
+ assert cell["type"] == "image" and cell["mime"] == "image/jpeg"
+
+
+def test_a_column_named_file_row_number_also_survives(tmp_path: Path):
+ from core.data_recipe.export import _write_jsonl_from_parquet
+
+ parquet_dir = tmp_path / "parquet-files"
+ _write_parquet_rows(parquet_dir, [{"file_row_number": 7, "filename": "a.png", "label": "x"}])
+ destination = tmp_path / "out.jsonl"
+ _write_jsonl_from_parquet(parquet_dir, destination)
+ assert json.loads(destination.read_text().strip()) == {
+ "file_row_number": 7,
+ "filename": "a.png",
+ "label": "x",
+ }
diff --git a/studio/frontend/src/features/recipe-studio/api/index.ts b/studio/frontend/src/features/recipe-studio/api/index.ts
index c99c21eca1b..2ab6afea4b5 100644
--- a/studio/frontend/src/features/recipe-studio/api/index.ts
+++ b/studio/frontend/src/features/recipe-studio/api/index.ts
@@ -2,6 +2,7 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { authFetch } from "@/features/auth";
+import { apiUrl } from "@/lib/api-base";
import {
formatFastApiDetail,
readFastApiError,
@@ -328,6 +329,37 @@ export async function getRecipeJobDataset(
);
}
+export type RecipeJobDownloadFormat = "jsonl" | "parquet";
+
+export async function downloadRecipeJobDataset(
+ jobId: string,
+ options?: {
+ format?: RecipeJobDownloadFormat;
+ artifactPath?: string | null;
+ filename?: string | null;
+ },
+): Promise<{ url: string; filename: string }> {
+ const params = new URLSearchParams();
+ params.set("format", options?.format ?? "jsonl");
+ if (options?.artifactPath) {
+ params.set("artifact_path", options.artifactPath);
+ }
+ if (options?.filename) {
+ params.set("filename", options.filename);
+ }
+ // Minted over authFetch: it refreshes an expired session and surfaces an unexportable run
+ // before the save dialog opens. The server names the file, since a JSONL is zipped only when
+ // the artifact has images.
+ const { path, filename } = await getJson<{ path: string; filename: string }>(
+ `/jobs/${jobId}/download-url?${params.toString()}`,
+ );
+ // The same base every other call here uses, so a repointed VITE_DATA_DESIGNER_API is honoured.
+ // Its trailing slash is dropped: authFetch survives the // via FastAPI's redirect, but the
+ // native downloader refuses every 3xx, and only after the save location has been chosen.
+ const base = DATA_DESIGNER_API_BASE.replace(/\/+$/, "");
+ return { url: apiUrl(`${base}${path}`), filename };
+}
+
export async function cancelRecipeJob(
jobId: string,
): Promise<JobStatusResponse> {
diff --git a/studio/frontend/src/features/recipe-studio/components/executions/download-execution-dataset.ts b/studio/frontend/src/features/recipe-studio/components/executions/download-execution-dataset.ts
new file mode 100644
index 00000000000..b621fd153c9
--- /dev/null
+++ b/studio/frontend/src/features/recipe-studio/components/executions/download-execution-dataset.ts
@@ -0,0 +1,77 @@
+// 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 { isTauri } from "@/lib/api-base";
+import {
+ downloadFile,
+ downloadUrlStreaming,
+ isDownloadCancelled,
+} from "@/lib/native-files";
+import { downloadRecipeJobDataset } from "../../api";
+import type { RecipeExecutionRecord } from "../../execution-types";
+import { hasCompleteLocalDataset } from "../../executions/execution-helpers";
+
+/** Whether the bytes are known to have landed: a browser anchor click resolves before the request
+ * is even sent, while the native downloader streams and rejects a non-2xx. "partial" is the rows
+ * this client still holds, written when the server no longer has the run. */
+export type DownloadOutcome = "saved" | "started" | "partial";
+
+function sanitizeFilenameStem(value: string): string {
+ const cleaned = value
+ .trim()
+ .replace(/[^\w.-]+/g, "-")
+ .replace(/^-+|-+$/g, "");
+ return cleaned || "recipe-dataset";
+}
+
+function buildDownloadFilename(execution: RecipeExecutionRecord): string {
+ const runName = execution.run_name?.trim();
+ if (runName) {
+ return sanitizeFilenameStem(runName);
+ }
+ return sanitizeFilenameStem(execution.id);
+}
+
+function triggerClientJsonlDownload(
+ rows: Record<string, unknown>[],
+ filenameStem: string,
+): Promise<void> {
+ const body =
+ rows.map((row) => JSON.stringify(row)).join("\n") + (rows.length > 0 ? "\n" : "");
+ return downloadFile(body, `${filenameStem}.jsonl`, "application/x-ndjson");
+}
+
+export async function downloadExecutionDataset(
+ execution: RecipeExecutionRecord,
+): Promise<DownloadOutcome> {
+ const filenameStem = buildDownloadFilename(execution);
+
+ // The backend export pages the whole dataset; the rows here are one page of it.
+ if (execution.jobId) {
+ try {
+ // A real authenticated request, so an unexportable run fails before anything is claimed.
+ const { url, filename } = await downloadRecipeJobDataset(execution.jobId, {
+ artifactPath: execution.artifact_path,
+ filename: filenameStem,
+ });
+ await downloadUrlStreaming(url, filename);
+ return isTauri ? "saved" : "started";
+ } catch (error) {
+ if (isDownloadCancelled(error)) {
+ throw error;
+ }
+ // An artifact-backed run must not fall through here: its images live beside the parquet,
+ // and a bare JSONL loses them.
+ if (execution.artifact_path) {
+ throw error;
+ }
+ }
+ }
+
+ if (execution.dataset.length === 0) {
+ throw new Error("This run does not have a dataset to download yet.");
+ }
+
+ await triggerClientJsonlDownload(execution.dataset, filenameStem);
+ return hasCompleteLocalDataset(execution) ? "saved" : "partial";
+}
diff --git a/studio/frontend/src/features/recipe-studio/components/executions/execution-overview-tab.tsx b/studio/frontend/src/features/recipe-studio/components/executions/execution-overview-tab.tsx
index 5ff9d644e7c..fd9d743e17c 100644
--- a/studio/frontend/src/features/recipe-studio/components/executions/execution-overview-tab.tsx
+++ b/studio/frontend/src/features/recipe-studio/components/executions/execution-overview-tab.tsx
@@ -10,7 +10,6 @@ import {
} from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { Badge } from "@/components/ui/badge";
-import { Button } from "@/components/ui/button";
import {
Table,
TableBody,
@@ -61,8 +60,6 @@ type ExecutionOverviewTabProps = {
terminalLines: string[];
terminalRef: RefObject<HTMLDivElement | null>;
onTerminalScroll: (event: UIEvent<HTMLDivElement>) => void;
- canPublish: boolean;
- onOpenPublish: () => void;
};
export function ExecutionOverviewTab({
@@ -80,8 +77,6 @@ export function ExecutionOverviewTab({
terminalLines,
terminalRef,
onTerminalScroll,
- canPublish,
- onOpenPublish,
}: ExecutionOverviewTabProps): ReactElement {
const sourceProgress = execution.source_progress;
@@ -89,19 +84,6 @@ export function ExecutionOverviewTab({
<div className="mt-3 space-y-3">
{showSummaryCards && (
<div className="space-y-3">
- {canPublish && (
- <div className="flex flex-col gap-3 rounded-xl border border-border/60 bg-card/55 p-3 sm:flex-row sm:items-center sm:justify-between">
- <div className="space-y-1">
- <p className="text-sm font-medium text-foreground">Next step</p>
- <p className="text-xs text-muted-foreground">
- This run is complete. Publish the generated dataset to Hugging Face.
- </p>
- </div>
- <Button type="button" variant="outline" size="sm" onClick={onOpenPublish}>
- Publish to Hugging Face
- </Button>
- </div>
- )}
<div className="grid gap-3 md:grid-cols-2">
<div className="h-full rounded-xl border border-border/60 bg-card/55 p-3">
<div className="mb-2 flex items-center justify-between">
diff --git a/studio/frontend/src/features/recipe-studio/components/executions/executions-view.tsx b/studio/frontend/src/features/recipe-studio/components/executions/executions-view.tsx
index 3488ac05954..77800a005a6 100644
--- a/studio/frontend/src/features/recipe-studio/components/executions/executions-view.tsx
+++ b/studio/frontend/src/features/recipe-studio/components/executions/executions-view.tsx
@@ -5,6 +5,7 @@ import { useEffect, useMemo, useRef, useState, type ReactElement } from "react";
import type { ColumnDef } from "@tanstack/react-table";
import {
CheckmarkCircle02Icon,
+ Download01Icon,
Flag02Icon,
Share08Icon,
} from "@hugeicons/core-free-icons";
@@ -12,6 +13,7 @@ import { HugeiconsIcon } from "@hugeicons/react";
import { publishRecipeJob } from "../../api";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
+import { toastError, toastSuccess } from "@/shared/toast";
import { Progress } from "@/components/ui/progress";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { cn } from "@/lib/utils";
@@ -25,6 +27,10 @@ import { ExecutionDataTab } from "./execution-data-tab";
import { ExecutionOverviewTab } from "./execution-overview-tab";
import { ExecutionRawTab } from "./execution-raw-tab";
import { ExecutionSidebar } from "./execution-sidebar";
+import {
+ type DownloadOutcome,
+ downloadExecutionDataset,
+} from "./download-execution-dataset";
import { PublishExecutionDialog } from "./publish-execution-dialog";
import {
PREVIEW_DATASET_PAGE_SIZE,
@@ -46,6 +52,13 @@ type ExecutionsViewProps = {
onLoadDatasetPage: (id: string, page: number) => void;
};
+function downloadOutcomeMessage(outcome: DownloadOutcome): string {
+ if (outcome === "saved") return "Dataset downloaded";
+ if (outcome === "started") return "Dataset download started";
+ // The server no longer has this run, so what was written is whatever this client still holds.
+ return "Downloaded the rows still loaded for this run";
+}
+
export function ExecutionsView({
executions,
selectedExecutionId,
@@ -66,6 +79,7 @@ export function ExecutionsView({
Record<string, number>
>({});
const [publishDialogOpen, setPublishDialogOpen] = useState(false);
+ const [downloadingDataset, setDownloadingDataset] = useState(false);
const terminalRef = useRef<HTMLDivElement | null>(null);
const shouldStickTerminalToBottomRef = useRef(true);
const selectedExecution = useMemo(
@@ -201,6 +215,12 @@ export function ExecutionsView({
selectedExecution.jobId &&
selectedExecution.artifact_path,
);
+ const canDownload = Boolean(
+ selectedExecution &&
+ selectedExecution.status === "completed" &&
+ ((selectedExecution.kind === "full" && selectedExecution.jobId) ||
+ selectedExecution.dataset.length > 0),
+ );
const datasetPage = selectedExecution?.datasetPage ?? 1;
const datasetPageSize = selectedExecution?.datasetPageSize ?? 20;
const datasetTotal = selectedExecution?.datasetTotal ?? 0;
@@ -242,7 +262,10 @@ export function ExecutionsView({
if (typeof selectedExecution.analysis?.num_records === "number") {
return selectedExecution.analysis.num_records;
}
- if (selectedExecution.datasetTotal > 0) {
+ if (
+ typeof selectedExecution.datasetTotal === "number" &&
+ selectedExecution.datasetTotal > 0
+ ) {
return selectedExecution.datasetTotal;
}
if (selectedExecution.dataset.length > 0) {
@@ -449,14 +472,47 @@ export function ExecutionsView({
)}
<Tabs value={detailTab} onValueChange={setDetailTab}>
- <div className="flex items-center justify-between gap-2">
+ <div className="flex flex-wrap items-center justify-between gap-2">
<TabsList className="border border-border/60 bg-card/40">
<TabsTrigger value="data">Data</TabsTrigger>
<TabsTrigger value="overview">Overview</TabsTrigger>
<TabsTrigger value="columns">Columns</TabsTrigger>
<TabsTrigger value="raw">Raw</TabsTrigger>
</TabsList>
- <div className="flex items-center gap-2">
+ <div className="flex flex-wrap items-center gap-2">
+ {canDownload && (
+ <Button
+ type="button"
+ size="sm"
+ variant="outline"
+ disabled={downloadingDataset}
+ aria-label={downloadingDataset ? "Downloading dataset" : "Download dataset"}
+ title="Download dataset as JSONL"
+ onClick={() => {
+ if (!selectedExecution) {
+ return;
+ }
+ setDownloadingDataset(true);
+ downloadExecutionDataset(selectedExecution)
+ .then((outcome) => {
+ toastSuccess(downloadOutcomeMessage(outcome));
+ })
+ .catch((error: unknown) => {
+ const message =
+ error instanceof Error
+ ? error.message
+ : "Could not download this dataset.";
+ toastError("Download failed", message);
+ })
+ .finally(() => {
+ setDownloadingDataset(false);
+ });
+ }}
+ >
+ <HugeiconsIcon icon={Download01Icon} className="size-4" />
+ {downloadingDataset ? "Downloading..." : "Download"}
+ </Button>
+ )}
{canPublish && (
<Button
type="button"
@@ -495,8 +551,6 @@ export function ExecutionsView({
modelUsageRows={modelUsageRows}
terminalLines={terminalLines}
terminalRef={terminalRef}
- canPublish={canPublish}
- onOpenPublish={() => setPublishDialogOpen(true)}
onTerminalScroll={(event) => {
const element = event.currentTarget;
const distanceFromBottom =
diff --git a/studio/frontend/src/features/recipe-studio/components/executions/publish-execution-dialog.tsx b/studio/frontend/src/features/recipe-studio/components/executions/publish-execution-dialog.tsx
index bd7a80df50b..e665edbcd7f 100644
--- a/studio/frontend/src/features/recipe-studio/components/executions/publish-execution-dialog.tsx
+++ b/studio/frontend/src/features/recipe-studio/components/executions/publish-execution-dialog.tsx
@@ -47,7 +47,7 @@ function getExecutionRecordCount(execution: RecipeExecutionRecord | null): numbe
if (typeof execution.analysis?.num_records === "number") {
return execution.analysis.num_records;
}
- if (execution.datasetTotal > 0) {
+ if (typeof execution.datasetTotal === "number" && execution.datasetTotal > 0) {
return execution.datasetTotal;
}
if (execution.rows > 0) {
diff --git a/studio/frontend/src/features/recipe-studio/execution-types.ts b/studio/frontend/src/features/recipe-studio/execution-types.ts
index 92dd88c4f51..f613b574c86 100644
--- a/studio/frontend/src/features/recipe-studio/execution-types.ts
+++ b/studio/frontend/src/features/recipe-studio/execution-types.ts
@@ -97,7 +97,7 @@ export type RecipeExecutionRecord = {
// biome-ignore lint/style/useNamingConvention: backend schema
log_lines: string[];
dataset: Record<string, unknown>[];
- datasetTotal: number;
+ datasetTotal: number | null;
datasetPage: number;
datasetPageSize: number;
analysis: RecipeExecutionAnalysis | null;
diff --git a/studio/frontend/src/features/recipe-studio/executions/execution-helpers.ts b/studio/frontend/src/features/recipe-studio/executions/execution-helpers.ts
index d845a0360c4..efa7ef3a5b2 100644
--- a/studio/frontend/src/features/recipe-studio/executions/execution-helpers.ts
+++ b/studio/frontend/src/features/recipe-studio/executions/execution-helpers.ts
@@ -122,6 +122,14 @@ export function sortExecutions(records: RecipeExecutionRecord[]): RecipeExecutio
return next;
}
+/** A cached page is complete only when the producer recorded the full row count. */
+export function hasCompleteLocalDataset(execution: RecipeExecutionRecord): boolean {
+ return (
+ typeof execution.datasetTotal === "number" &&
+ execution.dataset.length >= execution.datasetTotal
+ );
+}
+
export function withExecutionDefaults(
record: RecipeExecutionRecord,
): RecipeExecutionRecord {
@@ -140,7 +148,7 @@ export function withExecutionDefaults(
const datasetTotal =
typeof record.datasetTotal === "number" && record.datasetTotal >= 0
? record.datasetTotal
- : dataset.length;
+ : null;
return {
...record,
diff --git a/studio/frontend/src/features/recipe-studio/executions/tracker.ts b/studio/frontend/src/features/recipe-studio/executions/tracker.ts
index 272d07145a4..9666e2681fe 100644
--- a/studio/frontend/src/features/recipe-studio/executions/tracker.ts
+++ b/studio/frontend/src/features/recipe-studio/executions/tracker.ts
@@ -270,6 +270,7 @@ export async function trackRecipeExecution({
completedPayload.processor_artifacts !== null
? (completedPayload.processor_artifacts as Record<string, unknown>)
: null;
+ const eventDatasetRows = Array.isArray(eventDataset) ? eventDataset : [];
const shouldFetchPreviewDataset =
kind === "preview" && !Array.isArray(eventDataset);
const shouldFetchAnalysis =
@@ -284,7 +285,11 @@ export async function trackRecipeExecution({
: Promise.resolve(eventAnalysis),
shouldFetchPreviewDataset || kind === "full"
? getRecipeJobDataset(jobId, { limit: DATASET_PAGE_SIZE, offset: 0 })
- : Promise.resolve({ dataset: eventDataset ?? [], total: rows }),
+ : // The event carries every record produced, which is not always the number asked for.
+ Promise.resolve({
+ dataset: eventDatasetRows,
+ total: eventDatasetRows.length,
+ }),
]);
const analysis =
diff --git a/studio/frontend/tests/recipe-execution-dataset-completeness.test.ts b/studio/frontend/tests/recipe-execution-dataset-completeness.test.ts
new file mode 100644
index 00000000000..d1fed1a8c6f
--- /dev/null
+++ b/studio/frontend/tests/recipe-execution-dataset-completeness.test.ts
@@ -0,0 +1,32 @@
+// 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 type { RecipeExecutionRecord } from "../src/features/recipe-studio/execution-types.ts";
+import {
+ hasCompleteLocalDataset,
+ withExecutionDefaults,
+} from "../src/features/recipe-studio/executions/execution-helpers.ts";
+
+test("a legacy cached page without a recorded total remains partial", () => {
+ const legacy = withExecutionDefaults({
+ id: "legacy-run",
+ dataset: Array.from({ length: 20 }, (_, index) => ({ index })),
+ } as unknown as RecipeExecutionRecord);
+
+ assert.equal(legacy.datasetTotal, null);
+ assert.equal(hasCompleteLocalDataset(legacy), false);
+});
+
+test("a cached dataset is complete when its recorded total is present", () => {
+ const current = withExecutionDefaults({
+ id: "current-run",
+ dataset: [{ index: 0 }, { index: 1 }],
+ datasetTotal: 2,
+ } as unknown as RecipeExecutionRecord);
+
+ assert.equal(current.datasetTotal, 2);
+ assert.equal(hasCompleteLocalDataset(current), true);
+});