<!-- llms-explorer concept facts · https://llms-explorer.com/tree/ollama-modelfile-template-authoring-in-go-templates-for-chat-and-tool-calling-models/ · pack 2026-10-15 · ~2093 tokens -->

# Ollama Modelfile TEMPLATE authoring in Go templates for chat and tool-calling models

> Source-anchored research for Ollama Modelfile TEMPLATE authoring in Go templates for chat and tool-calling models. Run status: COMPLETED.

Parent: [Ollama templates and scale-out](https://llms-explorer.com/tree/ollama-templates-and-scale-out/) · 7 facets · 17 facts · page: https://llms-explorer.com/tree/ollama-modelfile-template-authoring-in-go-templates-for-chat-and-tool-calling-models/

## Definitions

- TEMPLATE is the Modelfile instruction for the full prompt template sent to the model; it uses Go template syntax and is model specific. Imported models default to {{ .Prompt }}, which sends user input unchanged and lacks chat or instruction markers. (high confidence) — [source](https://docs.ollama.com/template.md)

## Structure and components

- Inside range and with blocks the meaning of . changes, so use $. to reach root variables. Use {{- and -}} to trim leading and trailing whitespace, which matters because stray whitespace becomes part of the prompt. (medium confidence) — [source](https://docs.ollama.com/template.md)

## How it works

- A working community tool template (GLM-4.7-Flash, v2) adds a Tool Instructions section to the system block when tools exist, lists function signatures in <tools> tags, asks for JSON in <tool_call> tags, re-renders past assistant tool calls as <tool_call> blocks, and returns tool results under an <|observation|> role in <tool_response> tags. Its v1 template had no tools block and gave no tool definitions to the model. (low confidence) (verified as of 2026-02-09) — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)
- `ollama show --modelfile <model>` is the alternative when you also need the FROM blob path and the stop PARAMETERs; copy the shipped TEMPLATE from either command as the starting point for a custom template, then edit it. (medium confidence) — [source](https://github.com/ollama/ollama/blob/main/cmd/cmd.go)
- When a request carries both a Prompt and a Suffix, Ollama renders the template with only Prompt, Suffix, an empty Response and the thinking flags, which is the fill-in-middle path. A fill-in-middle template therefore must not depend on System or Messages. (low confidence) (verified as of 2026-10-14) — [source](https://github.com/ollama/ollama/blob/main/template/template.go)

## How-to and procedures

- To enable tool support, include a {{ .Tools }} node in the template. It only works for models trained to call tools. The documented Mistral example renders tools as JSON inside [AVAILABLE_TOOLS], wraps model calls in [TOOL_CALLS] and results in [TOOL_RESULTS]. (high confidence) — [source](https://docs.ollama.com/template.md)

## Problems, failure modes and limitations

- Templates can read thinking state: Think (bool), ThinkLevel (string, set only when Think is true and a level was given) and IsThinkSet (true when the caller set the flag explicitly; a template cannot otherwise tell unset from false). These are in the template package source; the Template guide on main does not list them. (medium confidence) (verified as of 2026-10-14) — [source](https://github.com/ollama/ollama/blob/main/template/template.go)
- The template package also ships a built-in index of known chat templates (Named, matched by Levenshtein distance; a score of 100 or more means no match and returns the error "no matching template found"). Do not rely on it for a custom model: supply TEMPLATE explicitly. (low confidence) (verified as of 2026-10-14) — [source](https://github.com/ollama/ollama/blob/main/template/template.go)

## Comparisons and alternatives

- Beyond the data variables, Ollama registers four template functions in the Go template: `json` (marshal any value to a JSON string, used for tool lists and arguments), `currentDate` and `yesterdayDate` (format YYYY-MM-DD; any format argument is accepted but ignored), and `toTypeScriptType` (renders a tool property as a TypeScript type). Templates are parsed with missingkey=zero, so a reference to an absent variable renders as the zero value instead of failing. (medium confidence) (verified as of 2026-10-14) — [source](https://github.com/ollama/ollama/blob/main/template/template.go)

## Facts and statements

- Template variables: legacy System, Prompt, Response, Suffix; and Messages with .Role (system, user, assistant, tool), .Content and .ToolCalls (.Function.Name, .Function.Arguments as a map). Tools lists available tools with .Type (always function), .Function.Name, .Description and .Parameters (Type always object, Required list, Properties map with Type, Description, Enum). (high confidence) — [source](https://docs.ollama.com/template.md)
- The Modelfile reference documents the System, Prompt and Response variables in its TEMPLATE table. Text after {{ .Response }} is omitted during generation. The richer Messages, Tools and Suffix variables are documented on the separate Template page (docs/template.mdx). Checked against the repository docs on main at Ollama v0.40.4 (2026-10-12). (medium confidence) (verified as of 2026-10-14) — [source](https://github.com/ollama/ollama/blob/main/docs/modelfile.mdx)
- Stop tokens must match the template's role markers. The GLM v2 Modelfile sets stop for <|user|>, <|observation|> and <|system|>, and the Llama 3 reference Modelfile sets stops for <|start_header_id|>, <|end_header_id|> and <|eot_id|>. Without them the model can run on into a fabricated next turn. (medium confidence) — [source](https://docs.ollama.com/modelfile)
- Fill-in-middle is enabled by a {{ .Suffix }} node (CodeLlama: <PRE> {{ .Prompt }} <SUF>{{ .Suffix }} <MID>; Codestral: [SUFFIX]{{ .Suffix }}[PREFIX] {{ .Prompt }}). It does not work with CodeLlama 34B/70B code models or any CodeLlama instruct or Python variants. (low confidence) — [source](https://docs.ollama.com/template.md)
- `ollama show` has a dedicated `--template` flag ("Show template of a model"), alongside `--modelfile`, `--license`, `--parameters`, `--system` and `-v/--verbose`. `ollama show --template <model>` prints only the model's shipped template (the server response's Template field, written with fmt.Print). Only one of --license, --modelfile, --parameters, --system or --template may be given per call; combining them fails with "only one of '--license', '--modelfile', '--parameters', '--system', or '--template' can be specified". Verified in cmd/cmd.go on main at Ollama v0.40.4 (2026-10-12). The CLI reference page (docs.ollama.com/cli) does not list the show flags, so the source (or `ollama show --help`) is the authority. The REST endpoint behind it (POST /api/show) returns a template field next to modelfile, parameters, license and system. (high confidence) (verified as of 2026-10-14) — [source](https://github.com/ollama/ollama/blob/main/cmd/cmd.go)
- Some clients inferred tool support by checking the model template for .Tools; Continue removed that check on 2026-03-26 (templateSupportsTools = body.template.includes(".Tools")) to trust upstream detection. A custom template lacking .Tools could therefore have tools stripped in older Continue builds but not newer ones. (low confidence) (verified as of 2026-03-26) — [source](https://gitea.varghacsongor.hu/GithubMirror/continue/commit/16a540c3378b833931429db9bcb3ce7c98bd7a36)
- Community Modelfiles for newer models (GLM-4.7) also set RENDERER glm-4.7 and PARSER glm-4.7 alongside a custom TEMPLATE. The repository Modelfile reference (docs/modelfile.mdx on main, Ollama v0.40.4 (2026-10-12)) does not document RENDERER, PARSER or a think setting, and the Template guide does not mention them either, so treat these directives as undocumented. (low confidence) (verified as of 2026-10-14) — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)
- Ollama picks the rendering path from the variables your template references. If the template uses .Messages, it receives System, Messages, Tools, Response (empty) and the thinking flags in one render. If it does not, Ollama falls back to the legacy path and renders once per user/assistant turn pair with System, Prompt and Response. A template that references neither messages nor response gets {{ .Response }} appended automatically. (low confidence) (verified as of 2026-10-14) — [source](https://github.com/ollama/ollama/blob/main/template/template.go)
