Skip to content

[Bug]: LLM tool call JSON parsing failures - suggest jsonrepair fallback #9916

Description

@AlexGn

Summary

When LLMs (particularly KIMI K2) generate tool call arguments, they occasionally produce malformed JSON that causes JSON.parse() to fail. The tool call never reaches the skill handler, and the LLM falls back to providing (often incorrect) manual analysis.

Error Message

Unterminated string in JSON at position 26 (line 1 column 27)

Root Cause

LLMs generate JSON probabilistically. Common failure modes:

  • Truncated strings (missing closing quote)
  • Unescaped special characters
  • Missing commas between properties
  • Incomplete objects (missing closing brace)

The error occurs in the gateway's tool call argument parsing, before the skill handler is invoked.

Real-World Impact

User Input:

SKR (Long)
Entry: 0.022351
SL: 0.019364
Target: 0.036531

What Happened:

  1. KIMI K2 attempted to call tool with malformed JSON
  2. JSON.parse() threw error at position 26
  3. Skill handler never executed
  4. LLM said: "Tool call failed" then provided incorrect fallback analysis
  5. On retry (user said "fix it"), tool call succeeded

Proposed Solution

Add jsonrepair as a fallback before failing tool calls:

import { jsonrepair } from 'jsonrepair';

function parseToolArgs(raw: string): object {
  try {
    return JSON.parse(raw);
  } catch (e) {
    // Attempt repair for common LLM mistakes
    try {
      const repaired = jsonrepair(raw);
      return JSON.parse(repaired);
    } catch {
      throw e; // Re-throw original error if repair fails
    }
  }
}

jsonrepair handles:

  • Missing quotes around property names
  • Trailing commas
  • Unescaped special characters
  • Missing closing brackets/braces
  • Truncated strings

Alternative Mitigations (client-side)

We've already implemented:

  1. Simplified tool schemas (shorter descriptions = fewer tokens = less error)
  2. Input sanitization layer in skill handler
  3. System prompt instructions for JSON formatting

But these don't help when the JSON is so malformed that parsing fails entirely.

Environment

  • Clawdbot version: Latest
  • Model: nvidia/moonshotai/kimi-k2-instruct (KIMI K2)
  • Error frequency: ~5-10% of tool calls
  • Workaround: User retries the message

Files Affected

Gateway tool call argument parsing (exact location in clawdbot source unknown to me)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleMarked as stale due to inactivity

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions