Skip to content

fix(partial-json): merge consecutive string tokens in generate#971

Open
ysnows wants to merge 1 commit intoanthropics:mainfrom
ysnows:fix/partial-json-consecutive-strings
Open

fix(partial-json): merge consecutive string tokens in generate#971
ysnows wants to merge 1 commit intoanthropics:mainfrom
ysnows:fix/partial-json-consecutive-strings

Conversation

@ysnows
Copy link
Copy Markdown

@ysnows ysnows commented Apr 2, 2026

Summary

  • When the partial JSON tokenizer produces consecutive string tokens (e.g., from strings containing escaped quotes that get split across token boundaries), the generate function now merges them with an escaped quote instead of producing invalid JSON with adjacent quoted strings like "hello""world".

Test plan

  • Verify partialParse('{"key": "hello"}') still works correctly
  • Verify partial JSON with escaped quotes handles consecutive string tokens

🤖 Generated with Claude Code

When the tokenizer produces consecutive string tokens (e.g., from
strings containing escaped quotes that get split across token
boundaries), the generate function now merges them with an escaped
quote instead of producing invalid JSON with adjacent quoted strings.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ysnows ysnows requested a review from a team as a code owner April 2, 2026 14:14
rbwsam added a commit to rbwsam/pi-mono that referenced this pull request Apr 6, 2026
Switch from client.messages.stream() to client.messages.create() to avoid
the Anthropic SDK's unguarded partialParse() call in MessageStream, which
crashes with 'Expected , or } after property value in JSON' or 'Bad control
character in string literal' when tool input JSON contains control characters
or triggers the vendored partial-json tokenizer's consecutive string token bug.

Pi already accumulates tool arguments with its own parseStreamingJson() which
handles all these cases gracefully. The SDK's MessageStream accumulation was
redundant and its partialParse crash killed the entire stream before pi ever
saw the event. The raw Stream from .create() yields identical SSE events
without the buggy accumulation layer.

Upstream: anthropics/anthropic-sdk-typescript#971 (open, unmerged)
Related: anthropics/anthropic-sdk-typescript#882
Related: anthropics/anthropic-sdk-python#1265
rbwsam added a commit to rbwsam/pi-mono that referenced this pull request Apr 9, 2026
Switch from client.messages.stream() to client.messages.create() to avoid
the Anthropic SDK's unguarded partialParse() call in MessageStream, which
crashes with 'Expected , or } after property value in JSON' or 'Bad control
character in string literal' when tool input JSON contains control characters
or triggers the vendored partial-json tokenizer's consecutive string token bug.

Pi already accumulates tool arguments with its own parseStreamingJson() which
handles all these cases gracefully. The SDK's MessageStream accumulation was
redundant and its partialParse crash killed the entire stream before pi ever
saw the event. The raw Stream from .create() yields identical SSE events
without the buggy accumulation layer.

Upstream: anthropics/anthropic-sdk-typescript#971 (open, unmerged)
Related: anthropics/anthropic-sdk-typescript#882
Related: anthropics/anthropic-sdk-python#1265
rbwsam added a commit to rbwsam/pi-mono that referenced this pull request Apr 15, 2026
Switch from client.messages.stream() to client.messages.create() to avoid
the Anthropic SDK's unguarded partialParse() call in MessageStream, which
crashes with 'Expected , or } after property value in JSON' or 'Bad control
character in string literal' when tool input JSON contains control characters
or triggers the vendored partial-json tokenizer's consecutive string token bug.

Pi already accumulates tool arguments with its own parseStreamingJson() which
handles all these cases gracefully. The SDK's MessageStream accumulation was
redundant and its partialParse crash killed the entire stream before pi ever
saw the event. The raw Stream from .create() yields identical SSE events
without the buggy accumulation layer.

Upstream: anthropics/anthropic-sdk-typescript#971 (open, unmerged)
Related: anthropics/anthropic-sdk-typescript#882
Related: anthropics/anthropic-sdk-python#1265
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.

1 participant