Skip to content

fix(server): render Anthropic error envelopes - #315

Merged
nachiketb-nvidia merged 2 commits into
mainfrom
nachiketb/switch-1221-anthropic-error-envelope
Aug 5, 2026
Merged

fix(server): render Anthropic error envelopes#315
nachiketb-nvidia merged 2 commits into
mainfrom
nachiketb/switch-1221-anthropic-error-envelope

Conversation

@nachiketb-nvidia

@nachiketb-nvidia nachiketb-nvidia commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

What

Render errors from /v1/messages and /v1/messages/count_tokens with Anthropic's native type: "error" envelope. OpenAI Chat and Responses errors keep their existing envelope.

Why

Anthropic-compatible clients currently receive native Anthropic success bodies but OpenAI-shaped failures. Upstream JSON error bodies are also embedded as escaped strings instead of exposing their message.

Fixes SWITCH-1221.

How

  • Retain private canonical error metadata on server error responses.
  • Re-render that metadata once at the endpoint boundary using the inbound wire format.
  • Map Anthropic error types from HTTP status.
  • Extract nested upstream error messages before building the client envelope.
  • Expand the existing error-envelope integration test across Chat, Responses, and Messages.

What to review

  • Anthropic status-to-error-type mapping.
  • Preservation of existing OpenAI Chat and Responses envelopes.
  • Error rendering remains server-local and does not enter the translation IR.

Validation

  • cargo test -p switchyard-server --test server request_and_upstream_errors_use_the_inbound_wire_format
  • cargo clippy -p switchyard-server --all-targets -- -D warnings
  • cargo fmt --all -- --check

Summary by CodeRabbit

  • Bug Fixes
    • Improved API error formatting across OpenAI, Responses, and Anthropic endpoints.
    • Anthropic endpoints now return compatible error envelopes and status-specific error types.
    • Upstream JSON errors display a concise message when available.
    • Authentication failures and unknown-model errors now consistently match the requested API format.
    • Anthropic token-counting errors now use the same standardized error formatting.

Signed-off-by: nachiketb <nachiketb@nvidia.com>
@nachiketb-nvidia
nachiketb-nvidia requested a review from a team as a code owner August 5, 2026 20:14
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 3217e777-b568-46f1-9d81-7effa94fc7f6

📥 Commits

Reviewing files that changed from the base of the PR and between f4aa288 and 39a99df.

📒 Files selected for processing (2)
  • crates/switchyard-server/src/lib.rs
  • crates/switchyard-server/tests/server.rs

Walkthrough

The server now stores structured ApiError values and renders them at the endpoint boundary. OpenAI and Anthropic endpoints produce format-specific envelopes. Tests cover upstream authentication failures and Anthropic unknown-model errors.

Changes

Error rendering

Layer / File(s) Summary
Structured error storage and rendering
crates/switchyard-server/src/lib.rs
ApiError retains status, message, type, and code until wire-format rendering. Anthropic token-counting and endpoint errors use Anthropic envelopes. Upstream JSON errors prefer /error/message.
Wire-format error validation
crates/switchyard-server/tests/server.rs
Mock authentication failures and unknown-model responses are checked for Chat, Responses, and Anthropic error formats.

Estimated code review effort: 4 (Complex) | ~45 minutes

Poem

A rabbit hops through errors bright,
With OpenAI and Anthropic right.
It keeps each code and status near,
Then shapes the envelope clear.
“Auth failures now speak each format!” the rabbit cheers.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: rendering Anthropic-compatible error envelopes in the server.
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.

Signed-off-by: nachiketb <nachiketb@nvidia.com>
@nachiketb-nvidia
nachiketb-nvidia merged commit e1895c7 into main Aug 5, 2026
17 checks passed
@nachiketb-nvidia
nachiketb-nvidia deleted the nachiketb/switch-1221-anthropic-error-envelope branch August 5, 2026 20:38
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.

2 participants