Skip to content

fix: use errors='replace' in TextReceiveStream to handle malformed UTF-8#615

Open
Br1an67 wants to merge 1 commit intoanthropics:mainfrom
Br1an67:fix/issue-549-utf8-decode
Open

fix: use errors='replace' in TextReceiveStream to handle malformed UTF-8#615
Br1an67 wants to merge 1 commit intoanthropics:mainfrom
Br1an67:fix/issue-549-utf8-decode

Conversation

@Br1an67
Copy link

@Br1an67 Br1an67 commented Mar 1, 2026

Summary

When MCP servers or other child processes write non-UTF-8 bytes, or when concurrent writes cause byte stream reordering, TextReceiveStream raises UnicodeDecodeError with the default strict error handler, crashing the SDK.

Fixes #549

Changes

Pass errors='replace' to TextReceiveStream for both stdout and stderr streams in SubprocessCLITransport._start_process(). This replaces malformed byte sequences with the Unicode replacement character (U+FFFD) instead of raising an exception.

Testing

  • All 160 existing tests pass
  • ruff check passes with no issues
  • The fix is minimal and backward-compatible — valid UTF-8 input is unaffected

When MCP servers or other child processes write non-UTF-8 bytes or
concurrent writes cause byte stream reordering, TextReceiveStream
raises UnicodeDecodeError with the default strict error handler.

Pass errors='replace' so that malformed byte sequences are replaced
with the Unicode replacement character instead of crashing the SDK.
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.

UTF-8 Decode Error in Claude SDK when Processing CLI stdout

1 participant