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 prompt/completion message lists as one conversation (#12910)
Nilay4fa4db5ee280
diff --git a/studio/backend/hub/utils/dataset_format.py b/studio/backend/hub/utils/dataset_format.pyindex 33523a454bb..933d66fe77e 100644--- a/studio/backend/hub/utils/dataset_format.py+++ b/studio/backend/hub/utils/dataset_format.py@@ -7,7 +7,7 @@ import refrom typing import Any, Optionalfrom utils.datasets.cells import text_cell_check-from utils.datasets.format_detection import detect_dataset_format+from utils.datasets.format_detection import detect_dataset_format, has_message_prompt_completiondef _first_row(dataset) -> Optional[dict]:@@ -338,6 +338,14 @@ def detect_custom_format_heuristic(dataset):if col not in mapping and is_context_column(col):mapping[col] = "system"break+ if has_message_prompt_completion(dataset):+ mapping = {+ col: role+ for col, role in mapping.items()+ if role == "system" and col not in {"messages", "conversations", "texts"}+ }+ mapping.update({"prompt": "user", "completion": "assistant"})+ has_user = has_assistant = Truereturn mapping if has_user and has_assistant else Nonediff --git a/studio/backend/tests/test_dataset_prompt_completion_messages.py b/studio/backend/tests/test_dataset_prompt_completion_messages.pynew file mode 100644index 00000000000..ee6e11cff07--- /dev/null+++ b/studio/backend/tests/test_dataset_prompt_completion_messages.py@@ -0,0 +1,196 @@+# 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 hub.utils.dataset_format import check_dataset_format+from utils.datasets import format_dataset++_SYSTEM = {"role": "system", "content": "Answer in one word."}+++def _user(text):+ return {"role": "user", "content": text}+++def _assistant(text):+ return {"role": "assistant", "content": text}+++_CASES = [+ pytest.param(+ {"prompt": [_user("What color is the sky?")], "completion": [_assistant("Blue")]},+ [_user("What color is the sky?"), _assistant("Blue")],+ id = "trl",+ ),+ pytest.param(+ {"prompt": [_SYSTEM, _user("Sky?")], "completion": [_assistant("Blue")]},+ [_SYSTEM, _user("Sky?"), _assistant("Blue")],+ id = "system-in-prompt",+ ),+ pytest.param(+ {+ "prompt": [_user("Hi"), _assistant("Hello"), _user("Sky?")],+ "completion": [_assistant("Blue")],+ },+ [_user("Hi"), _assistant("Hello"), _user("Sky?"), _assistant("Blue")],+ id = "multi-turn-prompt",+ ),+ pytest.param(+ {+ "prompt": [_user("Sky?")],+ "completion": [_assistant("Blue"), _user("Grass?"), _assistant("Green")],+ },+ [_user("Sky?"), _assistant("Blue"), _user("Grass?"), _assistant("Green")],+ id = "multi-message-completion",+ ),+ pytest.param(+ {"prompt": "Sky?", "completion": [_assistant("Blue")]},+ [_user("Sky?"), _assistant("Blue")],+ id = "text-prompt",+ ),+ pytest.param(+ {"prompt": [_SYSTEM, _user("Sky?")], "completion": "Blue"},+ [_SYSTEM, _user("Sky?"), _assistant("Blue")],+ id = "text-completion",+ ),+]++_MAPPING = {"prompt": "user", "completion": "assistant"}++[email protected]("mapping", [None, _MAPPING], ids = ["auto_mapping", "user_mapping"])[email protected]("row, conversation", _CASES)+def test_prompt_completion_messages_train_as_one_conversation(row, conversation, mapping):+ result = format_dataset(Dataset.from_list([row]), custom_format_mapping = mapping)++ assert result["chat_column"] == "conversations"+ assert result["dataset"][0]["conversations"] == conversation++[email protected]("row, conversation", _CASES)+def test_check_suggests_prompt_and_completion_together(row, conversation):+ result = check_dataset_format(Dataset.from_list([row]))++ assert result["detected_format"] == "custom_heuristic"+ assert result["suggested_mapping"] == _MAPPING++[email protected]("prompt", [[_user("Sky?")], "Sky?"], ids = ["messages", "text"])+def test_prompt_completion_pair_wins_over_another_chat_column(prompt):+ row = {+ "prompt": prompt,+ "completion": [_assistant("Blue")],+ "history": [_user("Hi"), _assistant("Hello")],+ }+ dataset = Dataset.from_list([row])++ assert check_dataset_format(dataset)["suggested_mapping"] == _MAPPING+ result = format_dataset(dataset)+ assert result["dataset"][0]["conversations"] == [_user("Sky?"), _assistant("Blue")]++[email protected]("chat_column", ["messages", "conversations", "texts"])+def test_prompt_completion_pair_wins_over_an_exact_chat_column(chat_column):+ row = {+ "prompt": [_user("Sky?")],+ "completion": [_assistant("Blue")],+ chat_column: [_user("Unrelated?"), _assistant("Unrelated")],+ }+ dataset = Dataset.from_list([row])++ assert check_dataset_format(dataset)["suggested_mapping"] == _MAPPING+ result = format_dataset(dataset)+ assert result["dataset"][0]["conversations"] == [_user("Sky?"), _assistant("Blue")]+++def test_plain_prompt_completion_keeps_existing_column_priority():+ question = "This longer question column keeps its prior user-role priority over prompt text."+ dataset = Dataset.from_list([{"prompt": "p", "completion": "c", "question": question}])++ assert check_dataset_format(dataset)["suggested_mapping"] == {+ "completion": "assistant",+ "question": "user",+ }+ result = format_dataset(dataset)+ assert result["dataset"][0]["conversations"] == [_user(question), _assistant("c")]++[email protected]("mapping", [None, {"question": "user", "answer": "assistant"}])+def test_empty_generic_list_remains_training_text(mapping):+ rows = [+ {"question": "Return an empty JSON array", "answer": []},+ {"question": "Return one labelled item", "answer": [{"label": "x"}]},+ ]++ result = format_dataset(Dataset.from_list(rows), custom_format_mapping = mapping)++ assert result["dataset"][0]["conversations"] == [+ _user("Return an empty JSON array"),+ _assistant("[]"),+ ]+++_CALL = {"id": "c1", "type": "function", "function": {"name": "weather", "arguments": "{}"}}++[email protected]("mapping", [None, _MAPPING], ids = ["auto_mapping", "user_mapping"])+def test_tool_calls_and_empty_message_lists(mapping):+ rows = [+ {+ "prompt": [_user("Weather?")],+ "completion": [+ {"role": "assistant", "content": None, "tool_calls": [_CALL]},+ {"role": "tool", "content": "Sunny", "tool_call_id": "c1"},+ _assistant("Sunny"),+ ],+ },+ {"prompt": [_user("Sky?")], "completion": []},+ ]+ result = format_dataset(Dataset.from_list(rows), custom_format_mapping = mapping)++ conversations = [+ [{key: value for key, value in turn.items() if value is not None} for turn in row]+ for row in result["dataset"]["conversations"]+ ]+ assert conversations == [+ [+ _user("Weather?"),+ {"role": "assistant", "tool_calls": [_CALL]},+ {"role": "tool", "content": "Sunny", "tool_call_id": "c1"},+ _assistant("Sunny"),+ ],+ [_user("Sky?")],+ ]++[email protected]("mapping", [None, _MAPPING], ids = ["auto_mapping", "user_mapping"])+def test_tool_call_message_may_omit_content(mapping):+ row = {+ "prompt": [_user("Weather?")],+ "completion": [{"role": "assistant", "tool_calls": [_CALL]}],+ }++ result = format_dataset(Dataset.from_list([row]), custom_format_mapping = mapping)+ conversation = [+ {key: value for key, value in turn.items() if value is not None}+ for turn in result["dataset"][0]["conversations"]+ ]++ assert conversation == [_user("Weather?"), {"role": "assistant", "tool_calls": [_CALL]}]++[email protected]("mapping", [None, _MAPPING], ids = ["auto_mapping", "user_mapping"])+def test_sharegpt_message_lists_train_as_one_conversation(mapping):+ row = {+ "prompt": [{"from": "system", "value": "Be brief."}, {"from": "human", "value": "Sky?"}],+ "completion": [{"from": "gpt", "value": "Blue"}],+ }+ result = format_dataset(Dataset.from_list([row]), custom_format_mapping = mapping)++ assert result["dataset"][0]["conversations"] == [+ {"role": "system", "content": "Be brief."},+ _user("Sky?"),+ _assistant("Blue"),+ ]diff --git a/studio/backend/utils/datasets/cells.py b/studio/backend/utils/datasets/cells.pyindex c04267c8961..894199d221c 100644--- a/studio/backend/utils/datasets/cells.py+++ b/studio/backend/utils/datasets/cells.py@@ -30,7 +30,7 @@ def cell_text(value):if value is None:return ""if isinstance(value, dict) and {"text", "answer_start"} <= value.keys():- # SQuAD-style `answers` span: train the first answer, as _extract_column_value does.+ # match _extract_column_value by training the first SQuAD answer.answer = value["text"]if isinstance(answer, list):answer = answer[0] if answer else None@@ -42,18 +42,63 @@ def cell_text(value):return str(value)+_SHAREGPT_ROLES = {"human": "user", "gpt": "assistant"}+++def _message(turn):+ if not isinstance(turn, dict):+ return None+ if {"role", "content"} <= turn.keys():+ return turn+ if turn.get("role") == "assistant" and "tool_calls" in turn:+ return {**turn, "content": None}+ if {"from", "value"} <= turn.keys():+ return {"role": _SHAREGPT_ROLES.get(turn["from"], turn["from"]), "content": turn["value"]}+ return None+++def message_list_columns(dataset):+ features = getattr(dataset, "features", None) or {}+ columns = set()+ for column, feature in features.items():+ item = getattr(feature, "feature", None)+ if item is None and isinstance(feature, list) and len(feature) == 1:+ item = feature[0]+ if isinstance(item, dict) and (+ {"role", "content"} <= item.keys() or {"from", "value"} <= item.keys()+ ):+ columns.add(column)+ return columns+++def cell_turns(+ value,+ role,+ empty_is_messages = False,+):+ messages = [_message(turn) for turn in value] if isinstance(value, list) else [None]+ if (messages or empty_is_messages) and all(message is not None for message in messages):+ return [+ message+ if message["content"] is None and message.get("tool_calls")+ else {**message, "content": cell_text(message["content"])}+ for message in messages+ ]+ return [{"role": role, "content": cell_text(value)}]++def _column_ids(dataset) -> dict:features = getattr(dataset, "features", None) or {}return {column: getattr(feature, "id", None) for column, feature in features.items()}def typed_csv_columns(dataset) -> frozenset:- """CSV columns the csv loader would have typed: numbers, booleans or only missing cells."""+ """CSV columns inferred as numbers, booleans, or all missing by the loader."""return frozenset(column for column, tag in _column_ids(dataset).items() if tag == _CSV_TYPED)def text_cell_check(dataset):- """`(column, cell) -> bool`: False for CSV cells the csv loader would not read as strings."""+ """`(column, cell) -> bool`: `False` for CSV cells the loader would not parse as strings."""ids = _column_ids(dataset)def is_text(column, cell):diff --git a/studio/backend/utils/datasets/dataset_utils.py b/studio/backend/utils/datasets/dataset_utils.pyindex f12986d124b..fd63d78b734 100644--- a/studio/backend/utils/datasets/dataset_utils.py+++ b/studio/backend/utils/datasets/dataset_utils.py@@ -26,7 +26,7 @@ from .chat_templates import (get_tokenizer_chat_template,DEFAULT_ALPACA_TEMPLATE,)-from .cells import cell_text+from .cells import cell_text, cell_turns, message_list_columnsfrom .raw_text import prepare_raw_text_datasetfrom .vlm_processing import generate_smart_vlm_instructionfrom .data_collators import DeepSeekOCRDataCollator, VLMDataCollator@@ -37,7 +37,7 @@ logger = get_logger(__name__)def check_dataset_format(dataset, is_vlm: bool = False) -> dict:- """Lightweight format check without processing, for frontend validation: quickly determines whether the user must manually map columns before the full format_and_template_dataset(). Returns requires_manual_mapping, detected_format, columns (for the mapping UI), suggested_mapping, and detected_image_column / detected_text_column for VLM datasets."""+ """check whether frontend column mapping is required without processing the dataset."""columns = (list(dataset.column_names)if hasattr(dataset, "column_names")@@ -164,6 +164,14 @@ _CHATML_TO_ALPACA = {"user": "instruction", "system": "input", "assistant": "out_KNOWN_CHAT_COLUMNS = {"messages", "conversations", "texts"}+def _mapped_message_columns(dataset, columns):+ messages = message_list_columns(dataset)+ prompt_completion = {"prompt", "completion"}+ if prompt_completion <= set(columns) and messages & prompt_completion:+ messages.update(prompt_completion)+ return messages++def _chatml_final_format(chat_column: str | None) -> str:return "chatml_messages" if chat_column == "messages" else "chatml_conversations"@@ -179,20 +187,19 @@ def _apply_user_mapping(mapping: dict,batch_size: int = 1000,):- """Apply user-provided column mapping to convert dataset to conversations format. Accepts chatml (user/assistant/system), sharegpt (human/gpt/system) and alpaca (instruction/input/output) role names, all normalised to chatml. If the mapping has ``__``-prefixed metadata keys (from the conversion advisor), routes to template-based conversion instead of simple role mapping. Returns a dataset with a single 'conversations' column."""+ """normalize chatml, ShareGPT, or Alpaca roles; ``__`` metadata selects template conversion."""meta = {k: v for k, v in mapping.items() if k.startswith("__")}column_roles = {k: v for k, v in mapping.items() if not k.startswith("__")}if meta:return _apply_template_mapping(dataset, column_roles, meta, batch_size)- # ── Simple mode (original logic) ──- # Pre-compute: group columns by canonical chatml rolerole_groups: dict[str, list[str]] = {r: [] for r in _CHATML_ROLE_ORDER}for col_name, role in column_roles.items():canonical = _TO_CHATML.get(role)if canonical:role_groups[canonical].append(col_name)+ message_columns = _mapped_message_columns(dataset, column_roles)def _convert(examples):num = len(next(iter(examples.values())))@@ -202,12 +209,12 @@ def _apply_user_mapping(for chatml_role in _CHATML_ROLE_ORDER:for col in role_groups[chatml_role]:if col in examples:- content = examples[col][i]- convo.append(- {- "role": chatml_role,- "content": cell_text(content),- }+ convo.extend(+ cell_turns(+ examples[col][i],+ chatml_role,+ empty_is_messages = col in message_columns,+ ))conversations.append(convo)return {"conversations": conversations}@@ -221,7 +228,6 @@ def _apply_user_mapping(def _extract_column_value(val, col: str, label_mapping: dict) -> str:- """Extract a string value from a column, handling complex types and label mapping."""if isinstance(val, dict):if "text" not in val:return json.dumps(val, ensure_ascii = False)@@ -511,6 +517,7 @@ def format_dataset(custom_mapping = detect_custom_format_heuristic(dataset)if custom_mapping:warnings.append(f"Auto-detected column mapping: {custom_mapping}")+ message_columns = _mapped_message_columns(dataset, custom_mapping)def _apply_auto_mapping(examples):conversations = []@@ -519,7 +526,9 @@ def format_dataset(all_columns = set(examples.keys())mapped_columns = set(custom_mapping.keys())preserved_columns = {- col: examples[col] for col in all_columns - mapped_columns+ col: examples[col]+ for col in all_columns - mapped_columns+ if col != "conversations"}for i in range(num_examples):@@ -527,12 +536,18 @@ def format_dataset(for target_role in ["system", "user", "assistant"]:for col_name, role in custom_mapping.items():if role == target_role and col_name in examples:- text = cell_text(examples[col_name][i])- if text.strip():- convo.append({"role": role, "content": text})+ convo.extend(+ turn+ for turn in cell_turns(+ examples[col_name][i],+ role,+ empty_is_messages = col_name in message_columns,+ )+ if turn.get("tool_calls") or turn["content"].strip()+ )conversations.append(convo)- return {"conversations": conversations, **preserved_columns}+ return {**preserved_columns, "conversations": conversations}try:dataset = dataset.map(diff --git a/studio/backend/utils/datasets/format_detection.py b/studio/backend/utils/datasets/format_detection.pyindex bddaa035656..fbc237704b6 100644--- a/studio/backend/utils/datasets/format_detection.py+++ b/studio/backend/utils/datasets/format_detection.py@@ -89,8 +89,28 @@ def _inspect_conversation_column(rows: list[dict], column_name: str) -> dict | Nreturn None+def _has_message_prompt_completion(rows: list[dict], column_names: list[str]) -> bool:+ if not {"prompt", "completion"} <= set(column_names):+ return False+ for column_name in ("prompt", "completion"):+ inspected = _inspect_conversation_column(rows, column_name)+ if inspected and inspected["format"] in {"sharegpt", "chatml"}:+ return True+ return False+++def has_message_prompt_completion(dataset) -> bool:+ rows = _sample_dataset_rows(dataset)+ return bool(rows) and _has_message_prompt_completion(+ rows, _get_dataset_column_names(dataset, rows[0])+ )++def _detect_conversation_column(rows: list[dict], column_names: list[str]) -> dict | None:column_name_set = set(column_names)+ if _has_message_prompt_completion(rows, column_names):+ return None+unknown_exact = Nonefor column_name in CONVERSATION_COLUMNS:if column_name not in column_name_set:@@ -409,7 +429,6 @@ def detect_custom_format_heuristic(dataset):for col in content_columnsif col not in system_named and is_non_metadata_column(col) and has_keyword(col, user_words)]-assistant_candidates = []for col in assistant_potential:score = score_column(col, assistant_words, "assistant", len(assistant_potential))@@ -521,6 +540,15 @@ def detect_custom_format_heuristic(dataset):mapping[col] = "system"break+ if has_message_prompt_completion(dataset):+ mapping = {+ col: role+ for col, role in mapping.items()+ if role == "system" and col not in CONVERSATION_COLUMNS+ }+ mapping.update({"prompt": "user", "completion": "assistant"})+ has_user = has_assistant = True+if has_user and has_assistant:return mapping@@ -528,7 +556,6 @@ def detect_custom_format_heuristic(dataset):def detect_multimodal_dataset(dataset):- """Detect multimodal data (images and/or audio) in a dataset. Two passes per modality, a column-name keyword heuristic then value-type inspection, returning is_image/is_audio flags, detected columns, modality types and detected audio/text/speaker columns."""sample = next(iter(dataset))column_names = list(sample.keys())