fix(jsonl): do not treat a lone CR as a line ending - #1778
Open
hsusul wants to merge 1 commit into
Open
Conversation
Batch results are streamed with small iter_bytes chunks, so a CRLF terminator can split across chunk boundaries. Finalizing on `\r` alone left a leftover `\n` that caused JSONDecodeError.
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
JSONLDecoder/AsyncJSONLDecoderpreviously treated a lone\ras a complete line ending.iter_byteschunks (batch results usechunk_size=64), that left a leftover\nwhich was parsed as an empty JSONL row and raisedJSONDecodeError.\n(stripping a preceding\rfor CRLF), matching JSON Lines and the TypeScript SDK’sLineDecoderbehavior.Problem
messages.batches.results/beta.messages.batches.resultsstream JSONL via:The previous decoder finalized a line whenever the buffer ended with
\r,\n, or\r\n. If a chunk ended at\rand the next began with\n, the decoder:\r(OK for that object)\nas another line →json.JSONDecodeError: Expecting valueIntact
\r\nin a single chunk already worked; only the split case failed.Minimal reproduction
Current behavior
Split CRLF across chunks raises
JSONDecodeErrorwhile decoding batch results.Corrected behavior
CRLF is treated as one terminator even when
\rand\narrive in separate chunks. Sync and async decoders behave the same.Root cause
bytes.splitlines(keepends=True)+buf.endswith((b"\r", b"\n", b"\r\n"))treated a dangling CR as end-of-line. JSON Lines uses\nas the line separator (with\r\nalso valid); a lone CR must stay buffered until\narrives (or EOF flush).Implementation
\n.\rfrom each line (CRLF).\rbefore parsing any remaining bytes.JSONLDecoderandAsyncJSONLDecoder.This is intentionally small: no public API changes, no dependency changes, and no generated resource changes.
_decoders/jsonl.pyis a Stainless-persisted module; the change may need re-application across regenerations, same as prior jsonl fixes in this repo.Why this approach is minimal
LineDecoder(defer trailing CR) without pulling httpx internals into this path.Sync and async coverage
Both
JSONLDecoderandAsyncJSONLDecoderwere updated and tested.Regression tests
Added in
tests/decoders/test_jsonl.py(sync + async for each):Validation
Pre-fix (expected failures):
Post-fix:
No live Anthropic API calls were made.
Generated-code considerations
Edited
src/anthropic/_decoders/jsonl.py(persisted across Stainless generations; may conflict on regen). Tests undertests/decoders/are hand-maintained.Compatibility
\n-terminated JSONL (unchanged).\r-only line endings (non-JSONL) are no longer treated as terminators mid-stream; JSON Lines requires\n/\r\n.Limitations
./scripts/testwas run for Python 3.9 (min) with both Pydantic major versions; focused JSONL tests were also run on Python 3.14.