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: train structured recipe columns as JSON, not as Python dicts (#13635)

Nilayca725cf55d3e
diff --git a/studio/backend/tests/test_chat_mapping_squad_answers.py b/studio/backend/tests/test_chat_mapping_squad_answers.py
index bdba6bbdc39..ec81df0a57d 100644
--- a/studio/backend/tests/test_chat_mapping_squad_answers.py
+++ b/studio/backend/tests/test_chat_mapping_squad_answers.py
@@ -57,7 +57,7 @@ def test_cell_text_reads_the_text_of_an_answer_dict():
assert cell_text({"text": ["first", "second"], "answer_start": [1, 9]}) == "first"
# squad_v2 marks an unanswerable question with no answer.
assert cell_text({"text": [], "answer_start": []}) == ""
- assert cell_text({"label": 1}) == "{'label': 1}"
+ assert cell_text({"label": 1}) == '{"label": 1}'
assert cell_text({"label": ["A", "B"], "text": ["x", "y"]}) == (
- "{'label': ['A', 'B'], 'text': ['x', 'y']}"
+ '{"label": ["A", "B"], "text": ["x", "y"]}'
)
diff --git a/studio/backend/tests/test_structured_cells_as_json.py b/studio/backend/tests/test_structured_cells_as_json.py
new file mode 100644
index 00000000000..893677b085d
--- /dev/null
+++ b/studio/backend/tests/test_structured_cells_as_json.py
@@ -0,0 +1,55 @@
+# 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 datetime
+import json
+
+import pytest
+from datasets import Dataset
+
+from utils.datasets import format_dataset
+
+_REPLY = {
+ "response": "Lower the batch size; l'OOM vient de là.",
+ "followups": [],
+ "cites": None,
+ "resolved": True,
+}
+_MAPPINGS = pytest.mark.parametrize(
+ "mapping",
+ [
+ None,
+ {"User": "user", "Assistant": "assistant"},
+ {"User": "user", "Assistant": "assistant", "__system_prompt": "Be brief."},
+ ],
+ ids = ["auto_mapping", "user_mapping", "template_mapping"],
+)
+
+
+def _reply(cell, mapping):
+ row = Dataset.from_dict({"User": ["Training runs out of memory."], "Assistant": [cell]})
+ reply = format_dataset(row, custom_format_mapping = mapping)["dataset"][0]["conversations"][-1]
+ assert reply["role"] == "assistant"
+ return reply["content"]
+
+
+@_MAPPINGS
+def test_structured_cell_trains_as_json(mapping):
+ content = _reply(_REPLY, mapping)
+ assert json.loads(content) == _REPLY
+ assert "l'OOM vient de là" in content
+
+
+@_MAPPINGS
+def test_non_finite_numbers_in_a_structured_cell_train_as_null(mapping):
+ def reject(constant):
+ raise ValueError(constant)
+
+ content = _reply({"score": float("nan"), "top": [float("inf"), 1.5]}, mapping)
+ assert json.loads(content, parse_constant = reject) == {"score": None, "top": [None, 1.5]}
+
+
+@_MAPPINGS
+def test_list_cell_with_a_date_trains_as_json(mapping):
+ cell = [None, {"at": datetime.date(2024, 1, 2)}]
+ assert _reply(cell, mapping) == '[null, {"at": "2024-01-02"}]'
diff --git a/studio/backend/utils/datasets/cells.py b/studio/backend/utils/datasets/cells.py
index 894199d221c..7afbfbb1ccd 100644
--- a/studio/backend/utils/datasets/cells.py
+++ b/studio/backend/utils/datasets/cells.py
@@ -3,6 +3,8 @@
"""Turning raw dataset cells into the text we are willing to train on."""
+import json
+import math
from pathlib import Path
# Feature ids tagging each column of a CSV read by `csv_as_text_kwargs`.
@@ -17,6 +19,16 @@ _NA_CELLS = frozenset(
)
+def _finite(value):
+ if isinstance(value, dict):
+ return {key: _finite(item) for key, item in value.items()}
+ if isinstance(value, list):
+ return [_finite(item) for item in value]
+ if isinstance(value, float) and not math.isfinite(value):
+ return None
+ return value
+
+
def cell_text(value):
"""The text to train on for a single Alpaca-style dataset cell.
@@ -25,7 +37,7 @@ def cell_text(value):
arrives as 1.0. Untreated, the first trains the word None and the second
raises on .strip(). NaN counts as blank too: Arrow normalises a blank
numeric cell to null, but a dataset built straight from pandas need not have
- passed through Arrow.
+ passed through Arrow. A dict or list cell trains as JSON, not its Python repr.
"""
if value is None:
return ""
@@ -39,6 +51,8 @@ def cell_text(value):
return value
if isinstance(value, float) and value != value:
return ""
+ if isinstance(value, (dict, list)):
+ return json.dumps(_finite(value), ensure_ascii = False, default = str)
return str(value)
diff --git a/studio/backend/utils/datasets/dataset_utils.py b/studio/backend/utils/datasets/dataset_utils.py
index 490881a049f..9745ced5af2 100644
--- a/studio/backend/utils/datasets/dataset_utils.py
+++ b/studio/backend/utils/datasets/dataset_utils.py
@@ -3,8 +3,6 @@
"""Dataset utilities for format detection, conversion, and template application. Entry points: check_dataset_format (lightweight check whether manual mapping is needed, for the frontend), format_dataset (detects and normalizes dataset formats) and format_and_template_dataset (end-to-end processing with chat template). Internal utilities live in format_detection, format_conversion, chat_templates, vlm_processing, data_collators and model_mappings."""
-import json
-
from .format_detection import (
detect_dataset_format,
detect_multimodal_dataset,
@@ -233,9 +231,11 @@ def _apply_user_mapping(
def _extract_column_value(val, col: str, label_mapping: dict) -> str:
if isinstance(val, dict):
if "text" not in val:
- return json.dumps(val, ensure_ascii = False)
+ return cell_text(val)
inner = val["text"]
val = inner[0] if isinstance(inner, list) and inner else inner
+ if isinstance(val, list) and any(isinstance(item, (dict, list)) for item in val):
+ return cell_text(val)
names = label_mapping.get(col)
if not isinstance(names, dict):