Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.
Rust
fix(translation): reject unsupported native Responses output (#788)
nachiketb-nvidia107cdede38f1
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rsindex 8a1bd44c6..67f03ebc4 100644--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs@@ -406,6 +406,7 @@ impl FormatCodec for AnthropicMessagesCodec {response: &AggLlmResponse,_policy: &TranslationPolicy,) -> Result<EncodedResponse> {+ super::super::responses::validate_response_output(response, WireFormat::AnthropicMessages)?;if let Some(body) = exact_preserved_response(&response.preservation,WireFormat::AnthropicMessages,diff --git a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rsindex 5a3d8d316..968b6f93b 100644--- a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs+++ b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs@@ -353,6 +353,7 @@ impl FormatCodec for OpenAiChatCodec {response: &AggLlmResponse,_policy: &TranslationPolicy,) -> Result<EncodedResponse> {+ super::super::responses::validate_response_output(response, WireFormat::OpenAiChat)?;if let Some(body) =exact_preserved_response(&response.preservation, WireFormat::OpenAiChat, _policy){diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rsindex 6d185c8ac..998505913 100644--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs@@ -1805,6 +1805,15 @@ fn decode_responses_output_item(content: decode_responses_reasoning_item(item),stop_reason: None,})),+ Some(kind) if super::is_native_output(kind) => Ok(Some(ResponseOutput {+ url_citations: Vec::new(),+ role: Role::Assistant,+ content: vec![ContentBlock::Unknown {+ provider: WireFormat::OpenAiResponses.into(),+ raw: Value::Object(item.clone()),+ }],+ stop_reason: None,+ })),_ => Ok(None),}}@@ -1848,10 +1857,12 @@ fn encode_responses_output(outputs: &[ResponseOutput]) -> Value {outputs.iter().flat_map(|output| {- let has_tool_calls = output- .content- .iter()- .any(|block| matches!(block, ContentBlock::ToolCall(_)));+ let has_output_items = output.content.iter().any(|block| {+ matches!(block, ContentBlock::ToolCall(_))+ || matches!(block, ContentBlock::Unknown { provider, raw }+ if provider.as_str() == WireFormat::OpenAiResponses.as_str()+ && raw["type"].as_str().is_some_and(super::is_native_output))+ });let text = text_from_blocks(&output.content, "");let reasoning = reasoning_text_from_blocks(&output.content, "\n");let status = if matches!(output.stop_reason, Some(StopReason::MaxTokens)) {@@ -1907,7 +1918,7 @@ fn encode_responses_output(outputs: &[ResponseOutput]) -> Value {));}- if !text.is_empty() || (!has_tool_calls && reasoning.is_empty()) {+ if !text.is_empty() || (!has_output_items && reasoning.is_empty()) {items.push(json!({"type": "message","id": "msg_switchyard",@@ -1934,6 +1945,12 @@ fn encode_responses_output(outputs: &[ResponseOutput]) -> Value {"name": call.name,"arguments": json_string_python_style(&call.arguments),})),+ ContentBlock::Unknown { provider, raw }+ if provider.as_str() == WireFormat::OpenAiResponses.as_str()+ && raw["type"].as_str().is_some_and(super::is_native_output) =>+ {+ Some(raw.clone())+ }_ => None,}));diff --git a/crates/switchyard-translation/src/codecs/responses/mod.rs b/crates/switchyard-translation/src/codecs/responses/mod.rsindex 14ac3ce03..3939a6c8c 100644--- a/crates/switchyard-translation/src/codecs/responses/mod.rs+++ b/crates/switchyard-translation/src/codecs/responses/mod.rs@@ -9,6 +9,52 @@ mod stream;pub use buffered::OpenAiResponsesCodec;pub use stream::OpenAiResponsesStreamCodec;+pub(crate) fn is_native_output(kind: &str) -> bool {+ matches!(+ kind,+ "apply_patch_call" | "shell_call" | "computer_call" | "image_generation_call"+ )+}++pub(crate) fn validate_response_output(+ response: &crate::AggLlmResponse,+ target: crate::WireFormat,+) -> crate::Result<()> {+ for block in response.outputs.iter().flat_map(|output| &output.content) {+ if let crate::ContentBlock::Unknown { provider, raw } = block+ && provider.as_str() == crate::WireFormat::OpenAiResponses.as_str()+ && raw["type"].as_str().is_some_and(is_native_output)+ {+ return Err(crate::TranslationError::UnsupportedTranslation {+ from: provider.clone(),+ to: target.into(),+ });+ }+ }+ Ok(())+}++// Native outputs have no neutral streaming representation; inspect them before discarding raw events.+pub(crate) fn validate_stream_output(+ source: &crate::FormatId,+ target: &crate::FormatId,+ event: &serde_json::Value,+) -> crate::Result<()> {+ if source.as_str() == crate::WireFormat::OpenAiResponses.as_str() && source != target {+ let mut items = event+ .get("item")+ .into_iter()+ .chain(event["response"]["output"].as_array().into_iter().flatten());+ if items.any(|item| item["type"].as_str().is_some_and(is_native_output)) {+ return Err(crate::TranslationError::UnsupportedTranslation {+ from: source.clone(),+ to: target.clone(),+ });+ }+ }+ Ok(())+}+// This is a transport envelope, not encryption. Keep it distinct from native OpenAI// ciphertext, and retain the original text because the signature covers that text.const ANTHROPIC_THINKING_PREFIX: &str = "switchyard:anthropic-thinking:v1:";diff --git a/crates/switchyard-translation/src/codecs/stream.rs b/crates/switchyard-translation/src/codecs/stream.rsindex 26b918c21..87da2d649 100644--- a/crates/switchyard-translation/src/codecs/stream.rs+++ b/crates/switchyard-translation/src/codecs/stream.rs@@ -316,6 +316,14 @@ pub(crate) fn encode_response_stream_event(let (preservation, normalized) = event.into_parts();if let Some(preservation) = preservation {let (source, raw) = preservation.into_parts();+ if let Err(error) = super::responses::validate_stream_output(&source, target, &raw) {+ return target_codec.encode_event(+ state,+ LlmResponseChunk::DecodeError {+ message: error.to_string(),+ },+ );+ }// Invalid protocol data must become an error frame, not be replayed as ordinary data.let has_decode_error = normalized.iter()diff --git a/crates/switchyard-translation/src/engine.rs b/crates/switchyard-translation/src/engine.rsindex 4bd99f8e7..9e739c503 100644--- a/crates/switchyard-translation/src/engine.rs+++ b/crates/switchyard-translation/src/engine.rs@@ -260,6 +260,16 @@ impl TranslationEngine {let target = target.into();let source_codec = self.stream_registry.codec(source.clone())?;let target_codec = self.stream_registry.codec(target.clone())?;+ if let Err(error) =+ crate::codecs::responses::validate_stream_output(&source, &target, event)+ {+ return Ok(target_codec.encode_event(+ state,+ crate::LlmResponseChunk::DecodeError {+ message: error.to_string(),+ },+ ));+ }let canonical = source_codec.decode_event(state, event);state.source = Some(source);state.target = Some(target);diff --git a/crates/switchyard-translation/tests/response_translation.rs b/crates/switchyard-translation/tests/response_translation.rsindex 43a7ee602..4280fa3f1 100644--- a/crates/switchyard-translation/tests/response_translation.rs+++ b/crates/switchyard-translation/tests/response_translation.rs@@ -17,6 +17,123 @@ use common::{type TestResult = std::result::Result<(), Box<dyn std::error::Error + Send + Sync>>;+#[test]+fn responses_native_output_is_preserved_or_rejected() -> TestResult {+ use switchyard_translation::{StreamTranslationState, TranslationError};+ let engine = TranslationEngine::default();+ let source = WireFormat::OpenAiResponses;+ let items = [+ json!({"type": "apply_patch_call", "id": "ap_1", "call_id": "call_1", "status": "completed",+ "operation": {"type": "create_file", "path": "probe.txt", "diff": "+probe\n"}}),+ json!({"type": "shell_call", "id": "sh_1", "call_id": "call_1", "status": "completed",+ "action": {"commands": ["pwd"], "timeout_ms": 1000, "max_output_length": 1024}}),+ json!({"type": "computer_call", "id": "cu_1", "call_id": "call_1", "status": "completed",+ "action": {"type": "click", "x": 10, "y": 20, "button": "left"}, "pending_safety_checks": []}),+ json!({"type": "image_generation_call", "id": "ig_1", "status": "completed", "result": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/l2QAAAAASUVORK5CYII="}),+ ];+ let text = json!({"type": "message", "role": "assistant", "content": [+ {"type": "output_text", "text": "Working."}+ ]});+ let mut failures = Vec::new();+ for item in items {+ let kind = item["type"].as_str().ok_or("missing item type")?;+ let body = json!({"id": "resp_native", "model": "provider", "status": "completed", "output": [item]});+ for policy in [TranslationPolicy::default(), normalized_policy()] {+ let decoded = engine.decode_response(source, &body, &policy)?.response;+ let replay = engine.encode_response(source, &decoded, &policy)?.body;+ if replay["output"] != body["output"] {+ failures.push(format!("{kind}: same-format buffered output lost"));+ }+ for target in [WireFormat::OpenAiChat, WireFormat::AnthropicMessages] {+ for output in [json!([item]), json!([text, item])] {+ let mut body = body.clone();+ body["output"] = output;+ let result = engine.translate_response(source, target, &body, &policy);+ if !matches!(result, Err(TranslationError::UnsupportedTranslation { .. })) {+ failures.push(format!("{kind}: {target:?} buffered did not reject"));+ }+ }+ if !matches!(+ engine.encode_response(target, &decoded, &policy),+ Err(TranslationError::UnsupportedTranslation { .. })+ ) {+ failures.push(format!(+ "{kind}: {target:?} separate buffered encode did not reject"+ ));+ }+ }+ }+ let mut initial = item.clone();+ initial["status"] = json!("in_progress");+ let added =+ json!({"type": "response.output_item.added", "output_index": 0, "item": initial});+ let done = json!({"type": "response.output_item.done", "output_index": 0, "item": item});+ let completed = json!({"type": "response.completed", "response": body});+ let incomplete = json!({"type": "response.incomplete", "response": {+ "id": "resp_native", "status": "incomplete", "output": [item],+ "incomplete_details": {"reason": "max_output_tokens"}+ }});+ for frames in [+ vec![added, done.clone(), completed.clone()],+ vec![done, completed.clone()],+ vec![completed.clone()],+ vec![+ json!({"type": "response.output_text.delta", "output_index": 1, "delta": "Working."}),+ completed,+ ],+ vec![incomplete],+ ] {+ let mut decoder = StreamTranslationState::default();+ let mut replay_state = StreamTranslationState::default();+ let mut replay = Vec::new();+ for frame in &frames {+ let event = engine.decode_stream_event(&mut decoder, source, frame.clone())?;+ replay.extend(engine.encode_stream_event(&mut replay_state, source, event)?);+ }+ replay.extend(engine.finish_stream(&mut replay_state, source)?);+ assert_eq!(replay, frames, "{kind}: same-format stream");+ for target in [WireFormat::OpenAiChat, WireFormat::AnthropicMessages] {+ for is_preserved in [false, true] {+ let mut decoder = StreamTranslationState::default();+ let mut encoder = StreamTranslationState::default();+ let mut events = Vec::new();+ for frame in &frames {+ let encoded = if is_preserved {+ let event =+ engine.decode_stream_event(&mut decoder, source, frame.clone())?;+ engine.encode_stream_event(&mut encoder, target, event)?+ } else {+ engine.translate_event(&mut encoder, source, target, frame)?+ };+ events.extend(encoded);+ }+ events.extend(engine.finish_stream(&mut encoder, target)?);+ let errors = events+ .iter()+ .filter(|event| event["error"].is_object())+ .count();+ let last = events.last().ok_or("missing terminal error")?;+ if !encoder.errored+ || errors != 1+ || !last["error"]["message"]+ .as_str()+ .is_some_and(|message| message.contains("not supported"))+ || events.iter().any(|event| {+ event["type"] == "message_stop"+ || event["choices"][0]["finish_reason"].is_string()+ })+ {+ failures.push(format!("{kind}: {target:?} stream did not terminate with one unsupported-translation error"));+ }+ assert!(engine.finish_stream(&mut encoder, target)?.is_empty());+ }+ }+ }+ }+ assert!(failures.is_empty(), "{}", failures.join("\n"));+ Ok(())+}+#[test]fn url_citations_survive_chat_responses_translation() -> TestResult {let engine = TranslationEngine::default();