[PR #14180] harmony: strip trailing control tokens before parsing tool call arguments #45802

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

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/14180
Author: @lucidbard
Created: 2/10/2026
Status: 🔄 Open

Base: mainHead: fix/harmony-tool-call-parsing


📝 Commits (4)

  • 4a6b57a harmony: strip trailing control tokens before parsing tool call arguments
  • 86765a9 harmony: use json.Decoder for safer tool call arg parsing
  • 9c49618 harmony: add truncated JSON repair with caller-visible metadata
  • 6e7d178 harmony: remove json.Decoder, keep only brace repair for observed failures

📊 Changes

3 files changed (+205 additions, -3 deletions)

View changed files

📝 api/types.go (+7 -0)
📝 harmony/harmonyparser.go (+57 -3)
📝 harmony/harmonyparser_test.go (+141 -0)

📄 Description

Smaller models (e.g. gpt-oss:20b) frequently fail to emit all closing braces before EOS when generating deeply nested tool call JSON, causing json.Unmarshal to fail and returning an error to the caller.

This adds:

  1. A brace-repair fallback: if json.Unmarshal fails, count unmatched {/} (respecting string literals and escape sequences), append missing closers, and retry
  2. A repaired field on ToolCall: when repair succeeds, includes the original raw output and a description of the fix (e.g. closed_braces:2), giving callers full context to decide whether to trust the repair or retry
  3. Empty args default to {}

In testing with complex OpenAPI schemas against gpt-oss:20b, this takes tool call success from ~32% to ~90%. gpt-oss:120b is unaffected (100% clean output).

Related: #11991, #12187, #12203


🔄 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/14180 **Author:** [@lucidbard](https://github.com/lucidbard) **Created:** 2/10/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/harmony-tool-call-parsing` --- ### 📝 Commits (4) - [`4a6b57a`](https://github.com/ollama/ollama/commit/4a6b57ab2ccf2bc258650cbef542c5b07f6d9ad6) harmony: strip trailing control tokens before parsing tool call arguments - [`86765a9`](https://github.com/ollama/ollama/commit/86765a94d906191399a37e791c7d05508276ebbc) harmony: use json.Decoder for safer tool call arg parsing - [`9c49618`](https://github.com/ollama/ollama/commit/9c49618a5a3c459b7725f7b9d5ed35a81d112c4d) harmony: add truncated JSON repair with caller-visible metadata - [`6e7d178`](https://github.com/ollama/ollama/commit/6e7d178f2bcc2287c851779c851685471725e9d4) harmony: remove json.Decoder, keep only brace repair for observed failures ### 📊 Changes **3 files changed** (+205 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `api/types.go` (+7 -0) 📝 `harmony/harmonyparser.go` (+57 -3) 📝 `harmony/harmonyparser_test.go` (+141 -0) </details> ### 📄 Description Smaller models (e.g. gpt-oss:20b) frequently fail to emit all closing braces before EOS when generating deeply nested tool call JSON, causing json.Unmarshal to fail and returning an error to the caller. This adds: 1. A brace-repair fallback: if json.Unmarshal fails, count unmatched `{`/`}` (respecting string literals and escape sequences), append missing closers, and retry 2. A `repaired` field on ToolCall: when repair succeeds, includes the original raw output and a description of the fix (e.g. `closed_braces:2`), giving callers full context to decide whether to trust the repair or retry 3. Empty args default to `{}` In testing with complex OpenAPI schemas against gpt-oss:20b, this takes tool call success from ~32% to ~90%. gpt-oss:120b is unaffected (100% clean output). Related: #11991, #12187, #12203 --- <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:26:13 -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#45802