Improved and fixed template for GPT-OSS (again). Includes preserve_thinking and fix for Unsloth-induced bug
retained text
I posted an updated GPT-OSS [template](https://huggingface.co/arbv/gpt-oss-fixed-jinja-template) a [couple of months ago](https://www.reddit.com/r/LocalLLaMA/comments/1vfo8be/gptoss_has_turned_one_year_old_today/), which was based on [Unsloth's version](https://huggingface.co/unsloth/gpt-oss-120b/blob/main/chat_template.jinja).
It turns out that both Unsloth's version (and, thus, mine) contain a very serious bug that can degrade the model when chat history is replayed and contains previous reasoning (aka the `analysis` channel) turns. As far as I can tell, retaining this history is pretty much the default for a lot of tools now and definitely can happen at the API level - tools can consolidate reasoning and answer.
Can you spot the problem in this snippet from the message rendering loop? (Taken from [Unsloth's template](https://huggingface.co/unsloth/gpt-oss-120b/blob/main/chat_template.jinja#L317)):
```jinja
{%- elif "thinking" in message %}
{#- CoT is dropped during all previous turns, so we never render it for inference #}
{{- "<|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 #}
{{- "<|start|>assistant<|channel|>final<|message|>" + message.content + "<|end|>" }}
{%- set last_tool_call.name = none %}
```
When chat history is rendered, in cases where a message contains both `content` (the model's answer) and `thinking` (reasoning), only the reasoning is rendered for the model, while the answer itself is dropped! That can significantly confuse the model across turns.
The comment is also wrong—the whole branch looks like a copy-paste error. [OpenAI's reference template](https://huggingface.co/openai/gpt-oss-120b/blob/main/chat_template.jinja#L302) does not have it.
I noticed that in some cases GPT-OSS 20B could go completely off the rails, and now I see why. Interestingly, GPT-OSS 120B seems smart enough to recover the context and direction of the conversation using only the reasoning traces.
After this experience, I implemented `preserve_thinking` in my template as well, because the model handles it just fine without losing coherence. This should make multi-turn inference faster in harnesses (via prefix caching) at the expense of higher token usage.
So there you have it: https://huggingface.co/arbv/gpt-oss-fixed-jinja-template
Give GPT-OSS a second chance if you are bored.
Noticed by pure chance while working on a fixed template for Laguna XS/S 2.1, but more on that another day.
P.S.
Casting u/danielhanchen to take a look, too.
document fetched 2026-09-27 00:38 · canonical https://reddit.com/r/LocalLLaMA/comments/1wr0wki/improved_and_fixed_template_for_gptoss_again