fix: recover from non-JSON lines in stdout stream#588
Open
esensible wants to merge 1 commit intoanthropics:mainfrom
Open
fix: recover from non-JSON lines in stdout stream#588esensible wants to merge 1 commit intoanthropics:mainfrom
esensible wants to merge 1 commit intoanthropics:mainfrom
Conversation
When non-JSON lines appear on stdout (e.g. verbose HTTP logs, sandbox
debug messages, mTLS configuration output), the existing json_buffer
accumulation logic permanently poisons the buffer. Once any non-JSON
content enters the buffer, all subsequent json.loads() calls fail,
causing the SDK to silently drop every remaining message. In practice
this manifests as query() returning incomplete results or the client
hanging indefinitely.
Root cause: _read_messages_impl unconditionally appends every stdout
line to json_buffer. A non-JSON line like "2026-02-19 [DEBUG] ..."
gets concatenated with the next valid JSON object, producing
unparseable content that persists for the lifetime of the stream.
Fix: try standalone json.loads() first (covers the common case of a
complete JSON object on a single line). Only start buffering if the
line starts with "{" (the stream-json protocol emits JSON objects).
Non-JSON lines are logged at DEBUG level and skipped.
Fixes anthropics#347
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When non-JSON lines appear on stdout (e.g. verbose HTTP logs, sandbox debug messages, mTLS configuration output), the
json_bufferaccumulation logic in_read_messages_implpermanently poisons the buffer. Once any non-JSON contententers the buffer, all subsequent
json.loads()calls fail, causing the SDK to silently drop every remaining message.In practice this manifests as
query()returning incomplete results or the client hanging indefinitely._read_messages_implunconditionally appends every stdout line tojson_buffer. A non-JSON line like2026-02-19 [DEBUG] configureGlobalMTLS startinggets concatenated with the next valid JSON object, producingunparseable content that persists for the lifetime of the stream.
json.loads()first (covers the common case of a complete JSON object on a single line). Only start buffering if the line starts with{(the stream-json protocol emits JSON objects). Non-JSON lines are loggedat DEBUG level and skipped.
Fixes #347
Test plan
test_non_json_lines_dont_poison_buffer— simulates non-JSON log lines interleaved with valid JSON messagestest_non_json_lines_between_split_json— verifies non-JSON lines before a split JSON object don't corrupt the buffer🤖 Generated with Claude Code