Skip to content

fix(relay): forward bare-JSON stream frames without "data:" prefix - #6798

Open
TuTouPower wants to merge 1 commit into
QuantumNous:mainfrom
TuTouPower:fix/bare-json-stream-frames
Open

TuTouPower wants to merge 1 commit into
QuantumNous:mainfrom
TuTouPower:fix/bare-json-stream-frames

Conversation

@TuTouPower

@TuTouPower TuTouPower commented Aug 12, 2026 •

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • AI-generated: this PR was written with Claude Code assistance.

📝 变更描述 / Description

StreamScannerHandler(relay/helper/stream_scanner.go)此前只转发以 data: 或 [DONE] 开头的行,其余行一律丢弃。部分上游(如 Console Go 网关的 deepseek-v4-pro)的 Anthropic stream 直接下发无 data: 前缀的裸 JSON 帧({"type":"content_block_delta",...},以及若干 {} 心跳帧),导致这些帧全部被丢弃,客户端收到 HTTP 200 + 空 body。对照 deepseek-v4-flash(上游用标准 data: SSE)则正常。

改动:在原有过滤条件上,额外接受以 { 开头的行作为数据帧;仅在存在 data: 前缀时剥离前缀。空对象 {} 心跳帧继续由既有 len(data) < 6 守卫过滤,无需特殊处理。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 描述已人工整理(含 AI 生成声明)。
  • 非重复提交: 已搜索现有 Issues 与 PRs(feat: SSE raw passthrough for Claude-format relay #5846 为已关闭的 SSE raw passthrough,处理 event: 行过滤,与本缺陷无前缀裸 JSON 行不同)。
  • Bug fix 说明: 已关联 Issue [Bug] Anthropic stream 空 body:上游无 "data:" 前缀的裸 JSON 帧被 StreamScannerHandler 丢弃(deepseek-v4-pro) #6797。
  • 变更理解: 理解改动:通用流扫描器多接受 { 开头裸 JSON 行,已由直连上游 vs 经 new-api 对比验证根因。
  • 范围聚焦: 仅改 stream_scanner.go 及其测试,不含无关改动。
  • 本地验证: go test ./relay/... 全部通过(含新增 TestStreamScannerHandler_BareJSONFrames、TestStreamScannerHandler_MixedSSEAndBareJSON)。
  • 安全合规: 无敏感凭据,符合项目规范(relaykit 独立构建通过、go vet 通过)。

📸 运行证明 / Proof of Work

go test ./relay/helper/ -run TestStreamScannerHandler -v
# PASS 全部用例,含新增 BareJSONFrames / MixedSSEAndBareJSON

go test ./relay/...     # 全绿
GOWORK=off go build ./...  # relaykit 独立构建通过

Summary by CodeRabbit

  • Bug Fixes

    • Stream processing now supports JSON lines without the data: prefix.
    • Mixed SSE and unprefixed JSON streams are handled correctly.
    • Unrecognized lines and {} heartbeat frames continue to be ignored.
    • Streams can end at EOF without requiring a [DONE] marker.
  • Tests

    • Added coverage for bare JSON and mixed-format streaming responses.

Some upstreams (e.g. the Console Go gateway for certain models like
deepseek-v4-pro) emit Anthropic stream chunks as bare JSON lines
({"type":"content_block_delta",...}) instead of standard SSE with a
"data:" prefix. StreamScannerHandler dropped every non-"data:" line, so
clients received a 200 with an empty body while other models (which do
use SSE) worked.

Accept lines starting with '{' as data frames in addition to "data:" /
"[DONE]" prefixed ones, and only strip the "data:" prefix when present.
Empty-object "{}" heartbeat frames are still filtered by the existing
len<6 guard.

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 642b65b2-d45f-4db7-bf80-677a16844b61

📥 Commits

Reviewing files that changed from the base of the PR and between ccd535e and 924bb46.

📒 Files selected for processing (2)
  • relay/helper/stream_scanner.go
  • relay/helper/stream_scanner_test.go

Walkthrough

The stream scanner now accepts bare JSON lines alongside data: SSE frames. It preserves unprefixed JSON, skips empty heartbeat objects, continues to ignore [DONE], and allows streams to end at EOF. Tests cover bare and mixed-format streams.

Changes

Stream frame support

Layer / File(s) Summary
Scanner filtering and stream coverage
relay/helper/stream_scanner.go, relay/helper/stream_scanner_test.go
The scanner forwards lines beginning with { and strips data: only when present. Tests cover empty heartbeat frames, valid bare JSON, mixed SSE and bare JSON frames, [DONE], and EOF termination.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: seefs001, calcium-ion

Poem

I’m a rabbit with frames in my stream,
Bare JSON now joins the SSE dream.
Heartbeats hop by,
[DONE] waves goodbye,
And EOF closes the gleam.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes forwarding bare JSON stream frames without a data: prefix.
Linked Issues check ✅ Passed The changes satisfy issue #6797 by forwarding bare JSON frames, preserving SSE handling, filtering {} heartbeats, and adding coverage.
Out of Scope Changes check ✅ Passed The changes are limited to the stream scanner fix and focused tests required by issue #6797.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

This branch has not been deployed

No deployments
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.

[Bug] Anthropic stream 空 body:上游无 "data:" 前缀的裸 JSON 帧被 StreamScannerHandler 丢弃(deepseek-v4-pro)

1 participant