Skip to content

[BUGFIX] gpt-oss chat template: emit blank lines after developer message - #19090

Closed
DimitriChrysafis wants to merge 2 commits into
unslothai:mainfrom
DimitriChrysafis:fix-gptoss-developer-blankline
Closed

DimitriChrysafis wants to merge 2 commits into
unslothai:mainfrom
DimitriChrysafis:fix-gptoss-developer-blankline

Conversation

@DimitriChrysafis

Copy link
Copy Markdown
Contributor

In our gpt-oss template, the \n\n after the developer message is only emitted when tools are present. Upstream moved it inside if developer_message (openai/gpt-oss-20b commit cbf31f6266), the day after the revision we ported, and we never picked that up. A developer message without tools runs straight into the next block.

This moves that one line. The output now matches the current official chat_template.jinja in every scenario I tried. Our more forgiving handling of content=None tool calls (#17176) stays as it is.

Tests: tests/python/test_gptoss_developer_block.py. 4 cases fail on main, and all 8 pass with the fix.

@danielhanchen

Copy link
Copy Markdown
Member

Thank you for the PR! After checking this against OpenAI's reference Harmony renderer (openai-harmony, which vLLM uses for gpt-oss), the current Unsloth template is already correct, so we will close this one.

openai-harmony renders a developer message without tools as # Instructions\n\nBe helpful.<|end|>, with no trailing blank line. That is what main produces today. The trailing \n\n in the official Hugging Face chat_template.jinja came in with openai/gpt-oss-120b discussion #85 (gpt-oss-20b commit cbf31f6266). That change fixed a different case, a stray blank line when tools are present and there are no instructions, and added this blank line as a side effect. Unsloth already handles that tools-only case correctly.

Comparing developer blocks against openai-harmony across 9 cases (instructions empty, one line or two lines; with or without tools; system or developer role):

Renderer Matches openai-harmony
Unsloth main 9/9
HF official jinja 5/9
This PR 5/9

All 4 mismatches are instructions without tools. With the PR, the last token before <|end|> would change from . to .\n\n, so gpt-oss fine-tunes with a system prompt would train on different tokens than vLLM and Ollama serve. Our unsloth/gpt-oss-20b template and the Ollama template both match main.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 24, 2026
…#19241)

Adds a regression test that pins the gpt-oss developer block rendered by
`get_chat_template(tokenizer, "gpt-oss")` (and the `gptoss` alias) to
the output of OpenAI's reference Harmony renderer, `openai-harmony`,
which vLLM uses for gpt-oss.

The official Hugging Face `chat_template.jinja` appends `\n\n` after the
developer instructions even when no tools are given (side effect of
openai/gpt-oss-120b discussion #85). Harmony does not. Unsloth main
already matches Harmony, but porting the HF template again (as in
unslothai#19090) would silently change the last token before `<|end|>` from `.`
to `.\n\n` for every gpt-oss fine-tune with a system prompt, so training
would no longer match what vLLM and Ollama serve.

Test cases (system and developer role, both template aliases):

| Case | Expected developer block |
|---|---|
| Instructions, no tools | `# Instructions\n\nBe helpful.<\|end\|>` |
| Instructions and tools | `# Instructions\n\nBe helpful.\n\n# Tools
...<\|end\|>` |
| Tools, no instructions | `# Tools ...<\|end\|>` |

The expected strings were rendered with `openai-harmony` 0.0.8. A second
test re-renders them with the live library when it is installed (skipped
otherwise), so the literals cannot drift from Harmony.

Verification:
- main: 13 passed (10 template cases plus the 3 live Harmony checks)
- main with the unslothai#19090 template change applied: 4 failed, 9 passed
(exactly the instructions-without-tools cells fail)

Test only, no library code changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants