Skip to content

[Bug] Raw JSON parse error surfaced to user when Anthropic SSE stream contains control characters #14321

@dyoung522

Description

@dyoung522

Description

When the Anthropic streaming API sends SSE data: lines containing unescaped control characters (C0 range U+0000–U+001F), the @anthropic-ai/sdk streaming parser throws:

Bad control character in string literal in JSON at position N (line 1 column N+1)

This error is surfaced verbatim to the user as a message in the chat, with no retry or graceful recovery. The response is lost and marked stopReason: "error".

Steps to Reproduce

  1. Use claude-opus-4-6 (streaming) in a coding-heavy session
  2. Request code generation or file editing that may include control characters
  3. Intermittently, a response will fail with the above error

Observed Behavior

  • User sees raw JSON parse error as a chat message
  • Response is lost (stopReason: "error")
  • Session recovers on the next user message (no persistent corruption)
  • Position in error varies (122, 131, 134, 156, etc.)

Expected Behavior

Either:

  1. Retry the request automatically (the error is transient — next attempt usually succeeds)
  2. Sanitize control characters from SSE data before JSON.parse() (defense-in-depth, since the upstream SDK should also fix this)
  3. At minimum, show a user-friendly error like "Response interrupted, please try again" instead of the raw JSON parse error

Environment

  • OpenClaw: 2026.2.9
  • @anthropic-ai/sdk: 0.73.0
  • Model: claude-opus-4-6 (streaming)
  • Node.js: 22.x

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleMarked as stale due to inactivity

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions