Ollama Modelfile TEMPLATE authoring in Go templates for chat and tool-calling models
Parent: Ollama templates and scale-out · Published reference · snapshot 2026-10-14 · skill ollama-expert/references/ollama-templates-and-scale-out.md
Also known as: Ollama Modelfile Go templates
↓ Facts as markdownall context files
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)
These notes link each claim to its source. Some citations identify archived research reports that are no longer hosted on this site. A published reference means the content is available; it does not certify independent review or accuracy.Read the editorial policy and follow the sources before relying on a claim.
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]
- A Modelfile is the blueprint to create and share customized models using Ollama. [source]
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]
- | 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]
- `Messages[].ToolCalls[].Function.Name` (string): function name [source]
- `Messages[].ToolCalls[].Function.Arguments` (map): mapping of argument name to argument value [source]
- | 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]
- 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]
- `Messages[].ToolCalls` (list): list of tools the model wants to call [source]
- `Messages[].ToolCalls[].Function` (object): function to call [source]
- `Tools[].Function.Name` (string): function name [source]
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]
- 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]
- 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]
- 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]
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]
- 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]
- 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]
- | 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]
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]
- 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]
- 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]
- 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]
- To view the Modelfile of a given model, use the ollama show --modelfile command. [source]
- First, create a `Modelfile` with a `FROM` command which points to the directory containing your Safetensors weights: [source]
- If you create the Modelfile in the same directory as the weights, you can use the command `FROM .`. [source]
- To import a single-file GGUF model, create a `Modelfile` containing: [source]
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]
- ``` — `# Modelfile generated by "ollama show"` … [source]
- ```go theme={"system"} — `{{- range $index, $_ := .Messages }}` … [source]
- 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]
- ```dockerfile theme={"system"} — `FROM llama3.2` … [source]
- TEMPLATE """[gMASK]<sop>{{- if .Messages }} {{- if or .System .Tools }}<|system|> {{- if .System }} {{ .System }} {{- end }} {{- if .Tools }} [source]
- ```go theme={"system"} — `<PRE> {{ .Prompt }} <SUF>{{ .Suffix }} <MID>` … [source]
- ```gotmpl theme={"system"} — `[SUFFIX]{{ .Suffix }}[PREFIX] {{ .Prompt }}` … [source]
- ```go theme={"system"} — `{{- range .Messages }}<|im_start|>{{ .Role }}` … [source]
- {{ .Response }}<|eot_id|>""" PARAMETER stop "<|start_header_id|>" PARAMETER stop "<|end_header_id|>" PARAMETER stop "<|eot_id|>" PARAMETER stop "<|reserved_special_token" [source]
- ```gotmpl theme={"system"} — `{{- range .Messages }}` … [source]
- TEMPLATE """{{ if .System }}<|im_start|>system {{ .System }}<|im_end|> {{ end }}{{ if .Prompt }}<|im_start|>user {{ .Prompt }}<|im_end|> {{ end }}<|im_start|>assistant """ [source]
- RENDERER glm-4.7 PARSER glm-4.7 [source]
- You are provided with function signatures within <tools></tools> XML tags: <tools> {{- range .Tools }} {"type": "function", "function": {{ .Function }}} {{- end }} </tools> [source]
- 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]
- ```shell theme={"system"} — `ollama show --modelfile llama3.2` … [source]
- TEMPLATE """[gMASK]<sop><|system|> {{ .System }}<|user|> {{ .Prompt }}<|assistant|> {{ .Response }}""" [source]
- this.fimSupported = !!body?.template?.includes(".Suffix"); [source]
- this.templateSupportsTools = body.template.includes(".Tools"); [source]
- ``` — `FROM llama3.2` … [source]
- PARAMETER stop "<|user|>" PARAMETER stop "<|observation|>" PARAMETER stop "<|system|>" [source]
- {{ .Content }} <|im_end|> {{ end }} {{- if and (ne .Role "assistant") $last }}<|im_start|>assistant {{ end -}} {{- end }}""" [source]
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]
- <Note> CodeLlama 34B and 70B code completion and all instruct and Python fine-tuned models do not support fill-in-middle. </Note> [source]
Changes and history
- * fix: let explicit capabilities.tools override Ollama template tool detection [source]
Facts and statements
- Show information about a model including details, modelfile, template, parameters, license, system prompt. [source]
- 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]
- Codestral [22B](https://ollama.com/library/codestral:22b) supports fill-in-middle. [source]
- the Modelfile is not case sensitive. In the examples, uppercase instructions are used to make it easier to distinguish it from arguments. [source]
- var DefaultTemplate, _ = Parse("{{ .Prompt }}") [source]
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
Children
- No children recorded.