A community GGUF imported into Ollama 0.35 with `FROM ./model.gguf` runs past its answer or prints role markers — how do you find and fix its prompt template?
answer
- who formats the turns
- autodetected template on import
- GGUF tokenizer.chat_template as fallback
- ollama show --template and the server log
- Go TEMPLATE plus PARAMETER stop
basics
~20 sCheck which template Ollama used: create may have matched the GGUF to a close-but-wrong built-in template, or the file has none. Write the model's format as a Go TEMPLATE with matching PARAMETER stop lines and re-run ollama create.
solid answer
~50 sOn a GGUF import, Ollama 0.35 compares the file's embedded `tokenizer.chat_template` with its built-in Go templates; when one is close enough it stores that as the model's `TEMPLATE`, with the template's stop strings (create prints `using autodetected template ...`). With no close match it stores no template, and chats are rendered by the bundled llama.cpp server from the GGUF's own template. A file with no template at all leaves Ollama no model-specific format, and the server log warns that responses may be poorly formatted. Diagnose with `ollama show --template` (a bare `{{ .Prompt }}` means none was stored) and `--parameters`, and the server log's `template selection` line. Fix by writing the model's real format as a Go `TEMPLATE` (`.System`, `.Prompt`, `.Response`, or `range .Messages`) plus `PARAMETER stop` for its end-of-turn markers, then `ollama create` again — the weights are reused.
code
dockerfile · 9 linesFROM ./your-model-Q4_K_M.gguf
TEMPLATE """{{ if .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}{{ if .Prompt }}<|im_start|>user
{{ .Prompt }}<|im_end|>
{{ end }}<|im_start|>assistant
"""
PARAMETER stop "<|im_start|>"
PARAMETER stop "<|im_end|>"go deeper
Recall that a chat model needs its prompt wrapped in its own turn markers, and that a Modelfile TEMPLATE plus stop lines sets them.
Explain the import path: autodetected Go template when one is close, the GGUF's own template when none matches, nothing when the file has no template.
Diagnose with ollama show --template, the stop parameters and the template selection log line before blaming the weights, then pin the format and rebuild.
Decide whether team-imported GGUFs ship with an explicit, reviewed TEMPLATE and stop list instead of relying on autodetection, trading upkeep for predictable behaviour.
## What a template does, and why it breaks A chat model is trained on text with its own **turn markers** — tokens that say where a system message, a user turn and the assistant's turn start and end. A runtime has to wrap your messages in exactly those markers, and stop generating at the end-of-turn marker. Get either wrong and the symptoms are the ones in the question: the model keeps writing past its answer (often inventing the next user turn), echoes markers as text, or answers in an odd register. What each model family's markers are belongs to the chat-template material; here the subject is how Ollama picks a template for an imported GGUF and how you override it. ## What `ollama create` does on a GGUF import (0.35.0) When the Modelfile says `FROM ./model.gguf`, create reads the GGUF's metadata, including the **embedded Jinja chat template** (`tokenizer.chat_template`), and then: 1. **Tries to autodetect.** It compares the embedded template with Ollama's built-in Go templates (ChatML, Llama 3 instruct, Mistral instruct, Gemma and others) and takes the closest one if it is within a small edit distance. It stores that template as the model's `TEMPLATE` layer, plus the template's stop strings, and prints `using autodetected template <name>`. 2. **Falls back to the file's own template.** With no close match, no `TEMPLATE` layer is stored. Chat requests are then rendered natively by the bundled llama.cpp server from the GGUF's embedded template. 3. **Has nothing to work with.** If the GGUF carries no chat template and you gave no `TEMPLATE`, Ollama has no model-specific format to apply. The template docs describe an imported model's default as `{{ .Prompt }}` — input sent verbatim — and the server logs `model is missing tokenizer.chat_template and Go TEMPLATE support is unavailable; chat responses may be poorly formatted`. When a `TEMPLATE` exists — autodetected, inherited or yours — Ollama renders with it by default; in a narrow case where the GGUF's own template handles tool calls the Go template cannot, Ollama may prefer the GGUF's. ## Where it goes wrong | Case | What you see | Why | |---|---|---| | Close-but-wrong autodetect | runs past the answer, stray markers | the nearest built-in template is not quite this model's format, or its stop list misses the model's end marker | | No embedded template | raw continuation, no turn structure | no model-specific format is available to wrap the messages | | Hand-written `TEMPLATE` with a typo | markers printed, roles confused | your template, not the file's, is what renders | | A later `PARAMETER stop` line | may run past the answer | your stop list replaced the template's stops | ## Diagnosing it - `ollama show --template your-model` prints the stored Go template. The bare default `{{ .Prompt }}` means no template was stored, so chats fall to the GGUF's own template when the file has one — not necessarily a fault, but now you know which path runs. - `ollama show --parameters your-model` shows the stop strings; check that the model's end-of-turn marker is among them. - The server log prints a `template selection` line when the model is loaded, with `selected=` naming the source: `go_template`, `gguf_chat_template` or `none`. - Compare with the model card's documented prompt format, the reference for what the markers should be. ## Fixing it Write the format yourself in the Modelfile and rebuild: - `TEMPLATE` takes **Go template syntax**, not Jinja: `{{ .System }}`, `{{ .Prompt }}` and `{{ .Response }}` for the simple form, or `{{ range .Messages }}` with `.Role` and `.Content` for multi-turn chat. Text after `{{ .Response }}` is omitted while generating. - Add one `PARAMETER stop` line per end-of-turn marker the model emits. - Run `ollama create your-model -f Modelfile`. Create parses the template first and refuses an invalid one, and the GGUF blob is reused, so a rebuild is cheap. The strong interview answer does not jump to a different quant or a different runtime. It asks who formatted the turns — an autodetected template, the file's own, yours, or nobody — proves it with `ollama show` and the log, and then pins the format explicitly.
- Should you write the TEMPLATE in Jinja, copied from the GGUF's `tokenizer.chat_template`?No. Ollama's `TEMPLATE` is a Go template with variables such as `.System`, `.Prompt`, `.Response` and `.Messages`; create parses it as Go and rejects invalid syntax. Translate the Jinja template's markers into Go-template form instead. Leaving `TEMPLATE` out is the way to let the GGUF's own template be used.
- The template looks right but answers still run on into a fake next user turn — what do you check?The stop list. Run `ollama show --parameters` and confirm the model's end-of-turn marker is there. A `PARAMETER stop` line added later replaces the template's whole stop list, so a custom stop can silently drop the marker that ended each turn. Restate the model's stops alongside your own and rebuild.
- A GGUF always carries a template Ollama uses unchanged
- Ollama's TEMPLATE accepts the Jinja template copied from the GGUF
- Garbage chat output means the quantization is too aggressive
- Fixing a template requires re-downloading or re-converting the GGUF
- An autodetected template is always this model's exact format
Assumes Ollama 0.35.0: autodetection on import, the fallback to the GGUF's own chat template through the bundled llama.cpp server, and the missing-template warning are as that release's source implements them.