[PR #14835] parsers: handle truncated and invalid tool calls gracefully in qwen parsers #46114

Open
opened 2026-04-25 01:38:47 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/14835
Author: @MunemHashmi
Created: 3/13/2026
Status: 🔄 Open

Base: mainHead: fix/parsers-truncated-tool-call-error


📝 Commits (1)

  • d033c88 parsers: handle truncated and invalid tool calls gracefully in qwen parsers

📊 Changes

5 files changed (+319 additions, -4 deletions)

View changed files

📝 model/parsers/qwen3.go (+17 -2)
📝 model/parsers/qwen35_test.go (+46 -0)
📝 model/parsers/qwen3_test.go (+145 -0)
📝 model/parsers/qwen3coder.go (+12 -2)
📝 model/parsers/qwen3coder_test.go (+99 -0)

📄 Description

Summary

  • Fixes truncated tool calls (e.g. from num_predict limits) causing a 500 error by converting tool call parse failures into content fallback instead of returning a fatal error
  • Adds buffer drain on done=true so partial tool calls and trailing whitespace are flushed as content rather than silently discarded
  • Applies to Qwen3Parser (qwen3, qwen3-thinking) and Qwen3CoderParser (qwen3-coder, qwen3.5)

Closes #14570

Changes

model/parsers/qwen3.go

  • Changed parseQwen3ToolCall error handling from return "", "", nil, err to writing event.raw as content and continuing — prevents 500 responses on malformed JSON
  • Added state-aware buffer drain when done=true: flushes remaining buffer as content (when collecting content/tool) or thinking (when collecting thinking)

model/parsers/qwen3coder.go

  • Same error fallback fix for parseToolCall failures
  • Added buffer drain when done=true: flushes remaining buffer as content (this parser has no thinking support)

Tests

  • 15 new tests across qwen3_test.go, qwen3coder_test.go, and qwen35_test.go
  • Covers: truncated tool calls (single chunk + streaming), invalid JSON/XML, valid-after-invalid recovery, buffer drain on done, partial tags at end of stream

Test plan

  • go test ./model/parsers/ -count=1 — all tests pass
  • go vet ./model/parsers/ — no issues
  • Verify with a qwen3 model that truncated tool calls no longer produce 500 errors
  • Verify that valid tool calls continue to work correctly

🔄 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/14835 **Author:** [@MunemHashmi](https://github.com/MunemHashmi) **Created:** 3/13/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/parsers-truncated-tool-call-error` --- ### 📝 Commits (1) - [`d033c88`](https://github.com/ollama/ollama/commit/d033c88d2f6d73caced1c1b4b2fcf8ce0e60d4de) parsers: handle truncated and invalid tool calls gracefully in qwen parsers ### 📊 Changes **5 files changed** (+319 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `model/parsers/qwen3.go` (+17 -2) 📝 `model/parsers/qwen35_test.go` (+46 -0) 📝 `model/parsers/qwen3_test.go` (+145 -0) 📝 `model/parsers/qwen3coder.go` (+12 -2) 📝 `model/parsers/qwen3coder_test.go` (+99 -0) </details> ### 📄 Description ## Summary - Fixes truncated tool calls (e.g. from `num_predict` limits) causing a 500 error by converting tool call parse failures into content fallback instead of returning a fatal error - Adds buffer drain on `done=true` so partial tool calls and trailing whitespace are flushed as content rather than silently discarded - Applies to `Qwen3Parser` (qwen3, qwen3-thinking) and `Qwen3CoderParser` (qwen3-coder, qwen3.5) Closes #14570 ## Changes ### `model/parsers/qwen3.go` - Changed `parseQwen3ToolCall` error handling from `return "", "", nil, err` to writing `event.raw` as content and continuing — prevents 500 responses on malformed JSON - Added state-aware buffer drain when `done=true`: flushes remaining buffer as content (when collecting content/tool) or thinking (when collecting thinking) ### `model/parsers/qwen3coder.go` - Same error fallback fix for `parseToolCall` failures - Added buffer drain when `done=true`: flushes remaining buffer as content (this parser has no thinking support) ### Tests - 15 new tests across `qwen3_test.go`, `qwen3coder_test.go`, and `qwen35_test.go` - Covers: truncated tool calls (single chunk + streaming), invalid JSON/XML, valid-after-invalid recovery, buffer drain on done, partial tags at end of stream ## Test plan - [x] `go test ./model/parsers/ -count=1` — all tests pass - [x] `go vet ./model/parsers/` — no issues - [x] Verify with a qwen3 model that truncated tool calls no longer produce 500 errors - [x] Verify that valid tool calls continue to work correctly --- <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-25 01:38:47 -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#46114