[PR #14162] tools: fall back to raw JSON parsing when expected tag is missing #61241

Open
opened 2026-04-29 16:19:19 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/14162
Author: @JojoLesGo
Created: 2/8/2026
Status: 🔄 Open

Base: mainHead: fix/generic-parser-json-fallback


📝 Commits (1)

  • a10c4e5 tools: fall back to raw JSON parsing when expected tag is missing

📊 Changes

2 files changed (+35 additions, -3 deletions)

View changed files

📝 tools/tools.go (+18 -3)
📝 tools/tools_test.go (+17 -0)

📄 Description

Summary

Some models (e.g. qwen2.5-coder) have chat templates that instruct tool calls to be wrapped in <tool_call> tags, but the model emits bare JSON like {"name": "exec", "arguments": {"command": "ls"}} without the wrapper. The generic tools.Parser never finds the expected tag, so every token is flushed as plain-text content — the tool call is lost.
This PR adds a fallback in Parser.Add(): when findTag() fails and the accumulated content starts with {, switch p.tag to "{" and re-enter ToolCalling state. This reuses the existing raw-JSON code path (findTool / findArguments / done). If the content isn't a valid tool call, Content() returns it as regular text — safe fallback.
Changes: tools/tools.go (one method, ~15 lines)
Test: added qwen_raw_json_fallback case in tools/tools_test.go

Fixes #12174 — tool_calls missing from qwen2.5-coder
Related to #10899 — qwen2.5-coder returns empty content with tools
Related to #12064 — tool call parsing errors

Test plan

[x] All existing TestParser cases pass (33 cases)
[x] All TestDone, TestContent, TestFindTag, TestFindArguments pass
[x] New qwen_raw_json_fallback test passes
[x] End-to-end: patched binary with qwen2.5-coder:32b returns proper tool_calls via /v1/chat/completions


🔄 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/14162 **Author:** [@JojoLesGo](https://github.com/JojoLesGo) **Created:** 2/8/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/generic-parser-json-fallback` --- ### 📝 Commits (1) - [`a10c4e5`](https://github.com/ollama/ollama/commit/a10c4e5d74df762c6f1d297c34a73913ac8580b0) tools: fall back to raw JSON parsing when expected tag is missing ### 📊 Changes **2 files changed** (+35 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `tools/tools.go` (+18 -3) 📝 `tools/tools_test.go` (+17 -0) </details> ### 📄 Description ### Summary Some models (e.g. qwen2.5-coder) have chat templates that instruct tool calls to be wrapped in <tool_call> tags, but the model emits bare JSON like {"name": "exec", "arguments": {"command": "ls"}} without the wrapper. The generic tools.Parser never finds the expected tag, so every token is flushed as plain-text content — the tool call is lost. This PR adds a fallback in Parser.Add(): when findTag() fails and the accumulated content starts with {, switch p.tag to "{" and re-enter ToolCalling state. This reuses the existing raw-JSON code path (findTool / findArguments / done). If the content isn't a valid tool call, Content() returns it as regular text — safe fallback. Changes: tools/tools.go (one method, ~15 lines) Test: added qwen_raw_json_fallback case in tools/tools_test.go ### Related issues Fixes #12174 — tool_calls missing from qwen2.5-coder Related to #10899 — qwen2.5-coder returns empty content with tools Related to #12064 — tool call parsing errors ### Test plan [x] All existing TestParser cases pass (33 cases) [x] All TestDone, TestContent, TestFindTag, TestFindArguments pass [x] New qwen_raw_json_fallback test passes [x] End-to-end: patched binary with qwen2.5-coder:32b returns proper tool_calls via /v1/chat/completions --- <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-04-29 16:19:19 -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#61241