fix: finalize Claude streams after upstream EOF - #6721
Conversation
WalkthroughThe change adds an exported OpenAI-to-Claude stream finalization wrapper. Claude final-response handling now invokes the wrapper after processing converted stream responses and emits the finalized responses. ChangesOpenAI-to-Claude stream finalization
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
relay/channel/openai/helper.go (1)
197-199: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick winAdd regression coverage for the final SSE sequence.
Test a final normal chunk without
finish_reason, followed by finalization. Assertcontent_block_stop,message_delta, andmessage_stop.Also test a chunk that already completes conversion. Assert that the finalizer emits no duplicate events after
Doneis set. The PR adds no tests. (github.com)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@relay/channel/openai/helper.go` around lines 197 - 199, Add regression tests covering FinalizeStreamResponseOpenAI2Claude and the helper.ClaudeData flow: verify a final normal chunk without finish_reason followed by finalization emits content_block_stop, message_delta, and message_stop, and verify an already-completed chunk causes no duplicate events after Done is set.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@relay/channel/openai/helper.go`:
- Around line 197-199: Add regression tests covering
FinalizeStreamResponseOpenAI2Claude and the helper.ClaudeData flow: verify a
final normal chunk without finish_reason followed by finalization emits
content_block_stop, message_delta, and message_stop, and verify an
already-completed chunk causes no duplicate events after Done is set.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 88f13e74-68bb-414c-be3b-4764cfd98469
📒 Files selected for processing (2)
relay/channel/openai/helper.gorelaykit/relayconvert/response_compat.go
📝 变更描述 / Description
修复 OpenAI 兼容流在发送
[DONE]或直接 EOF、但没有可转换的终止 chunk 时,Claude Messages 流缺少结束事件的问题。此前
HandleFinalResponse处理最后一个普通 chunk 后会直接将转换状态标记为完成。若上游没有提供finish_reason终止 chunk,当前 content block 不会关闭,message_delta和message_stop也不会发送,Claude Code 等等待 Anthropic SSE 结束事件的客户端会一直停留在生成状态。本次改动在处理最后一个 chunk 后调用现有的 Claude 流 finalizer,由转换状态统一补齐仍未发送的
content_block_stop、message_delta和message_stop。正常已完成的流会由Done状态阻止重复结束事件。同时在
relayconvert兼容入口中暴露 finalizer,供 OpenAI relay 的 EOF 收尾路径调用。本 PR 仅修改两个源码文件,不包含测试文件。
与 #5345 的区别:该 PR 修改的是重构前的
service/convert.go路径;本 PR 针对当前relaykit架构下无finish_reason终止 chunk 的[DONE]/EOF 收尾路径,不改变正常终止 chunk 的 usage 处理逻辑。🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
📸 运行证明 / Proof of Work
Summary by CodeRabbit