<!-- 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-14 · ~4168 tokens -->

# Ollama Modelfile Go templates

> TEMPLATE of the full prompt template to be passed into the model. It may include (optionally) a system message, a user’s message and the response from the model. Note: syntax may be model specific. Templates use Go template syntax. (from docs.ollama.com)

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

## Definitions

- TEMPLATE of the full prompt template to be passed into the model. It may include (optionally) a system message, a user’s message and the response from the model. Note: syntax may be model specific. Templates use Go template syntax. — [source](https://docs.ollama.com/modelfile)
- A Modelfile is the blueprint to create and share customized models using Ollama. — [source](https://docs.ollama.com/modelfile)

## Structure and components

- * A basic messages structure (layout) * Three variables: `Messages`, `Role`, and `Content` (variables) * A custom function (action) that iterates over an array of items (`range .Messages`) and displays each item — [source](https://docs.ollama.com/template.md#basic-template-structure)
- | Variable | Description | | - | - | | `{{ .System }}` | The system message used to specify custom behavior. | | `{{ .Prompt }}` | The user prompt message. | | `{{ .Response }}` | The response from the model. When generating a response, text after this variable is omitted. | — [source](https://docs.ollama.com/modelfile#template-variables)
- `Messages[].ToolCalls[].Function.Name` (string): function name — [source](https://docs.ollama.com/template.md#variables)
- `Messages[].ToolCalls[].Function.Arguments` (map): mapping of argument name to argument value — [source](https://docs.ollama.com/template.md#variables)
- | Instruction | Description | | - | - | | [`FROM`](#from-required) (required) | Defines the base model to use. | | [`PARAMETER`](#parameter) | Sets the parameters for how Ollama will run the model. | | [`TEMPLATE`](#template) | The full prompt template to be sent to the model. | | [`SYSTEM`](#system) | Specifies the system message that will be set in the template. | | [`LICENSE`](#license) | Specifies the legal license. | | [`MESSAGE`](#message) | Specify message history. | | [`REQUIRES`](#requires) | Specify the minimum version of Ollama required by the model. | | [`CAPABILITY`](#capability) — [source](https://docs.ollama.com/modelfile#format)
- type Values struct { Messages []api.Message api.Tools Prompt string Suffix string Think bool // ThinkLevel contains the thinking level if Think is true and a string value was provided ThinkLevel string // whether or not the user explicitly set the thinking flag (vs. it being // implicitly false). Templates can't see whether `Think` is nil IsThinkSet bool — [source](https://github.com/ollama/ollama/blob/main/template/template.go)
- `Messages[].ToolCalls` (list): list of tools the model wants to call — [source](https://docs.ollama.com/template.md#variables)
- `Messages[].ToolCalls[].Function` (object): function to call — [source](https://docs.ollama.com/template.md#variables)
- `Tools[].Function.Name` (string): function name — [source](https://docs.ollama.com/template.md#variables)

## How it works

- By default, models imported into Ollama have a default template of `{{ .Prompt }}`, i.e. user inputs are sent verbatim to the LLM. This is appropriate for text or code completion models but lacks essential markers for chat or instruction models. — [source](https://docs.ollama.com/template.md#adding-templates-to-your-model)
- if flagsSet == 1 { switch showType { case "license": fmt.Println(resp.License) case "modelfile": fmt.Println(resp.Modelfile) case "parameters": fmt.Println(resp.Parameters) case "system": fmt.Print(resp.System) case "template": fmt.Print(resp.Template) } — [source](https://github.com/ollama/ollama/blob/main/cmd/cmd.go)
- if !slices.Contains(vars, "messages") && !slices.Contains(vars, "response") { // touch up the template and append {{ .Response }} tmpl.Tree.Root.Nodes = append(tmpl.Tree.Root.Nodes, &response) } — [source](https://github.com/ollama/ollama/blob/main/template/template.go)
- func (t *Template) Execute(w io.Writer, v Values) error { system, messages := collate(v.Messages) vars, err := t.Vars() if err != nil { return err } if v.Prompt != "" && v.Suffix != "" { return t.Template.Execute(w, map[string]any{ "Prompt": v.Prompt, "Suffix": v.Suffix, "Response": "", "Think": v.Think, "ThinkLevel": v.ThinkLevel, "IsThinkSet": v.IsThinkSet, }) } else if !v.forceLegacy && slices.Contains(vars, "messages") { return t.Template.Execute(w, map[string]any{ "System": system, "Messages": convertMessagesForTemplate(messages), "Tools": convertToolsForTemplate — [source](https://github.com/ollama/ollama/blob/main/template/template.go)

## Parameters and configuration

- showCmd.Flags().Bool("license", false, "Show license of a model") showCmd.Flags().Bool("modelfile", false, "Show Modelfile of a model") showCmd.Flags().Bool("parameters", false, "Show parameters of a model") showCmd.Flags().Bool("template", false, "Show template of a model") showCmd.Flags().Bool("system", false, "Show system message of a model") showCmd.Flags().BoolP("verbose", "v", false, "Show detailed model information") — [source](https://github.com/ollama/ollama/blob/main/cmd/cmd.go)
- var funcs = template.FuncMap{ "json": func(v any) string { b, _ := json.Marshal(v) return string(b) }, "currentDate": func(args ...string) string { // Currently ignoring the format argument, but accepting it for future use // Default format is YYYY-MM-DD return time.Now().Format("2006-01-02") }, "yesterdayDate": func(args ...string) string { return time.Now().AddDate(0, 0, -1).Format("2006-01-02") }, "toTypeScriptType": func(v any) string { if param, ok := v.(api.ToolProperty); ok { return param.ToTypeScriptType() } // Handle pointer case if param, ok := v.(*api.ToolProperty); ok && param != n — [source](https://github.com/ollama/ollama/blob/main/template/template.go)
- The GGUF file location should be specified as an absolute path or relative to the Modelfile location. For a split GGUF model, keep the original split filenames and use a wildcard that matches every shard. Shards can also be listed with separate FROM instructions. — [source](https://docs.ollama.com/modelfile)
- | Parameter | Description | Value Type | Example Usage | | - | - | - | - | | num\_ctx | Sets the size of the context window used to generate the next token. (Default: 2048) | int | num\_ctx 4096 | | repeat\_last\_n | Sets how far back for the model to look back to prevent repetition. (Default: 64, 0 = disabled, -1 = num\_ctx) | int | repeat\_last\_n 64 | | repeat\_penalty | Sets how strongly to penalize repetitions. A higher value (e.g., 1.5) will penalize repetitions more strongly, while a lower value (e.g., 0.9) will be more lenient. (Default: 1.0, disabled) | float | repeat\_penalty 1.1 | | — [source](https://docs.ollama.com/modelfile#valid-parameters-and-values)

## How-to and procedures

- Fill-in-middle support can be added to a model by adding a `{{ .Suffix }}` node to the template. This feature is useful for models that are trained to generate text in the middle of user input, such as code completion models. — [source](https://docs.ollama.com/template.md#example-fill-in-middle)
- Just thought i'd put this here. Here's a template ollama modelfile that allows you to use this GGUF with Ollama with working tool calls (calling GLM-4.7 Flash from Cline, roo code, kilo code, etc) — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)
- Tools support can be added to a model by adding a `{{ .Tools }}` node to the template. This feature is useful for models trained to call external tools and can a powerful tool for retrieving real-time data or performing complex tasks. — [source](https://docs.ollama.com/template.md#example-tools)
- To add templates in your model, you'll need to add a `TEMPLATE` command to the Modelfile. Here's an example using Meta's Llama 3. — [source](https://docs.ollama.com/template.md#adding-templates-to-your-model)
- To view the Modelfile of a given model, use the ollama show --modelfile command. — [source](https://docs.ollama.com/modelfile)
- First, create a `Modelfile` with a `FROM` command which points to the directory containing your Safetensors weights: — [source](https://docs.ollama.com/import#importing-a-model-from-safetensors-weights)
- If you create the Modelfile in the same directory as the weights, you can use the command `FROM .`. — [source](https://docs.ollama.com/import#importing-a-model-from-safetensors-weights)
- To import a single-file GGUF model, create a `Modelfile` containing: — [source](https://docs.ollama.com/import#importing-a-gguf-model)

## Examples and snippets

- TEMPLATE """ {{- $lastUserIdx := -1 -}} {{- range $idx, $msg := .Messages -}} {{- if eq $msg.Role "user" }}{{ $lastUserIdx = $idx }}{{ end -}} {{- end }} {{- if .System }}<|im_start|>system {{ .System }}<|im_end|> {{ end }} {{- range $i, $_ := .Messages }} {{- $last := eq (len (slice $.Messages $i)) 1 -}} {{- if eq .Role "user" }}<|im_start|>user {{ .Content }}<|im_end|> {{ else if eq .Role "assistant" }}<|im_start|>assistant {{ if .Content }}{{ .Content }} {{- else if .ToolCalls }} {{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}} {{ end }} {{- end — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)
- ``` — `# Modelfile generated by "ollama show"` … — [source](https://docs.ollama.com/modelfile#basic-modelfile)
- ```go theme={"system"} — `{{- range $index, $_ := .Messages }}` … — [source](https://docs.ollama.com/template.md#mistral)
- For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags: <tool_call> {"name": <function-name>, "arguments": <args-json-object>} </tool_call> {{- end }} {{- end }} {{- range $i, $_ := .Messages }} {{- $last := eq (len (slice $.Messages $i)) 1 -}} {{- if eq .Role "user" }}<|user|> {{ .Content }} {{- else if eq .Role "assistant" }}<|assistant|> {{- if .Content }}{{ .Content }}{{- end }} {{- if .ToolCalls }}<tool_call> {{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}} {{ end }}</tool_call> {{ — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)
- ```dockerfile theme={"system"} — `FROM llama3.2` … — [source](https://docs.ollama.com/template.md#adding-templates-to-your-model)
- TEMPLATE """[gMASK]<sop>{{- if .Messages }} {{- if or .System .Tools }}<|system|> {{- if .System }} {{ .System }} {{- end }} {{- if .Tools }} — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)
- ```go theme={"system"} — `<PRE> {{ .Prompt }} <SUF>{{ .Suffix }} <MID>` … — [source](https://docs.ollama.com/template.md#codellama)
- ```gotmpl theme={"system"} — `[SUFFIX]{{ .Suffix }}[PREFIX] {{ .Prompt }}` … — [source](https://docs.ollama.com/template.md#codestral)
- ```go theme={"system"} — `{{- range .Messages }}<|im_start|>{{ .Role }}` … — [source](https://docs.ollama.com/template.md#chatml)
- {{ .Response }}<|eot_id|>""" PARAMETER stop "<|start_header_id|>" PARAMETER stop "<|end_header_id|>" PARAMETER stop "<|eot_id|>" PARAMETER stop "<|reserved_special_token" — [source](https://docs.ollama.com/modelfile)
- ```gotmpl theme={"system"} — `{{- range .Messages }}` … — [source](https://docs.ollama.com/template.md#basic-template-structure)
- TEMPLATE """{{ if .System }}<|im_start|>system {{ .System }}<|im_end|> {{ end }}{{ if .Prompt }}<|im_start|>user {{ .Prompt }}<|im_end|> {{ end }}<|im_start|>assistant """ — [source](https://docs.ollama.com/modelfile)
- RENDERER glm-4.7 PARSER glm-4.7 — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)
- You are provided with function signatures within <tools></tools> XML tags: <tools> {{- range .Tools }} {"type": "function", "function": {{ .Function }}} {{- end }} </tools> — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)
- PARAMETER temperature 0.1 PARAMETER top_p 0.9 PARAMETER num_ctx 65536 PARAMETER stop "<|im_start|>" PARAMETER stop "<|im_end|>" PARAMETER repeat_penalty 1.05 PARAMETER top_k 20 PARAMETER min_p 0.05 — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)
- ```shell theme={"system"} — `ollama show --modelfile llama3.2` … — [source](https://docs.ollama.com/modelfile#basic-modelfile)
- TEMPLATE """[gMASK]<sop><|system|> {{ .System }}<|user|> {{ .Prompt }}<|assistant|> {{ .Response }}""" — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)
- this.fimSupported = !!body?.template?.includes(".Suffix"); — [source](https://gitea.varghacsongor.hu/GithubMirror/continue/commit/16a540c3378b833931429db9bcb3ce7c98bd7a36)
- this.templateSupportsTools = body.template.includes(".Tools"); — [source](https://gitea.varghacsongor.hu/GithubMirror/continue/commit/16a540c3378b833931429db9bcb3ce7c98bd7a36)
- ``` — `FROM llama3.2` … — [source](https://docs.ollama.com/modelfile#basic-modelfile)
- PARAMETER stop "<|user|>" PARAMETER stop "<|observation|>" PARAMETER stop "<|system|>" — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)
- {{ .Content }} <|im_end|> {{ end }} {{- if and (ne .Role "assistant") $last }}<|im_start|>assistant {{ end -}} {{- end }}""" — [source](https://huggingface.co/unsloth/GLM-4.7-Flash-GGUF/discussions/23)

## Problems, failure modes and limitations

- The Ollama provider checks the model template from /api/show for a `.Tools` placeholder to decide whether to pass tools to the API. For newer models (e.g. qwen3.5) whose templates may not yet include `.Tools`, this silently strips tools from the request — even when the user explicitly sets `capabilities: tools: true` in config.yaml. — [source](https://gitea.varghacsongor.hu/GithubMirror/continue/commit/16a540c3378b833931429db9bcb3ce7c98bd7a36)
- <Note> CodeLlama 34B and 70B code completion and all instruct and Python fine-tuned models do not support fill-in-middle. </Note> — [source](https://docs.ollama.com/template.md#codellama)

## Changes and history

- * fix: let explicit capabilities.tools override Ollama template tool detection — [source](https://gitea.varghacsongor.hu/GithubMirror/continue/commit/16a540c3378b833931429db9bcb3ce7c98bd7a36)

## Facts and statements

- Show information about a model including details, modelfile, template, parameters, license, system prompt. — [source](https://github.com/ollama/ollama/blob/main/docs/api.md#show-model-information)
- CodeLlama [7B](https://ollama.com/library/codellama:7b-code) and [13B](https://ollama.com/library/codellama:13b-code) code completion models support fill-in-middle. — [source](https://docs.ollama.com/template.md#codellama)
- Codestral [22B](https://ollama.com/library/codestral:22b) supports fill-in-middle. — [source](https://docs.ollama.com/template.md#codestral)
- the Modelfile is not case sensitive. In the examples, uppercase instructions are used to make it easier to distinguish it from arguments. — [source](https://docs.ollama.com/modelfile)
- var DefaultTemplate, _ = Parse("{{ .Prompt }}") — [source](https://github.com/ollama/ollama/blob/main/template/template.go)

## Related concepts

- template — is a related of Ollama Modelfile Go templates
- {{ .Prompt }} — is a part of Ollama Modelfile Go templates
- .Messages — is a part of Ollama Modelfile Go templates
- role marker — is a part of Ollama Modelfile Go templates
- {{ range }} — is a part of Ollama Modelfile Go templates
- .ToolCalls — is a part of Ollama Modelfile Go templates
- .Tools — is a part of Ollama Modelfile Go templates
- fill-in-middle — is a part of Ollama Modelfile Go templates
- Function.Arguments — is a part of Ollama Modelfile Go templates
- .Suffix — is a part of Ollama Modelfile Go templates
- stop token — is a part of Ollama Modelfile Go templates
- ollama show --modelfile — is a measure of Ollama Modelfile Go templates
- prompt template — is a synonym of Ollama Modelfile Go templates
- GLM-4.7 — is a instance of Ollama Modelfile Go templates
- ThinkLevel — is a part of Ollama Modelfile Go templates
- template variable — is a part of Ollama Modelfile Go templates
- RENDERER — is a part of Ollama Modelfile Go templates
- PARSER — is a part of Ollama Modelfile Go templates
- tool calling — is a related of Ollama Modelfile Go templates
- ollama show --template — is a measure of Ollama Modelfile Go templates
