[PR #11030] [MERGED] tools: loosen tool parsing to allow for more formats #75710

Closed
opened 2026-05-05 08:07:44 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/11030
Author: @jmorganca
Created: 6/9/2025
Status: Merged
Merged: 6/12/2025
Merged by: @jmorganca

Base: mainHead: jmorganca/tools


📝 Commits (10+)

📊 Changes

27 files changed (+1153 additions, -2090 deletions)

View changed files

📝 server/routes.go (+2 -6)
tools/template.go (+156 -0)
tools/template_test.go (+139 -0)
tools/testdata/command-r-plus.gotmpl (+0 -67)
tools/testdata/command-r-plus.out (+0 -39)
tools/testdata/firefunction.gotmpl (+0 -31)
tools/testdata/firefunction.out (+0 -17)
tools/testdata/llama3-groq-tool-use.gotmpl (+0 -43)
tools/testdata/llama3-groq-tool-use.out (+0 -24)
tools/testdata/llama3.2.gotmpl (+0 -44)
tools/testdata/llama3.2.out (+0 -24)
tools/testdata/messages.json (+0 -39)
tools/testdata/mistral.gotmpl (+0 -15)
tools/testdata/mistral.out (+0 -3)
tools/testdata/nemotron.gotmpl (+0 -33)
tools/testdata/nemotron.out (+0 -18)
tools/testdata/qwen2.5.gotmpl (+0 -51)
tools/testdata/qwen2.5.out (+0 -31)
tools/testdata/qwen3.gotmpl (+0 -50)
tools/testdata/qwen3.out (+0 -31)

...and 7 more files

📄 Description

Several new models such as DeepSeek-R1-0528 output tool calls in a new format that uses special tokens to separate function names from arguments. This PRs loosens tool call parsing by using the fact that we know the names of the tools and arguments/parameters that can be provided. It also provides a path towards python tool calling (all that would remain is detecting python kwargs)

  1. First look for the tool calling tag/prefix (e.g. <tool_call>)
  2. Then look for the tool name (e.g. get_weather)
  3. Then look for an object representing the tool call arguments (e.g. {"city": "Toronto"})

Once both 2 and 3 are found, the tool call is returned from parser.Add()

I've moved away from the static templates, as they are cumbersome we only need the content in the {{ if .ToolCalls }} portion of the template, which this can be provided inline.

Follow ups:

  • Use the new encoding/json package in Go 1.25 to escape sooner if we begin parsing a json object yet it turns out to not be arguments or valid json (should improve the case where the inferred tag is { or [

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/11030 **Author:** [@jmorganca](https://github.com/jmorganca) **Created:** 6/9/2025 **Status:** ✅ Merged **Merged:** 6/12/2025 **Merged by:** [@jmorganca](https://github.com/jmorganca) **Base:** `main` ← **Head:** `jmorganca/tools` --- ### 📝 Commits (10+) - [`38d4c30`](https://github.com/ollama/ollama/commit/38d4c30297ecf3bccd574cb06728a08eb47fabd7) tools: loosen tool parsing to allow for more formats - [`03fb778`](https://github.com/ollama/ollama/commit/03fb7782efc2ca39bf31ba387a9f536d517a5d4b) fix linter - [`6e041f9`](https://github.com/ollama/ollama/commit/6e041f9296412d00c4efaa720f7e4b1ab2eac5d3) fix sending back content multiple times - [`e76184e`](https://github.com/ollama/ollama/commit/e76184ef25aa990e13f89d80aa638c3691497bd0) fix tests - [`885c235`](https://github.com/ollama/ollama/commit/885c235ee4447dbdec702f7f3091973a6e73d340) add json tests - [`454516a`](https://github.com/ollama/ollama/commit/454516a4d756726349853e4a2c691888f8538ec5) add remaining test cases - [`1ad4475`](https://github.com/ollama/ollama/commit/1ad4475f0ca632803fce7536b8b384a854c6e16b) gofmt - [`4c2b9d7`](https://github.com/ollama/ollama/commit/4c2b9d7e0debf75600bacb929a5cb77dec13b368) fix tag parsing - [`899965e`](https://github.com/ollama/ollama/commit/899965e22831b0142c5ee7c193e277aa469caaec) address comments - [`81f21ff`](https://github.com/ollama/ollama/commit/81f21ffe64c56895f839b8b213b90717a2b3348a) add comment and simplify parseTag logic ### 📊 Changes **27 files changed** (+1153 additions, -2090 deletions) <details> <summary>View changed files</summary> 📝 `server/routes.go` (+2 -6) ➕ `tools/template.go` (+156 -0) ➕ `tools/template_test.go` (+139 -0) ➖ `tools/testdata/command-r-plus.gotmpl` (+0 -67) ➖ `tools/testdata/command-r-plus.out` (+0 -39) ➖ `tools/testdata/firefunction.gotmpl` (+0 -31) ➖ `tools/testdata/firefunction.out` (+0 -17) ➖ `tools/testdata/llama3-groq-tool-use.gotmpl` (+0 -43) ➖ `tools/testdata/llama3-groq-tool-use.out` (+0 -24) ➖ `tools/testdata/llama3.2.gotmpl` (+0 -44) ➖ `tools/testdata/llama3.2.out` (+0 -24) ➖ `tools/testdata/messages.json` (+0 -39) ➖ `tools/testdata/mistral.gotmpl` (+0 -15) ➖ `tools/testdata/mistral.out` (+0 -3) ➖ `tools/testdata/nemotron.gotmpl` (+0 -33) ➖ `tools/testdata/nemotron.out` (+0 -18) ➖ `tools/testdata/qwen2.5.gotmpl` (+0 -51) ➖ `tools/testdata/qwen2.5.out` (+0 -31) ➖ `tools/testdata/qwen3.gotmpl` (+0 -50) ➖ `tools/testdata/qwen3.out` (+0 -31) _...and 7 more files_ </details> ### 📄 Description Several new models such as DeepSeek-R1-0528 output tool calls in a new format that uses special tokens to separate function names from arguments. This PRs loosens tool call parsing by using the fact that we know the names of the tools and arguments/parameters that can be provided. It also provides a path towards python tool calling (all that would remain is detecting python kwargs) 1. First look for the tool calling tag/prefix (e.g. `<tool_call>`) 2. Then look for the tool name (e.g. `get_weather`) 3. Then look for an object representing the tool call arguments (e.g. `{"city": "Toronto"}`) Once both 2 and 3 are found, the tool call is returned from `parser.Add()` I've moved away from the static templates, as they are cumbersome we only need the content in the `{{ if .ToolCalls }}` portion of the template, which this can be provided inline. Follow ups: - [ ] Use the new `encoding/json` package in Go 1.25 to escape sooner if we begin parsing a json object yet it turns out to not be arguments or valid json (should improve the case where the inferred tag is `{` or `[` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-05-05 08:07:44 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#75710