litellm - 💡(How to fix) Fix [Bug]: JSON repair utility breaks on tool call arguments ending inside a string or containing raw newlines [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…

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:

Root Cause

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.

Fix Action

Fixed

Code Example

Write(code="very long file content that gets truncated...

---

Write(code="line1\nline2\nline3")
RAW_BUFFERClick to expand / collapse

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 the quoted string cause the JSON parser to fail.

Proposed fix

For Bug 1: Check in_string state before returning from the bracket-repair loop, and close any open string value with a ".

For Bug 2: Add _escape_raw_newlines_in_json() that walks the string character-by-character and escapes \n/\r to \\n/\\r only when inside a quoted string.

Related to #25985 "JSONDecodeError Unterminated string when parsing tool call arguments" (partially overlapping).

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

litellm - 💡(How to fix) Fix [Bug]: JSON repair utility breaks on tool call arguments ending inside a string or containing raw newlines [1 pull requests]