-
-
Notifications
You must be signed in to change notification settings - Fork 69.5k
[Bug] Raw JSON parse error surfaced to user when Anthropic SSE stream contains control characters #14321
Copy link
Copy link
Closed as not planned
Closed as not planned
Copy link
Labels
bugSomething isn't workingSomething isn't workingstaleMarked as stale due to inactivityMarked as stale due to inactivity
Description
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
- Use
claude-opus-4-6(streaming) in a coding-heavy session - Request code generation or file editing that may include control characters
- 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:
- Retry the request automatically (the error is transient — next attempt usually succeeds)
- Sanitize control characters from SSE data before
JSON.parse()(defense-in-depth, since the upstream SDK should also fix this) - 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
- Upstream SDK issue: U+2028 LINE SEPARATOR in MCP tool results causes JSON parsing failures. anthropics/anthropic-sdk-typescript#882 (U+2028 LINE SEPARATOR causes same class of JSON parse failure)
- OpenClaw Session corruption from invalid Unicode surrogates in browser tool output #7275 (similar: invalid Unicode surrogates in browser tool output)
- OpenClaw [Bug]: Calling opus4.6 resulted in an error, the JSON format could not be parsed and was truncated. #10507 (similar: Opus 4.6 JSON parse/truncation)
- OpenClaw Malformed tool call error leaked verbatim to user instead of being handled gracefully #7867 (similar: malformed tool call JSON leaked to user)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingstaleMarked as stale due to inactivityMarked as stale due to inactivity
Type
Fields
Give feedbackNo fields configured for issues without a type.