Skip to content

fix: handle invalid JSON escape sequences in partial-json-parser#989

Open
JosephDoUrden wants to merge 1 commit intoanthropics:mainfrom
JosephDoUrden:fix/partial-json-invalid-escapes
Open

fix: handle invalid JSON escape sequences in partial-json-parser#989
JosephDoUrden wants to merge 1 commit intoanthropics:mainfrom
JosephDoUrden:fix/partial-json-invalid-escapes

Conversation

@JosephDoUrden
Copy link
Copy Markdown

Summary

Fix partialParse crashing on invalid JSON escape sequences (e.g. \H, \M from PHP namespaces like App\Http\Middleware) by repairing them in the generate() function before JSON.parse().

Fixes #986

Problem

When a model emits tool call JSON containing unescaped backslashes (e.g. PHP namespaces), partialParse throws Bad escaped character in JSON, crashing MessageStream during input_json_delta accumulation and killing the entire streaming response.

The tokeniser correctly captures escape sequences like \H, but generate() outputs them verbatim into a JSON string. JSON.parse() then rejects them since only \" \\ \/ \b \f \n \r \t \uXXXX are valid JSON escapes.

Fix

In generate(), sanitise string token values by doubling backslashes before invalid escape characters:

token.value.replace(/\\([^"\\\/bfnrtu])/g, '\\\\$1')

This turns \H into \\H, \M into \\M, etc., producing valid JSON.

Changes

  • src/_vendor/partial-json-parser/parser.ts: Sanitise invalid escapes in generate()
  • tests/lib/partial-json.test.ts: Added tests for invalid escape sequences and mixed valid/invalid escapes

Test plan

  • All 19 partial-json tests pass (17 existing + 2 new)
  • New test: PHP namespace backslashes (App\Http\Middleware) parse correctly
  • New test: Mixed valid (\n) and invalid (\H) escapes in same string

Repair invalid JSON escape sequences (e.g. \H, \M from PHP namespaces
like App\Http\Middleware) in the generate() function by doubling
backslashes before characters that are not valid JSON escapes.

Previously, the tokeniser correctly captured these sequences but
generate() output them verbatim, causing JSON.parse() to throw
"Bad escaped character" and crashing MessageStream during
input_json_delta accumulation.

Fixes anthropics#986
@JosephDoUrden JosephDoUrden requested a review from a team as a code owner April 10, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

partialParse in _vendor/partial-json-parser crashes on invalid JSON escape sequences from model output

1 participant