fix: handle invalid JSON escape sequences in partial-json-parser#989
Open
JosephDoUrden wants to merge 1 commit intoanthropics:mainfrom
Open
fix: handle invalid JSON escape sequences in partial-json-parser#989JosephDoUrden wants to merge 1 commit intoanthropics:mainfrom
JosephDoUrden wants to merge 1 commit intoanthropics:mainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix
partialParsecrashing on invalid JSON escape sequences (e.g.\H,\Mfrom PHP namespaces likeApp\Http\Middleware) by repairing them in thegenerate()function beforeJSON.parse().Fixes #986
Problem
When a model emits tool call JSON containing unescaped backslashes (e.g. PHP namespaces),
partialParsethrowsBad escaped character in JSON, crashingMessageStreamduringinput_json_deltaaccumulation and killing the entire streaming response.The tokeniser correctly captures escape sequences like
\H, butgenerate()outputs them verbatim into a JSON string.JSON.parse()then rejects them since only\" \\ \/ \b \f \n \r \t \uXXXXare valid JSON escapes.Fix
In
generate(), sanitise string token values by doubling backslashes before invalid escape characters:This turns
\Hinto\\H,\Minto\\M, etc., producing valid JSON.Changes
src/_vendor/partial-json-parser/parser.ts: Sanitise invalid escapes ingenerate()tests/lib/partial-json.test.ts: Added tests for invalid escape sequences and mixed valid/invalid escapesTest plan
App\Http\Middleware) parse correctly\n) and invalid (\H) escapes in same string