Skip to content

Ollama Phi-4 template: write roles as text, not <|user|> pseudo tokens - #17285

Merged
danielhanchen merged 3 commits into
unslothai:mainfrom
jaeminbird:fix-phi4-ollama-role-tokens
Nov 12, 2026
Merged

danielhanchen merged 3 commits into
unslothai:mainfrom
jaeminbird:fix-phi4-ollama-role-tokens

Conversation

@jaeminbird

Copy link
Copy Markdown
Contributor

The Phi-4 Ollama template (OLLAMA_TEMPLATES["phi-4"], used by save_pretrained_gguf / create_ollama_modelfile) writes each role as <|im_start|><|user|><|im_sep|>. Phi-4's vocabulary has no <|system|>, <|user|> or <|assistant|> token, so Ollama splits each one into several ordinary tokens on every turn, which the model never saw in training. The HF chat template, phi4_template and ollama.com/library/phi4 all write roles as plain text (<|im_start|>user<|im_sep|>).

Cause: _phi4_ollama_template in unsloth/ollama_template_mappers.py, dating from the original Phi-4 commit 6cbfca8. The same unused copy in chat_templates.py is fixed too.

Testing:

  • Rendered with Ollama's own Go package (github.com/ollama/ollama/template): main differs from the HF jinja output on 4 of 4 conversations, the fix is byte-identical on all 4.
  • Ollama 0.40.13 in Docker, GGUF with the phi-4 chat template plus the Modelfile from create_ollama_modelfile: one user turn is 25 prompt tokens on main, 19 with the fix.
  • tests/test_ollama_phi4_roles.py fails on main, passes with the fix. Not tested: generation quality.

Phi-4's vocabulary has no <|system|>, <|user|> or <|assistant|> tokens, so
the Modelfile template split each role into several ordinary tokens on
every turn (one user turn: 25 prompt tokens in Ollama, 19 with the fix).
Roles are now plain text, as in the HF chat template, and the rendered
Ollama prompt matches it byte for byte.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T14:27:12.068556Z b30bcfa Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: b30bcfae72

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit b2784e3 into unslothai:main Nov 12, 2026
@jaeminbird
jaeminbird deleted the fix-phi4-ollama-role-tokens branch November 13, 2026 04:39
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