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.

GPT-OSS template: keep earlier answers on the final channel when the turn has thinking (#14700)

Daniel Han02d9ba1faf78
diff --git a/tests/python/test_gpt_oss_final_return_token.py b/tests/python/test_gpt_oss_final_return_token.py
index 1f77cdcbc18..eb342a9584d 100644
--- a/tests/python/test_gpt_oss_final_return_token.py
+++ b/tests/python/test_gpt_oss_final_return_token.py
@@ -62,3 +62,29 @@ def test_earlier_turns_and_generation_prompt_keep_end():
assert "<|return|>" not in text
prompt = _render([USER, REPLY, {"role": "user", "content": "Bye"}], add_generation_prompt = True)
assert prompt == text + "<|start|>assistant"
+
+
[email protected]("thinking", ["Greet back.", ""])
+def test_earlier_answer_with_thinking_stays_on_the_final_channel(thinking):
+ # Matches openai/gpt-oss-20b: an earlier turn's reasoning is dropped and its answer is
+ # rendered on final. Labelling the answer as analysis fed it back as reasoning.
+ earlier = {"role": "assistant", "content": "Hello!", "thinking": thinking}
+ text = _render([USER, earlier, {"role": "user", "content": "Bye"}], add_generation_prompt = True)
+ assert "<|start|>assistant<|channel|>final<|message|>Hello!<|end|>" in text
+ assert "<|channel|>analysis<|message|>Hello!" not in text
+ if thinking:
+ assert thinking not in text
+ assert text == _render(
+ [USER, REPLY, {"role": "user", "content": "Bye"}], add_generation_prompt = True
+ )
+
+
+ "earlier",
+ [{"thinking": "Let me think."}, {"content": None, "thinking": "Let me think."}],
+)
+def test_earlier_turn_with_only_thinking_keeps_its_analysis(earlier):
+ # Matches unsloth/gpt-oss-20b: a previous turn with reasoning but no answer stays on analysis.
+ earlier = {"role": "assistant", **earlier}
+ text = _render([USER, earlier, {"role": "user", "content": "Bye"}], add_generation_prompt = True)
+ assert "<|start|>assistant<|channel|>analysis<|message|>Let me think.<|end|>" in text
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py
index f2c0583df8f..c430e85e515 100644
--- a/unsloth/chat_templates.py
+++ b/unsloth/chat_templates.py
@@ -1906,12 +1906,12 @@ gptoss_template = \
{#- At this point only assistant/user/tool messages should remain #}
{%- if message.role == 'assistant' -%}
{#- Checks to ensure the messages are being passed in the format we expect #}
- {%- if "content" in message %}
+ {%- if message.content is string %}
{%- if "<|channel|>analysis<|message|>" in message.content or "<|channel|>final<|message|>" in message.content %}
{{- raise_exception("You have passed a message containing <|channel|> tags in the content field. Instead of doing this, you should pass analysis messages (the string between '<|message|>' and '<|end|>') in the 'thinking' field, and final messages (the string between '<|message|>' and '<|end|>') in the 'content' field.") }}
{%- endif %}
{%- endif %}
- {%- if "thinking" in message %}
+ {%- if message.thinking is string %}
{%- if "<|channel|>analysis<|message|>" in message.thinking or "<|channel|>final<|message|>" in message.thinking %}
{{- raise_exception("You have passed a message containing <|channel|> tags in the thinking field. Instead of doing this, you should pass analysis messages (the string between '<|message|>' and '<|end|>') in the 'thinking' field, and final messages (the string between '<|message|>' and '<|end|>') in the 'content' field.") }}
{%- endif %}
@@ -1952,16 +1952,16 @@ gptoss_template = \
{%- elif loop.last and not add_generation_prompt %}
{#- Only render the CoT if the final turn is an assistant turn and add_generation_prompt is false #}
{#- This is a situation that should only occur in training, never in inference. #}
- {%- if "thinking" in message %}
+ {%- if message.thinking is string %}
{{- "<|start|>assistant<|channel|>analysis<|message|>" + message.thinking + "<|end|>" }}
{%- endif %}
{#- <|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 + "<|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|>" }}
+ {%- elif message.thinking is string and message.content is not string %}
+ {#- A previous turn with only CoT and no final answer: keep the analysis message #}
+ {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.thinking + "<|end|>" }}
{%- set last_tool_call.name = none %}
{%- else %}
{#- CoT is dropped during all previous turns, so we never render it for inference #}