fix: increase JSONL stream scanner buffer to prevent silent truncation#302
Open
gn00295120 wants to merge 2 commits intoanthropics:mainfrom
Open
fix: increase JSONL stream scanner buffer to prevent silent truncation#302gn00295120 wants to merge 2 commits intoanthropics:mainfrom
gn00295120 wants to merge 2 commits intoanthropics:mainfrom
Conversation
…rge lines bufio.NewScanner uses a default maximum token size of 64 KB. When a MessageBatch result line exceeds this limit, Scan() returns false and Err() returns nil — the stream silently appears empty with no error. This is inconsistent with the SSE stream decoder (ssestream.go) which sets the scanner buffer to bufio.MaxScanTokenSize<<9 (4 MB). Increase the JSONL scanner buffer to match. Add tests for small lines, large lines exceeding 64 KB, nil responses, and error propagation.
There was a problem hiding this comment.
Pull request overview
Updates the JSONL streaming decoder to handle long JSONL lines (notably MessageBatch results) by increasing the underlying bufio.Scanner buffer limit, aligning behavior with the existing SSE stream decoder.
Changes:
- Increase JSONL
bufio.Scannermax token size (currentlybufio.MaxScanTokenSize<<9) to avoid premature scan termination on >64KB lines. - Add
packages/jsonlstream tests covering small lines, a >64KB line, nil response handling, and initial error handling.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
packages/jsonl/jsonl.go |
Allocates a scanner explicitly and increases its buffer limit before constructing the JSONL Stream. |
packages/jsonl/jsonl_test.go |
Adds coverage for normal operation, large-line behavior, and basic error-path behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…l-safe Close - Update comment from "4 MB" to "32 MiB" to match the actual buffer size (bufio.MaxScanTokenSize<<9 = 32 MiB, same as SSE stream decoder) - Propagate scanner errors into s.err in Next() so Err() reflects scan failures (e.g. "bufio.Scanner: token too long") - Make Close() nil-safe by guarding s.rc before calling Close() - Add defer stream.Close() to TestStream_NilResponse and TestStream_Error Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
rishabh-emergent
approved these changes
Mar 25, 2026
|
facing the same problem of silent truncation using bedrock. |
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
The JSONL stream decoder uses
bufio.NewScannerwith its default 64 KB maximum token size. MessageBatch result lines that exceed this limit are silently dropped —Scan()returnsfalsewith anilerror, making the stream appear empty.Problem
The SSE decoder already handles this correctly with a 4 MB buffer. The JSONL decoder was missed.
Before fix — a MessageBatch response line >64 KB:
After fix:
Changes
packages/jsonl/jsonl.go: Set scanner buffer tobufio.MaxScanTokenSize<<9(4 MB), matching the SSE stream decoderpackages/jsonl/jsonl_test.go: New test file with 4 test cases (small lines, large >64KB lines, nil response, error propagation)Test plan
TestStream_LargeLineExceeding64KB— fails before fix, passes afterTestStream_SmallLines— normal operation unchangedTestStream_NilResponse— error handlingTestStream_Error— error propagationgo test ./packages/jsonl/ -v— all 4 tests pass