Skip to content

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.

End the last gpt-oss training turn with <|return|>, not <|end|> (#13657)

Nilay48ca6b7b3719
diff --git a/tests/python/test_gpt_oss_final_return_token.py b/tests/python/test_gpt_oss_final_return_token.py
new file mode 100644
index 00000000000..1f77cdcbc18
--- /dev/null
+++ b/tests/python/test_gpt_oss_final_return_token.py
@@ -0,0 +1,64 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+import pytest
+from tokenizers import Tokenizer, models
+from transformers import PreTrainedTokenizerFast
+
+from unsloth.chat_templates import get_chat_template
+
+
[email protected](autouse = True)
+def _scratch_cwd(tmp_path, monkeypatch):
+ monkeypatch.chdir(tmp_path)
+
+
+def _tokenizer():
+ vocab = {"<unk>": 0, "<|return|>": 1, "<|endoftext|>": 2}
+ return get_chat_template(
+ PreTrainedTokenizerFast(
+ tokenizer_object = Tokenizer(models.WordLevel(vocab, unk_token = "<unk>")),
+ eos_token = "<|return|>",
+ unk_token = "<unk>",
+ pad_token = "<|endoftext|>",
+ ),
+ "gpt-oss",
+ )
+
+
+def _render(messages, add_generation_prompt = False):
+ return _tokenizer().apply_chat_template(
+ messages, tokenize = False, add_generation_prompt = add_generation_prompt
+ )
+
+
+USER = {"role": "user", "content": "Hi"}
+REPLY = {"role": "assistant", "content": "Hello!"}
+
+
+ "messages",
+ [
+ [USER, REPLY],
+ [{"role": "system", "content": "Be brief."}, USER, REPLY],
+ [USER, {"role": "assistant", "content": "Hello!", "thinking": "Greet back."}],
+ [
+ USER,
+ REPLY,
+ {"role": "user", "content": "Bye"},
+ {"role": "assistant", "content": "See you."},
+ ],
+ ],
+)
+def test_last_training_turn_ends_with_the_stop_token(messages):
+ text = _render(messages)
+ assert text.endswith("<|channel|>final<|message|>" + messages[-1]["content"] + "<|return|>")
+ assert text.count("<|return|>") == 1
+
+
+def test_earlier_turns_and_generation_prompt_keep_end():
+ text = _render([USER, REPLY, {"role": "user", "content": "Bye"}])
+ assert "<|channel|>final<|message|>Hello!<|end|><|start|>user" in text
+ assert "<|return|>" not in text
+ prompt = _render([USER, REPLY, {"role": "user", "content": "Bye"}], add_generation_prompt = True)
+ assert prompt == text + "<|start|>assistant"
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py
index b9036656ee1..b2ff8f5cc91 100644
--- a/unsloth/chat_templates.py
+++ b/unsloth/chat_templates.py
@@ -1448,7 +1448,7 @@ gptoss_template = \
{#- <|return|> indicates the end of generation, but <|end|> does not #}
{#- <|return|> should never be an input to the model, but we include it as the final token #}
{#- when training, so the model learns to emit it. #}
- {{- "<|start|>assistant<|channel|>final<|message|>" + message.content + "<|end|>" }}
+ {{- "<|start|>assistant<|channel|>final<|message|>" + message.content + "<|return|>" }}
{%- elif "thinking" in message %}
{#- CoT is dropped during all previous turns, so we never render it for inference #}
{{- "<|start|>assistant<|channel|>analysis<|message|>" + message.content + "<|end|>" }}