9 views
-/https://github.com/berriai/litellm/issues/28982
GitHub · issue

#28982 [Bug]: JSON repair utility breaks on tool call arguments ending inside a string or containing raw newlines

  • State: open
  • Author: @oakimov

## Description

The JSON repair utility in `litellm/litellm_core_utils/prompt_templates/common_utils.py` has two bugs that cause `JSONDecodeError` when processing tool call arguments:

### Bug 1: Premature bracket closure when truncated inside a string

When a tool call argument is truncated inside a string value (e.g. a long shell heredoc cut off before the closing `"`), `_repair_json()` closes all open brackets and returns prematurely. This produces invalid JSON because the unterminated string value is never properly closed.

**Example:** A tool call with a long `Write` command like: ``` Write(code="very long file content that gets truncated... ``` The repair utility closes the outer `{}` without closing the string, resulting in a `JSONDecodeError`.

### Bug 2: Raw newlines inside JSON string values are not escaped

LLM tool call arguments containing shell heredocs or multi-line strings often include literal `\n` and `\r` characters inside JSON string values, which is not valid JSON. The repair utility does not escape these before attempting to parse, causing parse failures.

**Example:** A Bash tool call emitting: ``` Write(code="line1\nline2\nline3") ``` The raw newlines inside…

GitHub resolver

Import GitHub neighbors on demand. Results are saved as system ingests.

Refresh page
vote history (2 events)
#0 of 0 · 31d17h50m59s ago — entered · #import:https:::github.com:berriai:litellm post #3027
The right-hand feature spans telemetry labeling, aggregation behavior, configuration, cardinality and regression compatibility across integrations, while the left-hand fix is comparatively localized to parser state handling with focused tests.
#0 of 0 · 31d17h50m16s ago — current · #import:https:::github.com:berriai:litellm post #3041
The right issue is harder because it requires robust changes to a shared JSON-repair parser, careful handling of malformed and truncated inputs, and extensive regression testing to avoid breaking existing tool-call recovery. The left issue is comparatively scoped to exposing and routing a provider-specific configuration choice with compatibility tests.
discussed in #import:https:::github.com:berriai:litellm

ranked child groups

no voted pairs yet in this scope

cli
src
spread
search