Claude Code crashes on tool output containing invalid Unicode surrogates

Resolved 💬 3 comments Opened Dec 22, 2025 by tannerpowell Closed Dec 25, 2025

Description

Claude Code freezes/crashes when a Bash tool returns output containing invalid Unicode (orphaned high surrogate characters). Instead of sanitizing or escaping the malformed data, it's passed directly to the Anthropic API, causing a JSON serialization failure.

Reproduction

  1. Run a Bash command that returns data containing malformed Unicode
  2. In my case: gh api repos/tannerpowell/villarreal/issues/9/comments --paginate
  3. The GitHub API response included ~86k+ characters with an invalid surrogate pair

Root cause: The PR comments referenced an Affinity Studio file (.af extension) - a binary file that probably shouldn't have been in the repository. The binary content likely contained byte sequences that look like orphaned Unicode surrogates when interpreted as text.

Error Message

API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"The request body is not valid JSON: no low surrogate in string: line 1 column 86475 (char 86474)"},"request_id":"req_011CWLwegHg2yNCakYXKQCKc"}

Expected Behavior

Claude Code should sanitize or escape invalid Unicode in tool output before sending to the Anthropic API. Malformed data from external sources shouldn't crash the session.

Actual Behavior

Raw tool output with invalid Unicode is passed directly to the API, causing JSON serialization failure at the Anthropic API layer. The session becomes unrecoverable.

Environment

  • Claude Code CLI
  • Model: claude-opus-4-5-20251101
  • macOS

Notes

While the binary file content shouldn't have been in the repo, Claude Code should handle malformed input gracefully rather than freezing. A defensive fix would be to sanitize tool results (e.g., replace invalid surrogates with the Unicode replacement character U+FFFD) before including them in API requests.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗