[PR #15466] tools: recover truncated JSON in findArguments #25703

Open
opened 2026-04-19 18:21:55 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/15466
Author: @Rih0z
Created: 4/9/2026
Status: 🔄 Open

Base: mainHead: fix/truncated-json-recovery


📝 Commits (1)

  • 43dbd31 tools: recover truncated JSON in findArguments

📊 Changes

2 files changed (+136 additions, -0 deletions)

View changed files

📝 tools/tools.go (+64 -0)
📝 tools/tools_test.go (+72 -0)

📄 Description

Summary

When a tool call's JSON arguments are truncated by token limits, findArguments returns nil because braces never balance. This adds a recovery step that finds the last complete key-value pair and closes the object.

Fixes #15465

Complements #14835 and #14915 which handle truncation in model-specific parsers. This targets the generic parser in tools/tools.go.

Changes

  • findArguments: detect truncation (braces > 0 at end of buffer) and attempt recovery
  • recoverTruncatedJSON: find last valid ", " separator, count unclosed braces, close object
  • Tests: 7 new test cases including integration test, all existing tests pass

How it works

  1. After the main parse loop, if braces > 0 && start != -1, truncation is detected
  2. recoverTruncatedJSON scans backward for the last ", " separator (key-value boundary)
  3. Counts unclosed braces to determine how many } are needed
  4. Tries to close the JSON and parse — if successful, extracts the arguments sub-object
  5. Only returns results when a valid arguments/parameters key is found in the recovered object

Testing

go test ./tools/ -v

All 42 tests pass (35 existing + 7 new), zero regressions.

Signed-off-by: Rih0z rawhi07@gmail.com


🔄 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/15466 **Author:** [@Rih0z](https://github.com/Rih0z) **Created:** 4/9/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/truncated-json-recovery` --- ### 📝 Commits (1) - [`43dbd31`](https://github.com/ollama/ollama/commit/43dbd31616f5c96971eea1eb0e71dc6d18cb38f2) tools: recover truncated JSON in findArguments ### 📊 Changes **2 files changed** (+136 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `tools/tools.go` (+64 -0) 📝 `tools/tools_test.go` (+72 -0) </details> ### 📄 Description ## Summary When a tool call's JSON arguments are truncated by token limits, `findArguments` returns nil because braces never balance. This adds a recovery step that finds the last complete key-value pair and closes the object. Fixes #15465 Complements #14835 and #14915 which handle truncation in model-specific parsers. This targets the generic parser in `tools/tools.go`. ## Changes - `findArguments`: detect truncation (`braces > 0` at end of buffer) and attempt recovery - `recoverTruncatedJSON`: find last valid `", "` separator, count unclosed braces, close object - Tests: 7 new test cases including integration test, all existing tests pass ## How it works 1. After the main parse loop, if `braces > 0 && start != -1`, truncation is detected 2. `recoverTruncatedJSON` scans backward for the last `", "` separator (key-value boundary) 3. Counts unclosed braces to determine how many `}` are needed 4. Tries to close the JSON and parse — if successful, extracts the `arguments` sub-object 5. Only returns results when a valid `arguments`/`parameters` key is found in the recovered object ## Testing ``` go test ./tools/ -v ``` All 42 tests pass (35 existing + 7 new), zero regressions. Signed-off-by: Rih0z <rawhi07@gmail.com> --- <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-19 18:21:55 -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#25703