Skip to content

fix(translation): reject incomplete anthropic streams - #283

Open
bhuvan002 wants to merge 1 commit into
mainfrom
bhuvana/reject-incomplete-anthropic-streams
Open

fix(translation): reject incomplete anthropic streams#283
bhuvan002 wants to merge 1 commit into
mainfrom
bhuvana/reject-incomplete-anthropic-streams

Conversation

@bhuvan002

@bhuvan002 bhuvan002 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

What

Explicitly throws and SSE error in case the upstream Anthropic stream ends prematurely without a message_stop event.

Why

The motivation — what problem does this solve, or which ticket does it close?

Closes #

How tested

  • uv run ruff check . clean
  • uv run mypy switchyard clean
  • uv run pytest tests/ green
  • Manual smoke (describe what was run)

Checklist

  • One class per file; filename = snake_case of the primary class.
  • New public symbols exported from switchyard/__init__.py.__all__ if intended for downstream use.
  • Unit tests added for new components / bug fixes.
  • README / --help updated if customer-facing surface changed.
  • Commits signed off (Signed-off-by: Your Name <email>) per the DCO.

Notes for reviewers

Anything reviewers should pay extra attention to — risky paths, follow-up tickets, intentional trade-offs.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Anthropic stream handling to detect incomplete responses.
    • Added an error when a stream ends without the expected completion event.
    • Prevented incomplete streams from being treated as successfully completed.

Signed-off-by: Bhuvan Agrawal <11240550+bhuvan002@users.noreply.github.com>
@bhuvan002
bhuvan002 requested a review from a team as a code owner August 4, 2026 17:42
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

Anthropic stream decoding now tracks message_stop events in normal and trailing frames. It reports a response-translation error when the stream ends without one. Tests verify the error and prevent synthetic stop events.

Changes

Anthropic stream completion validation

Layer / File(s) Summary
Track and validate message_stop completion
crates/switchyard-translation/src/helpers.rs
The decoder tracks message_stop events across all frames and reports an error for incomplete streams. Tests verify the error and confirm that no synthetic stop event is generated.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Poem

A rabbit watched the stream frames flow,
“Stop” must come before we go.
No stop? An error hops in sight,
No made-up event takes flight.
The decoder now ends right.

🚥 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 describes the main change: rejecting incomplete Anthropic streams without a message_stop event.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
crates/switchyard-translation/src/helpers.rs (1)

584-608: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover both terminal-frame paths.

This test covers only EOF after a normal message_start frame. It does not cover a valid message_stop in the normal path or the trailing-frame path at Lines 233-241. Add success cases for both paths. Add a concise comment stating that incomplete Anthropic streams must fail without synthesizing message_stop.

🤖 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 `@crates/switchyard-translation/src/helpers.rs` around lines 584 - 608, Extend
the Anthropic stream tests around encode_stream to cover successful completion
with a valid message_stop in both the normal terminal-frame path and the
trailing-frame path near the existing terminal handling. Add a concise comment
explaining that incomplete streams must fail without synthesizing message_stop,
while preserving the existing EOF failure assertion.

Source: Coding guidelines

🤖 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 `@crates/switchyard-translation/src/helpers.rs`:
- Around line 584-608: Extend the Anthropic stream tests around encode_stream to
cover successful completion with a valid message_stop in both the normal
terminal-frame path and the trailing-frame path near the existing terminal
handling. Add a concise comment explaining that incomplete streams must fail
without synthesizing message_stop, while preserving the existing EOF failure
assertion.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 83567ab8-0e78-43bc-a42d-6b34f6f7d41e

📥 Commits

Reviewing files that changed from the base of the PR and between 8d47c2f and 74042e7.

📒 Files selected for processing (1)
  • crates/switchyard-translation/src/helpers.rs

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.

1 participant