refactor(translation): normalize Anthropic requests through IR - #183
refactor(translation): normalize Anthropic requests through IR#183nachiketb-nvidia wants to merge 4 commits into
Conversation
Signed-off-by: nachiketb <nachiketb@nvidia.com>
Signed-off-by: nachiketb <nachiketb@nvidia.com>
WalkthroughAnthropic outbound requests now pass through shared translation and buffered normalization. The normalization handles endpoint-compatible fields, system content, tool-use IDs, and unsigned thinking blocks. Translation and native-backend tests verify the resulting request shape and replay behavior. ChangesAnthropic outbound request flow
Estimated code review effort: 4 (Complex) | ~45 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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.
Inline comments:
In `@crates/switchyard-translation/src/codecs/anthropic/buffered.rs`:
- Around line 488-497: Update normalize_outbound_request and its callers to
accept mutable diagnostics and policy, using push_lossy to report every dropped
message or non-text block. In
crates/switchyard-translation/src/codecs/anthropic/buffered.rs lines 488-497,
report removed messages and avoid emitting an empty messages array; in lines
375-386, report non-text blocks discarded when lifting system/developer turns.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 495958e0-d442-4cd2-a1d4-0fb611cdef29
📒 Files selected for processing (4)
crates/switchyard-components/src/backends/anthropic.rscrates/switchyard-components/tests/adversarial_native_backends.rscrates/switchyard-translation/src/codecs/anthropic/buffered.rscrates/switchyard-translation/tests/request_translation.rs
| let kept = blocks | ||
| .into_iter() | ||
| .filter(|block| !is_unsigned_thinking_block(block)) | ||
| .collect::<Vec<_>>(); | ||
| if kept.is_empty() { | ||
| return None; | ||
| } | ||
| message.insert("content".to_string(), Value::Array(kept)); | ||
| Some(Value::Object(message)) | ||
| } |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
Lossy normalization has no diagnostics path. normalize_outbound_request takes and returns a bare Value, so every drop it performs is invisible to callers even though the rest of this codec reports losses via push_lossy. Threading &mut Vec<Diagnostic> + policy through would cover both sites and give a place to guard the all-messages-removed case.
crates/switchyard-translation/src/codecs/anthropic/buffered.rs#L488-L497: report dropped messages and avoid emitting an emptymessagesarray.crates/switchyard-translation/src/codecs/anthropic/buffered.rs#L375-L386: report non-text blocks discarded when asystem/developerturn is lifted.
📍 Affects 1 file
crates/switchyard-translation/src/codecs/anthropic/buffered.rs#L488-L497(this comment)crates/switchyard-translation/src/codecs/anthropic/buffered.rs#L375-L386
🤖 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/codecs/anthropic/buffered.rs` around lines
488 - 497, Update normalize_outbound_request and its callers to accept mutable
diagnostics and policy, using push_lossy to report every dropped message or
non-text block. In
crates/switchyard-translation/src/codecs/anthropic/buffered.rs lines 488-497,
report removed messages and avoid emitting an empty messages array; in lines
375-386, report non-text blocks discarded when lifting system/developer turns.
Signed-off-by: nachiketb <nachiketb@nvidia.com>
Signed-off-by: nachiketb <nachiketb@nvidia.com>
What
LlmRequestIR.cache_controlannotations require it.Why
Request semantics should be normalized once by source decoding and target encoding. The previous implementation moved a large
serde_json::Valuepost-processor into the translation crate, but still duplicated behavior already represented by the IR and made provider ownership unclear.How
source_formatonLlmRequest.reasoning_effortand encode it as Anthropicthinkingplusoutput_config.context_managementand drop cross-provider extensions.What to review
LlmRequest::source_formatand provider-extension ownership.cache_controlblocks.Validation
cargo fmt --all --checkcargo clippy --workspace --all-targets -- -D warningscargo test --workspacecargo test -p switchyard-translationcargo test -p switchyard-components --test adversarial_native_backends