#20534: fix: handle malformed tool call JSON parse errors gracefully
agents
size: S
Cluster:
Error Handling in Agent Tools
## Problem
Fixes #7867
When a model (e.g. Venice.ai Kimi K2.5) produces malformed tool call arguments — such as concatenated JSON `{}{"path":...}` — the JSON parse `SyntaxError` is surfaced verbatim to the user via the messaging channel:
> Unexpected non-whitespace character after JSON at position 2 (line 1 column 3)
This is confusing and leaks internal implementation details.
## Solution
Add `isMalformedToolCallError()` in `formatAssistantErrorText()` to detect common JSON `SyntaxError` patterns (unexpected token, unterminated string, etc.) and rewrite them to a user-friendly message:
> The model produced a malformed tool call that couldn't be parsed. Please try again — if this persists, switch to a different model or use /new to start a fresh session.
## Related
- #16673 (centralized outbound sanitization gate)
- #9951 (context overflow errors leak)
- #18937 (API error messages leak)
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Added detection and user-friendly formatting for malformed tool call JSON parse errors. When models like Venice.ai Kimi K2.5 produce invalid JSON (e.g., concatenated objects `{}{"path":...}`), the system now shows a helpful message instead of exposing raw `SyntaxError` details to users.
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with minimal risk - it adds defensive error handling for a specific edge case
- The change is small, focused, and defensive in nature. It improves user experience by sanitizing internal error messages. The regex pattern could theoretically have false positives, but given the context (error messages from LLM APIs), this is unlikely to cause issues in practice. The only concern is the lack of tests for the new functionality.
- No files require special attention
<sub>Last reviewed commit: 04d2107</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#6464: fix: trigger model failover on malformed tool-call JSON
by ai-fanatic · 2026-02-01
80.4%
#3647: fix: sanitize tool arguments in session history
by nhangen · 2026-01-29
79.2%
#20538: fix: handle orphaned tool_result errors gracefully instead of leaki...
by echoVic · 2026-02-19
77.5%
#14098: Sanitize JSON tool-call payload text
by helloember99 · 2026-02-11
77.0%
#21873: fix: sanitize Bedrock toolUse.name in conversation history
by shan-lvr · 2026-02-20
76.4%
#14328: fix: strip incomplete tool_use blocks from errored/aborted messages...
by Kropiunig · 2026-02-12
76.4%
#19094: Fix empty tool_call_id and function names in provider transcript pa...
by yxshee · 2026-02-17
75.9%
#21195: fix: suppress orphaned tool_use/tool_result errors after session co...
by ruslansychov-git · 2026-02-19
75.8%
#9248: Fix: Webchat UI goes grey/unresponsive after Slack message tool calls
by vishaltandale00 · 2026-02-05
75.1%
#22321: fix: sanitize tool schemas for all Cloud Code Assist providers
by Sr-0w · 2026-02-21
75.1%