Skip to content

fix(stream): handle invalid JSON escape sequences in MessageStream#1007

Open
xodn348 wants to merge 1 commit intoanthropics:mainfrom
xodn348:fix/996-json-parse-crash-messagestream
Open

fix(stream): handle invalid JSON escape sequences in MessageStream#1007
xodn348 wants to merge 1 commit intoanthropics:mainfrom
xodn348:fix/996-json-parse-crash-messagestream

Conversation

@xodn348
Copy link
Copy Markdown

@xodn348 xodn348 commented Apr 17, 2026

Summary

Fixes #996

When the Anthropic API streams tool-use deltas containing invalid JSON escape sequences (e.g. \d, \p, \s from regex patterns), partialParse() throws an unhandled error that crashes the entire MessageStream. This is a runtime crash with no way for consumers to recover gracefully.

Change

Wrapped the partialParse(jsonBuf) call in #accumulateMessage with a try-catch that silently skips the delta on parse failure. The stream continues processing subsequent events, and the final complete JSON (which uses proper \\d escaping) parses correctly.

Before:

newContent.input = partialParse(jsonBuf);

After:

try {
  newContent.input = partialParse(jsonBuf);
} catch {
  // invalid escape sequences (e.g. \d, \p, \s) — skip this delta, stream continues
}

This is the minimal surgical fix — one file changed, 5 lines added, 1 removed.

Verification

  • tsc --noEmit passes (EXIT_CODE=0)
  • npm test passes (641 passed, 5 failed pre-existing Nock connection errors in ToolRunnerE2E unrelated to this change)

@xodn348 xodn348 requested a review from a team as a code owner April 17, 2026 02:32
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.

bug(streaming): JSON.parse crashes on invalid escape sequences in model output

1 participant