ollama - ✅(Solved) Fix fix: recover truncated tool call JSON when max_tokens cuts output [1 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Fix Action

Fixed

PR fix notes

PR #15466: tools: recover truncated JSON in findArguments

Description (problem / solution / changelog)

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 [email protected]

Changed files

  • tools/tools.go (modified, +64/-0)
  • tools/tools_test.go (modified, +72/-0)

Code Example

curl http://localhost:11434/api/chat -d '{
  "model": "qwen3:8b",
  "messages": [{"role": "user", "content": "Write a complete HTML dashboard page"}],
  "tools": [{"type": "function", "function": {"name": "write_file", "parameters": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}}}}}],
  "options": {"num_predict": 2048}
}'
RAW_BUFFERClick to expand / collapse

Problem

When a model generates a large tool call (e.g., write_file with HTML content), the JSON arguments can be truncated if the output hits the token limit. The tool call is silently dropped instead of returning partial arguments.

This causes missing tool calls when:

  • write_file produces large HTML/code content
  • Qwen3/Gemma4 models hit their output token limit mid-JSON

Proposed Fix

Add recovery for truncated tool call JSON so that at least the successfully parsed key-value pairs are returned rather than dropping the entire call.

Reproduction

curl http://localhost:11434/api/chat -d '{
  "model": "qwen3:8b",
  "messages": [{"role": "user", "content": "Write a complete HTML dashboard page"}],
  "tools": [{"type": "function", "function": {"name": "write_file", "parameters": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}}}}}],
  "options": {"num_predict": 2048}
}'

Tool call JSON gets truncated, no tool_calls in response.

Note: PRs #14835/#14915 fix this for model-specific parsers (Qwen3, etc.). This fix targets the generic tools/tools.go parser used when no model-specific parser is available.

extent analysis

TL;DR

Implementing a recovery mechanism for truncated tool call JSON in the generic tools/tools.go parser will allow for the return of successfully parsed key-value pairs instead of dropping the entire call.

Guidance

  • Identify the token limit that is causing the JSON arguments to be truncated and consider increasing it if possible.
  • Modify the tools/tools.go parser to handle truncated JSON by parsing and returning the successfully parsed key-value pairs.
  • Test the recovery mechanism with the provided reproduction script to ensure it works as expected.
  • Review PRs #14835 and #14915 for model-specific parser fixes to ensure consistency across all parsers.

Example

// Example of how to parse truncated JSON and return successfully parsed key-value pairs
func parseToolCallJSON(jsonString string) (map[string]string, error) {
    // Attempt to parse the JSON string
    var jsonData map[string]string
    err := json.Unmarshal([]byte(jsonString), &jsonData)
    if err != nil {
        // If parsing fails, try to parse individual key-value pairs
        pairs := make(map[string]string)
        // ... implement parsing of individual key-value pairs ...
        return pairs, nil
    }
    return jsonData, nil
}

Notes

This solution assumes that the token limit is the primary cause of the issue and that increasing it or implementing a recovery mechanism will resolve the problem. However, the root cause may be more complex, and additional debugging may be necessary.

Recommendation

Apply workaround by implementing a recovery mechanism for truncated tool call JSON in the generic tools/tools.go parser, as this will allow for the return of successfully parsed key-value pairs instead of dropping the entire call.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING