Skip to content

Studio: train structured recipe columns as JSON, not as Python dicts - #13635

Merged
Lyxot merged 3 commits into
unslothai:mainfrom
NilayYadav:recipe-structured-cells-json
Oct 14, 2026
Merged

Lyxot merged 3 commits into
unslothai:mainfrom
NilayYadav:recipe-structured-cells-json

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

Training on a Data Recipe run whose answer column is LLM Structured (the GitHub Crawler and Structured Outputs recipes, for example) trained each answer as a Python dict, with single quotes, None and True, even though the recipe produced JSON and the dataset preview shows JSON. The fine-tuned model then learns to reply in Python syntax that no JSON parser accepts. The same happened to any dataset with a nested object or list column mapped to a chat role.

Dict and list cells are now written into the conversation as JSON, the same way the template mapping path already writes them. Plain text, numbers, blank cells and SQuAD-style answer columns train exactly as before.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

1 similar comment
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-14T07:03:24.767420Z a464713 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7df2f61368

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/utils/datasets/cells.py Resolved
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

1 similar comment
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0b29bc85b2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/utils/datasets/cells.py Resolved

@NilayYadav NilayYadav left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review + reproduction evidence

Pinned review: 0b29bc85b2d20aecdd2e8d7206faef6faa58c282. 1 finding, fixed in 0b29bc85b2 (NaN and infinity inside a structured cell were written as NaN / Infinity, which strict JSON parsers reject; they now train as null, with a test).

I reproduced the user flow on fresh upstream main (NVIDIA L4, Linux, Chromium 1440x900): Train page, unsloth/Qwen3-0.6B, QLoRA, upload a JSONL whose agent_reply column is an object (the shape an LLM Structured recipe column produces), keep the detected mapping (question to User, agent_reply to Assistant), 60 steps at 2e-4, then load the fine-tuned LoRA in Chat and send a training ticket.

  • main 03022a4 / fix reverted (Linux, macOS, Windows): the dataset preview shows JSON, but the model answers {'response': 'Lower the batch size to 1 and enable gradient checkpointing.', 'next_action': 'retry_with_smaller_batch', 'escalate': False, 'notes': None}. json.loads on that reply fails with Expecting property name enclosed in double quotes: line 1 column 2 (char 1). The second ticket gives the same Python syntax. In CI, the PR's tests fail with the same JSONDecodeError (4 failed).
  • PR head (Linux, macOS, Windows): the same run answers {"response": "Lower the batch size to 1 and enable gradient checkpointing.", "next_action": "retry_with_smaller_batch", "escalate": true, "notes": null}, which parses. The second ticket also parses. In CI: 7 passed, and 191 adjacent dataset-formatting tests passed on every OS.

PR 13635 before/after

  • Same results elsewhere: the structured run ends at loss 0.0177 on main and 0.0184 on the PR, both 60/60 steps. A plain-text question/answer dataset trains the same on both, at 60/60 steps and final loss 0.0006.
  • Unchanged paths: plain text, numbers, blank and NaN cells, SQuAD answer dicts, message-list columns and tool-call turns produce the same output as before. Only dict and list cells change. Every mapping path is covered (auto, manual, custom, Alpaca, Alpaca to ChatML), and the template mapping path already wrote JSON. No cached formatted dataset carries the old text over after an update.
  • Speed: formatting a 50k-row dataset whose answer is a struct takes 0.19 s (0.12 s before). Formatting text-only data is unchanged (0.07 s). This is one-time preprocessing, not per step.

Conclusion: the bug is real and the PR fixes it. A model fine-tuned on a structured column now answers in JSON instead of Python syntax. Two small follow-ups, both already on main and outside this PR: image datasets with a structured text column, and _extract_column_value, which joins a list of dicts with , and has no default=str.

…ell_text

The template mapping path kept its own json.dumps for dicts, which wrote NaN as a bare NaN token and raised on dates, and joined a list of objects element by element into text no JSON parser accepts. It now serialises dicts and any list holding a dict or list through cell_text, so every mapping path trains the same JSON. Lists of scalar labels keep their comma join.
@Lyxot

Lyxot commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Pushed a464713 on top of the PR: the template mapping path (__system_prompt / __label_mapping, and Alpaca custom mapping) now serialises structured cells through cell_text instead of its own json.dumps.

  • Before, that path wrote NaN as a bare NaN, raised TypeError on a date inside a dict, and joined a list of objects element by element into {"a": 1}, {"a": 2}, which no JSON parser accepts. It now trains the same JSON as the chat-role mapping paths ([{"a": 1}, {"a": 2}], NaN as null).
  • Lists of scalar labels keep their comma join ([1, 2] with a label mapping still trains anger, fear).
  • test_structured_cells_as_json.py now runs every case through auto, user and template mapping, and adds a list cell whose object is not the first item.

@Lyxot

Lyxot commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: a464713f61

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Lyxot

Lyxot commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Codex converged on a464713f61: #13635 (comment)

What changed in this round

  • a464713: the template mapping path serialises dicts, and lists holding a dict or list, through cell_text, so every mapping path trains the same JSON (details in the comment above).
  • Replied to and resolved both earlier Codex threads: NaN/Infinity was already fixed in 0b29bc8; tuples were not changed, because no dataset Unsloth Studio loads delivers tuples (reasoning in the thread).

CI (staging copy, same head and base, since upstream jobs were queued): Lyxot/unsloth#158, 34 passed, 7 failed, 2 skipped. All 7 failures come from this branch's base (6a70df3, 43 commits behind main) and pass or are gone on current main; none touch dataset formatting:

The backend tests, including test_structured_cells_as_json.py, test_chat_mapping_squad_answers.py and test_alpaca_ai_assist_mapping.py, passed in "Repo tests (CPU, studio)".

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Structured dict/list cells now train as JSON on every mapping path; Codex converged on a464713, and the PR merged into current main passes the dataset formatting tests.

@Lyxot
Lyxot merged commit ca725cf into unslothai:main Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants