Skip to content

fix(go-models): raise SSE scanner buffer so large stream chunks are not dropped - #15382

Merged
JinHai-CN merged 1 commit into
infiniflow:mainfrom
Dexterity104:fix/go-models-sse-scanner-buffer
May 29, 2026
Merged

JinHai-CN merged 1 commit into
infiniflow:mainfrom
Dexterity104:fix/go-models-sse-scanner-buffer

Conversation

@Dexterity104

@Dexterity104 Dexterity104 commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #15381

Every provider in internal/entity/models/ reads its streaming response with bufio.NewScanner(resp.Body) and iterates over scanner.Scan(). The default bufio.Scanner maximum token size is 64KB, so when an upstream sends a single SSE data: line larger than 64KB (long content deltas, large tool or function call argument blobs, bundled reasoning_content, or providers that emit a whole message in one event) scanner.Scan() returns false and scanner.Err() returns bufio.ErrTooLong. Streaming chat then ends with an error partway through the response.

This change adds scanner.Buffer(make([]byte, 64*1024), 1024*1024) immediately after every SSE scanner that was still bare, raising the cap to 1MB. 1MB is the value already used for streaming chat in openai.go, modelscope.go, groq.go, mistral.go, xai.go and the other already patched providers (the 8MB cap in the repo is reserved for TTS and embedding paths), so this simply converges the remaining providers onto the established pattern. Nothing else changes: line parsing, data: prefix handling, [DONE] detection, JSON unmarshalling, error handling, and the existing scanner.Err() checks all stay the same.

Providers covered (23 scanners across 22 files): 302ai, aliyun, baichuan, baidu, cohere, deepinfra, deepseek, gitee, huggingface, lmstudio, minimax (the chat scanner, whose TTS scanner was already bumped), moonshot, nvidia, ollama, openrouter, orcarouter, paddleocr, siliconflow, tokenhub, vllm, volcengine, xunfei, zhipu-ai. jiekouai.go is excluded because it is covered by the in flight #15337.

A table driven regression test (sse_scanner_buffer_test.go) streams a single 128KB data: content delta followed by data: [DONE] through an httptest server and asserts that ChatStreamlyWithSender delivers the full content with no error across a representative subset of providers. Without the buffer fix the test fails with bufio.Scanner: token too long.

This PR also removes three duplicate declarations of the package level roundTripperFunc test helper that several recently merged provider PRs each added independently, which had left the internal/entity/models test package unable to compile. The helper now lives in a single place and is shared.

Type of change

  • Bug Fix (non-breaking change which fixes an issue)

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. 🧪 test Pull requests that update test cases. labels May 29, 2026
@coderabbitai

coderabbitai Bot commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds explicit bufio.Scanner.Buffer(make([]byte, 64*1024), 1024*1024) calls to SSE/stream scanners across many Go model providers (and one OCR reader), adds a regression test that emits a >64KiB SSE data: line, and consolidates/removes duplicate test helper definitions.

Changes

SSE Buffer Size and Test Consolidation

Layer / File(s) Summary
Increase scanner buffer in chat streaming providers
internal/entity/models/302ai.go, internal/entity/models/aliyun.go, internal/entity/models/baichuan.go, internal/entity/models/baidu.go, internal/entity/models/cohere.go, internal/entity/models/deepinfra.go, internal/entity/models/deepseek.go, internal/entity/models/gitee.go, internal/entity/models/huggingface.go, internal/entity/models/lmstudio.go, internal/entity/models/minimax.go, internal/entity/models/moonshot.go, internal/entity/models/nvidia.go, internal/entity/models/ollama.go, internal/entity/models/openrouter.go, internal/entity/models/orcarouter.go, internal/entity/models/paddleocr.go, internal/entity/models/siliconflow.go, internal/entity/models/tokenhub.go, internal/entity/models/vllm.go, internal/entity/models/volcengine.go, internal/entity/models/xunfei.go, internal/entity/models/zhipu-ai.go
Each provider's SSE/stream parsing now calls scanner.Buffer(make([]byte, 64*1024), 1024*1024) before scanning so single-line SSE payloads up to 1MiB are accepted without bufio.ErrTooLong.
Add regression test for large SSE chunks
internal/entity/models/sse_scanner_buffer_test.go
New table-driven test that spins up a local SSE server emitting one oversized data: line (>64KiB) plus sentinel events and verifies ChatStreamlyWithSender streams the complete payload for multiple providers.
Consolidate and document test helpers
internal/entity/models/ppio_test.go, internal/entity/models/longcat_test.go, internal/entity/models/modelscope_test.go, internal/entity/models/voyage_test.go
Removes duplicate roundTripperFunc helper type from several test files and adds a package-level doc comment in ppio_test.go documenting roundTripperFunc as the shared package test helper.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

size:L

Suggested reviewers

  • Haruko386

Poem

"A rabbit by the stream did see,
Long data lines that troubled thee.
I stretched the buffer, wide and grand,
Now no chunk slips from paw or hand. 🐰🌊"

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The PR title clearly and concisely describes the main change: raising the SSE scanner buffer limit to prevent large stream chunks from being dropped.
Linked Issues check ✅ Passed The PR fully addresses issue #15381 by adding scanner.Buffer() calls to 23 scanners across 22 provider files, matching the expected behavior, and including a regression test that reproduces the 64KB truncation issue.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the SSE scanner buffer issue and removing duplicate test helpers. No extraneous modifications are present.
Description check ✅ Passed The PR description comprehensively addresses the template requirements with clear problem statement, detailed context, change justification, and type classification.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Dexterity104
Dexterity104 force-pushed the fix/go-models-sse-scanner-buffer branch from f05dc73 to 846fa5a Compare May 29, 2026 01:30
@Dexterity104

Copy link
Copy Markdown
Contributor Author

Hi, @JinHai-CN @Haruko386
Could you please review this PR?

@Haruko386 Haruko386 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Dexterity104 LGTM
Thank you for your contribution

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 29, 2026
@Haruko386 Haruko386 added ci Continue Integration and removed 🐞 bug Something isn't working, pull request that fix bug. 🧪 test Pull requests that update test cases. labels May 29, 2026
@Haruko386
Haruko386 marked this pull request as draft May 29, 2026 02:19
@Haruko386
Haruko386 marked this pull request as ready for review May 29, 2026 02:19
@dosubot dosubot Bot added the 🐞 bug Something isn't working, pull request that fix bug. label May 29, 2026
@codecov

codecov Bot commented May 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (7fc909a) to head (846fa5a).
⚠️ Report is 17 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15382      +/-   ##
==========================================
- Coverage   94.56%   93.16%   -1.40%     
==========================================
  Files          10       10              
  Lines         717      717              
  Branches      118      118              
==========================================
- Hits          678      668      -10     
- Misses         25       29       +4     
- Partials       14       20       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Haruko386 Haruko386 removed the 🐞 bug Something isn't working, pull request that fix bug. label May 29, 2026
@JinHai-CN
JinHai-CN merged commit 04aa8d0 into infiniflow:main May 29, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Streaming chat fails on SSE lines larger than 64KB in Go model providers

3 participants