[GH-ISSUE #10949] docs: improve documentation for templates in relation to tools #84895

Open
opened 2026-05-09 21:59:21 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @Saw-mon-and-Natalie on GitHub (Jun 2, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/10949

Originally assigned to: @ParthSareen on GitHub.

What is the issue?

Currently ollama has the following assumptions about the tool calling for the model's template:

  1. tools/tools_utils.go#L42-L56: The model template for tool calls needs to use .ToolCalls explicitly:
{{-if .ToolCalls }} SOME_TEXT ... {{- end }}

For example the following would not work:

{{- tc := .ToolCalls }}
{{-if $tc }} SOME_TEXT ... {{- end }}

This is not a big issue, was one can use the pattern above. But it should be documented for template generation.

  1. tools/tools_utils.go#L117-L120: The tool templates would need to start with the following pattern:
{{- range .ToolCalls }} ... {{- end }}

and a again a more custom patterns like:

{{- $tc := .ToolCalls }}
{{- range $tc }} ... {{- end }}

would cause failures. Again this is not a big issue but at least it should be documented.

  1. tools/tools_utils.go#L169-L173, tools/tools_utils.go#L189-L196: It is assumed that all model templates should follow this pattern, where inside the range node of the tool calls the parsed string is a json parsed string. No other extra tokens are allowed:
{{- range .ToolCalls }}
{ "name": "SOME_STRING", "arguments": {...}}
{{- end }}

Here is where it gets tricky. Some models such as DeepSeek-R1-0528-Qwen3-8B expect special tokens before and after the json parsed tool call:

{{- range .ToolCalls -}}
    <toolcallbegin>
        {{- json .Function -}}
    <toolcallend>
{{- end -}}

So the above tool template would accuse an error during template parsing (tools/tools.go#L242). This is some assumption that should not be there. Since it does not apply to all model templates.

DeepSeek-R1-0528-Qwen3-8B has the following model template for tool calls based on the converted template from the original jinja template on HuggingFace model card:

<toolcallbegin> 
{{- .Function.Type -}}<toolsep> {{- /* <-- .Function does not have a `Type` key currently as well */ -}}
{{- .Function.Name }}
```json
{{json .Function.arguments}}
```
<toolcallend>"

related:

Originally created by @Saw-mon-and-Natalie on GitHub (Jun 2, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/10949 Originally assigned to: @ParthSareen on GitHub. ### What is the issue? Currently `ollama` has the following assumptions about the tool calling for the model's template: 1. [`tools/tools_utils.go#L42-L56`](https://github.com/ollama/ollama/blob/aaa7818000c42a82fc030212c35ef83f9799efd7/tools/tools_utils.go#L42-L56): The model template for tool calls needs to use `.ToolCalls` explicitly: ```go {{-if .ToolCalls }} SOME_TEXT ... {{- end }} ``` For example the following would not work: ```go {{- tc := .ToolCalls }} {{-if $tc }} SOME_TEXT ... {{- end }} ``` This is not a big issue, was one can use the pattern above. But it should be documented for template generation. 2. [`tools/tools_utils.go#L117-L120`](https://github.com/ollama/ollama/blob/aaa7818000c42a82fc030212c35ef83f9799efd7/tools/tools_utils.go#L117-L120): The tool templates would need to start with the following pattern: ```go {{- range .ToolCalls }} ... {{- end }} ``` and a again a more custom patterns like: ```go {{- $tc := .ToolCalls }} {{- range $tc }} ... {{- end }} ``` would cause failures. Again this is not a big issue but at least it should be documented. 3. [tools/tools_utils.go#L169-L173](https://github.com/ollama/ollama/blob/aaa7818000c42a82fc030212c35ef83f9799efd7/tools/tools_utils.go#L169-L173), [`tools/tools_utils.go#L189-L196`](https://github.com/ollama/ollama/blob/aaa7818000c42a82fc030212c35ef83f9799efd7/tools/tools_utils.go#L189-L196): It is assumed that all model templates should follow this pattern, where inside the `range` node of the tool calls the parsed string is a `json` parsed string. No other extra tokens are allowed: ```go {{- range .ToolCalls }} { "name": "SOME_STRING", "arguments": {...}} {{- end }} ``` Here is where it gets tricky. Some models such as `DeepSeek-R1-0528-Qwen3-8B` expect special tokens before and after the `json` parsed tool call: ```go {{- range .ToolCalls -}} <|tool▁call▁begin|> {{- json .Function -}} <|tool▁call▁end|> {{- end -}} ``` So the above tool template would accuse an error during template parsing ([`tools/tools.go#L242`](https://github.com/ollama/ollama/blob/aaa7818000c42a82fc030212c35ef83f9799efd7/tools/tools.go#L242)). This is some assumption that should not be there. Since it does not apply to all model templates. `DeepSeek-R1-0528-Qwen3-8B` has the following model template for tool calls based on the converted template from [the original `jinja` template on HuggingFace model card](https://huggingface.co/deepseek-ai/DeepSeek-R1-0528-Qwen3-8B?chat_template=default&format=true): ````go <|tool▁call▁begin|> {{- .Function.Type -}}<|tool▁sep|> {{- /* <-- .Function does not have a `Type` key currently as well */ -}} {{- .Function.Name }} ```json {{json .Function.arguments}} ``` <|tool▁call▁end|>" ```` related: - https://github.com/ollama/ollama/pull/10415 - https://github.com/ollama/ollama/issues/10897
GiteaMirror added the bug label 2026-05-09 21:59:21 -05:00
Author
Owner

@rick-github commented on GitHub (Jun 2, 2025):

https://github.com/ollama/ollama/pull/10917

<!-- gh-comment-id:2930428607 --> @rick-github commented on GitHub (Jun 2, 2025): https://github.com/ollama/ollama/pull/10917
Author
Owner

@ParthSareen commented on GitHub (Jun 2, 2025):

Yes we should improve the documentation and I'll tag the issue as such. They're givens and not assumptions at the stage of the parser.

<!-- gh-comment-id:2931923671 --> @ParthSareen commented on GitHub (Jun 2, 2025): Yes we should improve the documentation and I'll tag the issue as such. They're givens and not assumptions at the stage of the parser.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#84895