Skip to content

fix: increase JSONL stream scanner buffer to prevent silent truncation#302

Open
gn00295120 wants to merge 2 commits intoanthropics:mainfrom
gn00295120:fix/jsonl-scanner-buffer-limit
Open

fix: increase JSONL stream scanner buffer to prevent silent truncation#302
gn00295120 wants to merge 2 commits intoanthropics:mainfrom
gn00295120:fix/jsonl-scanner-buffer-limit

Conversation

@gn00295120
Copy link
Copy Markdown

Summary

The JSONL stream decoder uses bufio.NewScanner with its default 64 KB maximum token size. MessageBatch result lines that exceed this limit are silently dropped — Scan() returns false with a nil error, making the stream appear empty.

Problem

// packages/jsonl/jsonl.go — default 64 KB limit
scn: bufio.NewScanner(res.Body),
// packages/ssestream/ssestream.go — explicitly set to 4 MB
scn.Buffer(nil, bufio.MaxScanTokenSize<<9)

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:

=== RUN   TestStream_LargeLineExceeding64KB
    jsonl_test.go:60: expected stream.Next() to return true for large line, got false; err: <nil>
--- FAIL

After fix:

=== RUN   TestStream_LargeLineExceeding64KB
--- PASS

Changes

  • packages/jsonl/jsonl.go: Set scanner buffer to bufio.MaxScanTokenSize<<9 (4 MB), matching the SSE stream decoder
  • packages/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 after
  • TestStream_SmallLines — normal operation unchanged
  • TestStream_NilResponse — error handling
  • TestStream_Error — error propagation
  • go test ./packages/jsonl/ -v — all 4 tests pass

…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.
Copilot AI review requested due to automatic review settings March 23, 2026 14:14
@gn00295120 gn00295120 requested a review from a team as a code owner March 23, 2026 14:14
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Scanner max token size (currently bufio.MaxScanTokenSize<<9) to avoid premature scan termination on >64KB lines.
  • Add packages/jsonl stream 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
Copy link
Copy Markdown

facing the same problem of silent truncation using bedrock.

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.

3 participants