Implementation:Ollama Ollama Tools Template
| Knowledge Sources | |
|---|---|
| Domains | Tool Calling, Template Analysis |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Extracts tool calling tags from Go chat templates by analyzing the template AST for .ToolCalls field references and their surrounding text nodes.
Description
parseTag finds the tool calling delimiter by searching the template AST for an IfNode whose pipeline references "ToolCalls", then finds the first non-whitespace TextNode within that node's body. The tag text is trimmed at the first "{" character to separate the tag prefix from JSON content. If no tag is found, returns "{" as a fallback to trigger JSON-based tool call parsing. findToolCallNode recursively searches through nested IfNode, ListNode, RangeNode, and WithNode structures. findTextNode performs depth-first search for the first meaningful text content.
Usage
Called during response parsing to detect when the model begins outputting tool calls, by matching the template-specific tag prefix in the streaming output.
Code Reference
Source Location
- Repository: Ollama
- File: tools/template.go
- Lines: 1-156
Signature
func parseTag(tmpl *template.Template) string
func findToolCallNode(nodes []parse.Node) *parse.IfNode
func findTextNode(nodes []parse.Node) *parse.TextNode
Import
import "github.com/ollama/ollama/tools"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| tmpl | *template.Template | Yes | Parsed Go template from the model's chat template |
Outputs
| Name | Type | Description |
|---|---|---|
| tag | string | Tool call tag prefix (e.g. "<tool_call>", "[TOOL_CALL]", or "{") |
Usage Examples
tmpl, _ := template.New("chat").Parse(`
{{range .Messages}}
{{if .ToolCalls}}<tool_call>
{{range .ToolCalls}}{"name":"{{.Function.Name}}"}{{end}}
</tool_call>{{end}}
{{end}}
`)
tag := tools.parseTag(tmpl)
// tag: "<tool_call>"
// For templates without explicit tags:
// tag: "{" (parse any JSON as potential tool call)