Repository navigation
fix(anthropic): emit proper SSE error events on streaming requests - #2271
Conversation
When /v1/messages received an upstream provider error (for example an Anthropic 400 for a tool_use / tool_result mismatch) on a streaming request, the gateway ended the stream with only `event: message_stop`, which breaks the Anthropic streaming contract and hides the real error. The streaming handler now tracks SSE event types from the internal chat completions stream, detects error chunks (by event type or payload shape), and emits Anthropic-style `event: error` payloads followed by `event: message_stop`. The `!response.ok` path now returns SSE when the client asked for streaming instead of a JSON error body.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
WalkthroughThis PR adds Anthropic-specific error translation and integrates it into the streaming response handler. A new ChangesAnthropic Streaming Error Handling
🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
Summary
/v1/messagesstreaming requests ended with onlyevent: message_stopwhen validation failed upstream, instead of emitting a properevent: errorper the Anthropic streaming protocol.event:lines from the internal/v1/chat/completionsstream, detects error chunks (by event type or shape), and translates them into Anthropicevent: errorevents followed byevent: message_stop.!response.okbranch so that whenstream: truewas requested it returns SSE instead of a non-streaming JSON body, preserving the SSE contract.client_error,gateway_error,upstream_error.Reproduction
Malformed payload from the bug report: assistant
tool_usefollowed by a user message instead oftool_result:Before
After
Test plan
buildAnthropicErrorEventandmapInternalErrorTypeToAnthropic(11 cases): exact repro shape, wrapped internalchat.tsshape, canonical Anthropic types, unknown/future types, edge cases.pnpm --filter gateway buildpnpm --filter gateway lintpnpm formatcurlrepro against local gateway; output matches After above.Summary by CodeRabbit
Bug Fixes
Tests