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:
- KIMI K2 attempted to call tool with malformed JSON
JSON.parse() threw error at position 26
- Skill handler never executed
- LLM said: "Tool call failed" then provided incorrect fallback analysis
- 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:
- Simplified tool schemas (shorter descriptions = fewer tokens = less error)
- Input sanitization layer in skill handler
- 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)
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
Root Cause
LLMs generate JSON probabilistically. Common failure modes:
The error occurs in the gateway's tool call argument parsing, before the skill handler is invoked.
Real-World Impact
User Input:
What Happened:
JSON.parse()threw error at position 26Proposed Solution
Add
jsonrepairas a fallback before failing tool calls:jsonrepair handles:
Alternative Mitigations (client-side)
We've already implemented:
But these don't help when the JSON is so malformed that parsing fails entirely.
Environment
nvidia/moonshotai/kimi-k2-instruct(KIMI K2)Files Affected
Gateway tool call argument parsing (exact location in clawdbot source unknown to me)