Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: train a dataset's system column as the system prompt (#12614)
Daniel Han0599726f6f4f
diff --git a/studio/backend/tests/test_dataset_system_column.py b/studio/backend/tests/test_dataset_system_column.pynew file mode 100644index 00000000000..6c5b890c880--- /dev/null+++ b/studio/backend/tests/test_dataset_system_column.py@@ -0,0 +1,110 @@+# 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 pytest+from datasets import Dataset++from utils.datasets import format_and_template_dataset+++class _TurnTokenizer:+ chat_template = "{{ messages }}"+ eos_token = "</s>"++ def apply_chat_template(self, conversation, **_kwargs):+ return "".join(f"<{turn['role']}>{turn['content']}" for turn in conversation)+++def _sharegpt(turns):+ return "conversations", [{"from": role, "value": content} for role, content in turns]+++def _chatml(turns):+ return "messages", [{"role": role, "content": content} for role, content in turns]+++def _format(rows):+ result = format_and_template_dataset(+ Dataset.from_list(rows), model_name = "stub-model", tokenizer = _TurnTokenizer()+ )+ assert result["success"], result["errors"]+ return list(result["dataset"]["text"])++[email protected]("shape", [_sharegpt, _chatml], ids = ["sharegpt", "chatml"])+def test_system_column_is_trained_as_the_system_turn(shape):+ column, convo = shape([("user", "q"), ("assistant", "a")])++ texts = _format([{"system": "Here is a list of functions", column: convo}])++ assert texts == ["<system>Here is a list of functions<user>q<assistant>a"]++[email protected]("shape", [_sharegpt, _chatml], ids = ["sharegpt", "chatml"])+def test_system_column_does_not_override_a_leading_system_turn_or_add_a_blank_one(shape):+ column, with_system = shape([("system", "inline"), ("user", "q"), ("assistant", "a")])+ _, without_system = shape([("user", "q"), ("assistant", "a")])++ texts = _format(+ [+ {"system": "column", column: with_system},+ {"system": " ", column: without_system},+ ]+ )++ assert texts == ["<system>inline<user>q<assistant>a", "<user>q<assistant>a"]+++class _NoSystemTokenizer(_TurnTokenizer):+ def apply_chat_template(self, conversation, **kwargs):+ if any(turn["role"] == "system" for turn in conversation):+ raise ValueError("System role not supported")+ return super().apply_chat_template(conversation, **kwargs)++[email protected]("shape", [_sharegpt, _chatml], ids = ["sharegpt", "chatml"])+def test_system_column_falls_back_when_the_template_rejects_a_system_turn(shape):+ column, convo = shape([("user", "q"), ("assistant", "a")])++ result = format_and_template_dataset(+ Dataset.from_list([{"system": "Here is a list of functions", column: convo}]),+ model_name = "stub-model",+ tokenizer = _NoSystemTokenizer(),+ )++ assert result["success"], result["errors"]+ assert list(result["dataset"]["text"]) == ["<user>q<assistant>a"]+++def test_a_malformed_row_is_dropped_on_its_own_when_a_system_column_is_present():+ _, convo = _chatml([("user", "q"), ("assistant", "a")])++ texts = _format(+ [+ {"system": "column", "messages": convo},+ {"system": "column", "messages": [None, *convo]},+ ]+ )++ assert texts == ["<system>column<user>q<assistant>a"]++[email protected]("shape", [_sharegpt, _chatml], ids = ["sharegpt", "chatml"])+def test_system_column_is_kept_when_converting_to_alpaca(shape):+ column, convo = shape([("user", "q"), ("assistant", "a")])+ _, with_system = shape([("system", "inline"), ("user", "q"), ("assistant", "a")])++ result = format_and_template_dataset(+ Dataset.from_list(+ [+ {"system": "Here is a list of functions", column: convo},+ {"system": "column", column: with_system},+ ]+ ),+ model_name = "stub-model",+ tokenizer = _TurnTokenizer(),+ format_type = "alpaca",+ )++ assert result["success"], result["errors"]+ assert list(result["dataset"]["input"]) == ["Here is a list of functions", "inline"]diff --git a/studio/backend/utils/datasets/chat_templates.py b/studio/backend/utils/datasets/chat_templates.pyindex 741a0b7d47c..963a0b90f5a 100644--- a/studio/backend/utils/datasets/chat_templates.py+++ b/studio/backend/utils/datasets/chat_templates.py@@ -297,6 +297,18 @@ def get_dataset_info_summary(dataset_info):}+def _with_system_turn(convo, system):+ if (+ isinstance(system, str)+ and system.strip()+ and convo+ and isinstance(convo[0], dict)+ and convo[0].get("role") != "system"+ ):+ return [{"role": "system", "content": system}, *convo]+ return convo++def apply_chat_template_to_dataset(dataset_info,tokenizer,@@ -516,12 +528,20 @@ def apply_chat_template_to_dataset(def _format_chatml(examples):convos = examples[chat_column]+ systems = examples.get("system") or [None] * len(convos)texts = []row_errors = []- for convo in convos:+ for convo, system in zip(convos, systems):try:- text = _render_conversation(tokenizer, convo)+ with_system = _with_system_turn(convo, system)+ try:+ text = _render_conversation(tokenizer, with_system)+ except Exception:+ # A template without a system role still trains the conversation.+ if with_system is convo:+ raise+ text = _render_conversation(tokenizer, convo)if remove_bos_prefix:text = text.removeprefix('<bos>')diff --git a/studio/backend/utils/datasets/format_conversion.py b/studio/backend/utils/datasets/format_conversion.pyindex e5714187efe..bc0dd5868db 100644--- a/studio/backend/utils/datasets/format_conversion.py+++ b/studio/backend/utils/datasets/format_conversion.py@@ -236,8 +236,9 @@ def convert_chatml_to_alpaca(instructions = []outputs = []inputs = []+ column_systems = examples.get("system") or [None] * len(chatml_data)- for convo in chatml_data:+ for convo, column_system in zip(chatml_data, column_systems):turns = []for msg in convo or []:role = roles.get(msg.get("role") or msg.get("from"))@@ -248,6 +249,13 @@ def convert_chatml_to_alpaca(turns[-1][1] = f"{turns[-1][1]}\n\n{content}"else:turns.append([role, content])+ # A separate system column fills in only when the conversation has no leading system turn.+ if (+ isinstance(column_system, str)+ and column_system.strip()+ and not (turns and turns[0][0] == "system")+ ):+ turns.insert(0, ["system", column_system])system = ""context = []