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
Keep a dataset's thinking text when training Gemma 4 E2B and E4B (#15569)
Nilay1b0b58bf9346
diff --git a/studio/backend/tests/test_chat_template_thinking_rows.py b/studio/backend/tests/test_chat_template_thinking_rows.pyindex 0e5b10cf747..dbf9fb27559 100644--- a/studio/backend/tests/test_chat_template_thinking_rows.py+++ b/studio/backend/tests/test_chat_template_thinking_rows.py@@ -1,6 +1,9 @@# 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 ast+from pathlib import Path+import pytestfrom datasets import Dataset@@ -32,6 +35,17 @@ _GPT_OSS_TEMPLATE = """{%- endfor %}"""+_REPO_ROOT = Path(__file__).resolve().parents[3]+++def _unsloth_template(name):+ source = (_REPO_ROOT / "unsloth" / "chat_templates.py").read_text(encoding = "utf-8")+ for node in ast.parse(source).body:+ if isinstance(node, ast.Assign) and getattr(node.targets[0], "id", "") == name:+ return ast.literal_eval(node.value)+ raise AssertionError(name)++_THINKING = "The user asks in French about the capital. It is Paris."@@ -83,6 +97,12 @@ def test_dataset_thinking_trains_inside_qwen_think_block():assert f"<|im_start|>assistant\n<think>\n{_THINKING}\n</think>\n\nParis.<|im_end|>" in text+def test_dataset_thinking_trains_inside_gemma4_thought_channel():+ text = _training_text(_unsloth_template("gemma4_template"))++ assert f"<|turn>model\n<|channel>thought\n{_THINKING}\n<channel|>Paris.<turn|>" in text++def test_dataset_thinking_renders_once_on_a_template_reading_thinking():text = _training_text(_GPT_OSS_TEMPLATE)diff --git a/tests/test_gemma4_chat_template.py b/tests/test_gemma4_chat_template.pyindex 8c2c8be5d2e..ee32c796686 100644--- a/tests/test_gemma4_chat_template.py+++ b/tests/test_gemma4_chat_template.py@@ -273,3 +273,16 @@ def test_object_tool_result_renders_its_fields(name):assert out.endswith('<|tool_response>response:get_weather{sky:<|"|>sunny<|"|>,temperature:18}<tool_response|>')++[email protected]("key", ["reasoning_content", "reasoning"])+def test_final_turn_reasoning_becomes_the_thought_channel(key):+ msgs = [+ {"role": "user", "content": "2+2?"},+ {"role": "assistant", "content": "4", "reasoning_content": "first"},+ {"role": "user", "content": "3+3?"},+ {"role": "assistant", "content": "6", key: "3+3=6"},+ ]+ out = _render("gemma4_template", msgs)+ assert "first" not in out+ assert out.endswith("<|turn>model\n<|channel>thought\n3+3=6\n<channel|>6<turn|>\n")diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.pyindex 3c57b259219..603b6da8e20 100644--- a/unsloth/chat_templates.py+++ b/unsloth/chat_templates.py@@ -1109,6 +1109,8 @@ gemma4_template = \{%- endif -%}{{ '<turn|>\n' }}{%- endif -%}+{%- set ns_turn = namespace(last_user_idx=-1) -%}+{%- for m in loop_messages -%}{%- if m['role'] == 'user' -%}{%- set ns_turn.last_user_idx = loop.index0 -%}{%- endif -%}{%- endfor -%}{%- set ns_tool = namespace(turns=0, tool_calls=false, tool_response=false) -%}{%- for message in loop_messages -%}{%- if message['role'] != 'tool' -%}@@ -1125,6 +1127,12 @@ gemma4_template = \{%- set ns_tool.turns = ns_tool.turns + 1 -%}{{ '<|turn>' + role + '\n' }}{%- endif -%}+ {%- if role == "model" -%}+ {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}+ {%- if thinking_text and loop.index0 > ns_turn.last_user_idx -%}+ {{ '<|channel>thought\n' + thinking_text + '\n<channel|>' }}+ {%- endif -%}+ {%- endif -%}{%- set ns_tool.tool_calls = message.get('tool_calls') -%}{%- if ns_tool.tool_calls -%}{%- for tool_call in ns_tool.tool_calls -%}