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 dataset preview: hand a JSON file that is not JSON Lines to the loader without reading it whole (#17277)

Daniel Hand2a69e49ea2c
diff --git a/studio/backend/hub/services/datasets/local.py b/studio/backend/hub/services/datasets/local.py
index debc882c73f..353a869415e 100644
--- a/studio/backend/hub/services/datasets/local.py
+++ b/studio/backend/hub/services/datasets/local.py
@@ -206,10 +206,22 @@ def _build_local_dataset_items() -> list[LocalDatasetItem]:
_JSONL_SCHEMA_BYTES = 10 << 20
+def _first_significant_byte(f) -> bytes:
+ """First byte that ``bytes.strip`` would keep, or ``b""`` for an empty / all-whitespace file."""
+ while chunk := f.read(1 << 16):
+ if stripped := chunk.lstrip():
+ return stripped[:1]
+ return b""
+
+
def _jsonl_head(path: Path, preview_size: int) -> tuple[list[dict], bytes] | None:
"""First ``preview_size`` JSON Lines objects and the bytes of the loader's first batch. ``None`` if not JSON Lines."""
rows, keys, window, scanned = [], {}, [], 0
with path.open("rb") as f:
+ # Only an object decodes to a dict: a minified JSON array is one huge line, so don't read or parse it here.
+ if preview_size > 0 and _first_significant_byte(f) not in (b"", b"{"):
+ return None
+ f.seek(0)
for line in f:
if line.strip():
try:
diff --git a/studio/backend/tests/test_jsonl_preview_head.py b/studio/backend/tests/test_jsonl_preview_head.py
index 8b3b518d2ac..762aa9d069b 100644
--- a/studio/backend/tests/test_jsonl_preview_head.py
+++ b/studio/backend/tests/test_jsonl_preview_head.py
@@ -158,3 +158,15 @@ def test_a_file_that_is_not_json_lines_keeps_the_loader(tmp_path, loader_calls,
dataset, _ = local._stream_file_preview_slice(path, 10)
assert loader_calls == ["json"]
assert dataset["text"] == ["a", "b"]
+
+
+def test_a_minified_json_array_is_handed_to_the_loader_unread(tmp_path, monkeypatch):
+ path = tmp_path / "rows.json"
+ path.write_text(" \n" + json.dumps([{"a": i} for i in range(1000)]), encoding = "utf-8")
+ parsed = []
+ real_loads = json.loads
+ monkeypatch.setattr(
+ local.json, "loads", lambda s, *a, **k: parsed.append(len(s)) or real_loads(s, *a, **k)
+ )
+ assert local._jsonl_head(path, 10) is None
+ assert parsed == []