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: keep a dataset's thinking text when training Qwen models (#14016)
Nilay2682857ad654
diff --git a/studio/backend/tests/test_chat_template_thinking_rows.py b/studio/backend/tests/test_chat_template_thinking_rows.pynew file mode 100644index 00000000000..0e5b10cf747--- /dev/null+++ b/studio/backend/tests/test_chat_template_thinking_rows.py@@ -0,0 +1,96 @@+# 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++_QWEN3_TEMPLATE = """+{%- for message in messages %}+{%- if message.role == 'assistant' %}+{%- set reasoning_content = '' %}+{%- set content = message.content %}+{%- if message.reasoning_content is string %}{%- set reasoning_content = message.reasoning_content %}+{%- elif '</think>' in content %}+{%- set reasoning_content = content.split('</think>')[0].split('<think>')[-1].strip('\\n') %}+{%- set content = content.split('</think>')[-1].lstrip('\\n') %}+{%- endif %}+{{- '<|im_start|>assistant\\n<think>\\n' + reasoning_content + '\\n</think>\\n\\n' + content + '<|im_end|>\\n' }}+{%- else %}+{{- '<|im_start|>' + message.role + '\\n' + message.content + '<|im_end|>\\n' }}+{%- endif %}+{%- endfor %}+"""++_GPT_OSS_TEMPLATE = """+{%- for message in messages %}+{%- if message.role == 'assistant' and message.thinking %}+{{- '<|start|>assistant<|channel|>analysis<|message|>' + message.thinking + '<|end|>' }}+{%- endif %}+{{- '<|start|>' + message.role + '<|message|>' + message.content + '<|end|>' }}+{%- endfor %}+"""++_THINKING = "The user asks in French about the capital. It is Paris."+++class _JinjaTokenizer:+ eos_token = ""++ def __init__(self, template):+ self.chat_template = template++ def apply_chat_template(self, conversation, **_kwargs):+ sandbox = pytest.importorskip("jinja2.sandbox")+ env = sandbox.ImmutableSandboxedEnvironment(trim_blocks = True, lstrip_blocks = True)+ return env.from_string(self.chat_template).render(messages = conversation)+++def _multilingual_thinking_rows(answer = "Paris."):+ return Dataset.from_list(+ [+ {+ "reasoning_language": "French",+ "messages": [+ {"role": "system", "content": "reasoning language: French", "thinking": None},+ {+ "role": "user",+ "content": "Quelle est la capitale de la France ?",+ "thinking": None,+ },+ {"role": "assistant", "content": answer, "thinking": _THINKING},+ ],+ }+ ]+ )+++def _training_text(template, answer = "Paris."):+ result = format_and_template_dataset(+ _multilingual_thinking_rows(answer),+ model_name = "stub-model",+ tokenizer = _JinjaTokenizer(template),+ num_proc = 1,+ )+ assert result["success"] is True, result["errors"]+ return result["dataset"][0]["text"]+++def test_dataset_thinking_trains_inside_qwen_think_block():+ text = _training_text(_QWEN3_TEMPLATE)++ assert f"<|im_start|>assistant\n<think>\n{_THINKING}\n</think>\n\nParis.<|im_end|>" in text+++def test_dataset_thinking_renders_once_on_a_template_reading_thinking():+ text = _training_text(_GPT_OSS_TEMPLATE)++ assert text.count(_THINKING) == 1+++def test_dataset_thinking_already_inline_in_content_renders_once():+ text = _training_text(_QWEN3_TEMPLATE, answer = f"<think>\n{_THINKING}\n</think>\n\nParis.")++ assert text.count(_THINKING) == 1+ assert f"<think>\n{_THINKING}\n</think>\n\nParis.<|im_end|>" in textdiff --git a/studio/backend/utils/datasets/chat_templates.py b/studio/backend/utils/datasets/chat_templates.pyindex b8c2eac2008..96912d7e766 100644--- a/studio/backend/utils/datasets/chat_templates.py+++ b/studio/backend/utils/datasets/chat_templates.py@@ -344,9 +344,30 @@ def _render_conversation(tokenizer, conversation, tools = None, fallback_withoutreturn _render_messages(tokenizer, conversation, tools, fallback_without_tools)+def _thinking_as_reasoning_content(tokenizer, conversation):+ def wants_alias(message):+ return (+ isinstance(message, dict)+ and message.get("thinking")+ and message.get("reasoning_content") is None+ and "</think>" not in str(message.get("content") or "")+ )++ # Rows are checked first: stringifying a named-template catalog per row is costly.+ if not any(wants_alias(message) for message in conversation):+ return conversation+ if "reasoning_content" not in str(getattr(tokenizer, "chat_template", None) or ""):+ return conversation+ return [+ {**message, "reasoning_content": message["thinking"]} if wants_alias(message) else message+ for message in conversation+ ]++def _render_messages(tokenizer, conversation, tools = None, fallback_without_tools = True):from core.inference.chat_template_helpers import _normalize_tool_call_arguments+ conversation = _thinking_as_reasoning_content(tokenizer, conversation)attempts = []for messages in (_drop_none_values(conversation), conversation):for attempt in (_normalize_tool_call_arguments(messages), messages):