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): preserve Anthropic thinking signatures through Responses (#778)
Graham King7c6c2a4369fb
diff --git a/crates/switchyard-translation/src/codecs/anthropic/stream.rs b/crates/switchyard-translation/src/codecs/anthropic/stream.rsindex 62dc0e368..5ea41338c 100644--- a/crates/switchyard-translation/src/codecs/anthropic/stream.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/stream.rs@@ -326,17 +326,27 @@ fn decode_anthropic_content_block_start(object: &Map<String, Value>) -> Vec<LlmR}]}).unwrap_or_default(),- Some("thinking") => block- .and_then(|block| block.get("thinking"))- .and_then(Value::as_str)- .filter(|text| !text.is_empty())- .map(|text| {- vec![LlmResponseChunk::ReasoningDelta {+ Some("thinking") => {+ let mut out = Vec::new();+ if let Some(text) = block+ .and_then(|block| block.get("thinking"))+ .and_then(Value::as_str)+ .filter(|text| !text.is_empty())+ {+ out.push(LlmResponseChunk::ReasoningDelta {index,text: text.to_string(),- }]- })- .unwrap_or_default(),+ });+ }+ if let Some(signature) = block+ .and_then(|block| block.get("signature"))+ .and_then(Value::as_str)+ .filter(|signature| !signature.is_empty())+ {+ out.push(anthropic_signature_delta(index, signature));+ }+ out+ }Some("tool_use") => {let Some(block) = block else {return Vec::new();@@ -385,7 +395,11 @@ fn decode_anthropic_content_block_delta(object: &Map<String, Value>) -> Vec<LlmR}]}).unwrap_or_default(),- Some("signature_delta") => Vec::new(),+ Some("signature_delta") => delta+ .get("signature")+ .and_then(Value::as_str)+ .map(|signature| vec![anthropic_signature_delta(index, signature)])+ .unwrap_or_default(),Some("input_json_delta") => delta.get("partial_json").and_then(Value::as_str)@@ -402,6 +416,14 @@ fn decode_anthropic_content_block_delta(object: &Map<String, Value>) -> Vec<LlmR}}+fn anthropic_signature_delta(index: usize, signature: &str) -> LlmResponseChunk {+ LlmResponseChunk::ReasoningDetailsDelta {+ index,+ details: vec![json!({"type": "anthropic.signature_delta", "signature": signature})],+ text: String::new(),+ }+}+// Starts an Anthropic text block when the target stream needs one.fn ensure_anthropic_text_block(state: &mut StreamTranslationState) -> Vec<Value> {let mut out = close_anthropic_reasoning_block(state);diff --git a/crates/switchyard-translation/src/codecs/openai_chat/stream.rs b/crates/switchyard-translation/src/codecs/openai_chat/stream.rsindex 86fdf124d..5e7c41409 100644--- a/crates/switchyard-translation/src/codecs/openai_chat/stream.rs+++ b/crates/switchyard-translation/src/codecs/openai_chat/stream.rs@@ -217,13 +217,14 @@ fn encode_openai_chat_stream()]}LlmResponseChunk::ReasoningDetailsDelta { details, text, .. } => {- // A Responses decoder announces a reasoning item's provider id ahead of its- // payload; that announcement carries nothing a chat client can use.+ // Chat cannot replay Anthropic signature fragments or an encrypted item's+ // id announcement without its payload.let details: Vec<Value> = details.into_iter()- .filter(|detail| {- detail.get("type").and_then(Value::as_str) != Some("reasoning.encrypted")- || detail.get("data").is_some()+ .filter(|detail| match detail.get("type").and_then(Value::as_str) {+ Some("anthropic.signature_delta") => false,+ Some("reasoning.encrypted") => detail.get("data").is_some(),+ _ => true,}).collect();if details.is_empty() && text.is_empty() {diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rsindex 0cda95b04..115f808d2 100644--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs@@ -844,6 +844,17 @@ fn flush_responses_tool_block(// Decodes a Responses reasoning item into private reasoning IR content.fn decode_responses_reasoning_item(item: &Map<String, Value>) -> Vec<ContentBlock> {+ if let Some((text, signature)) = item+ .get("encrypted_content")+ .and_then(Value::as_str)+ .and_then(super::decode_anthropic_thinking)+ {+ return vec![ContentBlock::Reasoning {+ text,+ signature: Some(signature),+ details: Vec::new(),+ }];+ }let mut parts = Vec::new();collect_responses_reasoning_text(item.get("content"), &mut parts);collect_responses_reasoning_text(item.get("summary"), &mut parts);@@ -1831,7 +1842,37 @@ fn encode_responses_output(outputs: &[ResponseOutput]) -> Value {ContentBlock::Reasoning { details, .. } => encrypted_reasoning_item_id(details),_ => None,});- if !reasoning.is_empty() || encrypted_reasoning.is_some() {+ let has_signed_reasoning = output.content.iter().any(|block| {+ matches!(+ block,+ ContentBlock::Reasoning {+ signature: Some(_),+ ..+ }+ )+ });+ if has_signed_reasoning {+ for (index, block) in output.content.iter().enumerate() {+ if let ContentBlock::Reasoning {+ text,+ signature,+ details,+ } = block+ {+ let encrypted = signature+ .as_deref()+ .map(|signature| super::encode_anthropic_thinking(text, signature))+ .or_else(|| encrypted_reasoning_data(details));+ let id = encrypted_reasoning_item_id(details)+ .unwrap_or_else(|| format!("rs_switchyard_{index}"));+ items.push(encode_responses_reasoning_output(+ text,+ encrypted.as_deref(),+ Some(&id),+ ));+ }+ }+ } else if !reasoning.is_empty() || encrypted_reasoning.is_some() {items.push(encode_responses_reasoning_output(&reasoning,encrypted_reasoning.as_deref(),diff --git a/crates/switchyard-translation/src/codecs/responses/mod.rs b/crates/switchyard-translation/src/codecs/responses/mod.rsindex 2e8bfe6f5..14ac3ce03 100644--- a/crates/switchyard-translation/src/codecs/responses/mod.rs+++ b/crates/switchyard-translation/src/codecs/responses/mod.rs@@ -8,3 +8,18 @@ mod stream;pub use buffered::OpenAiResponsesCodec;pub use stream::OpenAiResponsesStreamCodec;++// 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:";++fn encode_anthropic_thinking(text: &str, signature: &str) -> String {+ format!(+ "{ANTHROPIC_THINKING_PREFIX}{}",+ serde_json::json!([text, signature])+ )+}++fn decode_anthropic_thinking(payload: &str) -> Option<(String, String)> {+ serde_json::from_str(payload.strip_prefix(ANTHROPIC_THINKING_PREFIX)?).ok()+}diff --git a/crates/switchyard-translation/src/codecs/responses/stream.rs b/crates/switchyard-translation/src/codecs/responses/stream.rsindex 7aa6df092..6d7e0e581 100644--- a/crates/switchyard-translation/src/codecs/responses/stream.rs+++ b/crates/switchyard-translation/src/codecs/responses/stream.rs@@ -338,6 +338,15 @@ fn encode_responses_stream(// so the client can replay it on the next turn.let data = encrypted_reasoning_data(&details);let item = state.response_reasoning.entry(index).or_default();+ for detail in &details {+ if detail.get("type").and_then(Value::as_str) == Some("anthropic.signature_delta")+ && let Some(signature) = detail.get("signature").and_then(Value::as_str)+ {+ item.anthropic_signature+ .get_or_insert_default()+ .push_str(signature);+ }+ }match encrypted_reasoning_item_id(&details) {Some(id) if item.started && item.item_id.as_deref() != Some(id.as_str()) => {// The item already opened under another id; the payload would fail@@ -474,7 +483,10 @@ fn finish_responses_stream(state: &mut StreamTranslationState) -> Vec<Value> {"status": "completed","summary": summary,});- if let Some(encrypted) = &reasoning.encrypted {+ if let Some(signature) = &reasoning.anthropic_signature {+ item["encrypted_content"] =+ Value::String(super::encode_anthropic_thinking(&reasoning.text, signature));+ } else if let Some(encrypted) = &reasoning.encrypted {item["encrypted_content"] = Value::String(encrypted.clone());}out.push(json!({diff --git a/crates/switchyard-translation/src/codecs/stream.rs b/crates/switchyard-translation/src/codecs/stream.rsindex dfcfff772..b53a0293e 100644--- a/crates/switchyard-translation/src/codecs/stream.rs+++ b/crates/switchyard-translation/src/codecs/stream.rs@@ -97,6 +97,8 @@ pub(crate) struct ResponseReasoningState {/// Opaque `encrypted_content` carried by a Responses reasoning item. Kept verbatim so/// the emitted item stays replayable by the client even when no plaintext streamed.pub(crate) encrypted: Option<String>,+ #[serde(default)]+ pub(crate) anthropic_signature: Option<String>,}// Tracks an in-progress streamed tool call across provider-specific deltas.diff --git a/crates/switchyard-translation/tests/stream_translation.rs b/crates/switchyard-translation/tests/stream_translation.rsindex 1613445fe..474509c25 100644--- a/crates/switchyard-translation/tests/stream_translation.rs+++ b/crates/switchyard-translation/tests/stream_translation.rs@@ -1381,6 +1381,94 @@ fn responses_function_call_stream() -> Vec<Value> {]}+#[test]+fn anthropic_thinking_survives_responses_tool_continuation() -> TestResult {+ let engine = TranslationEngine::default();+ let policy = common::normalized_policy();+ let thinking = json!({+ "type": "thinking", "thinking": "Check the weather.\n", "signature": "opaque-signature"+ });+ let empty_thinking =+ json!({"type": "thinking", "thinking": "", "signature": "second-signature"});+ let tool = json!({"type": "tool_use", "id": "toolu_weather", "name": "weather", "input": {}});+ let buffered = engine+ .translate_response(+ WireFormat::AnthropicMessages,+ WireFormat::OpenAiResponses,+ &json!({"id": "msg_test", "type": "message", "role": "assistant", "model": "claude",+ "content": [thinking, empty_thinking, tool], "stop_reason": "tool_use",+ "usage": {"input_tokens": 1, "output_tokens": 1}}),+ &policy,+ )?+ .body;+ let events = vec![+ json!({"type": "message_start", "message": {"id": "msg_test", "model": "claude"}}),+ json!({"type": "content_block_start", "index": 0, "content_block": {+ "type": "thinking", "thinking": "Check ", "signature": "opaque-"}}),+ json!({"type": "content_block_delta", "index": 0, "delta": {+ "type": "thinking_delta", "thinking": "the weather.\n"}}),+ json!({"type": "content_block_delta", "index": 0, "delta": {+ "type": "signature_delta", "signature": "sig"}}),+ json!({"type": "content_block_delta", "index": 0, "delta": {+ "type": "signature_delta", "signature": "nature"}}),+ json!({"type": "content_block_stop", "index": 0}),+ json!({"type": "content_block_start", "index": 1, "content_block": empty_thinking}),+ json!({"type": "content_block_stop", "index": 1}),+ json!({"type": "content_block_start", "index": 2, "content_block": tool}),+ json!({"type": "content_block_delta", "index": 2, "delta": {+ "type": "input_json_delta", "partial_json": "{}"}}),+ json!({"type": "content_block_stop", "index": 2}),+ json!({"type": "message_delta", "delta": {"stop_reason": "tool_use"}}),+ json!({"type": "message_stop"}),+ ];+ let streamed = translate_stream(+ &engine,+ WireFormat::AnthropicMessages,+ WireFormat::OpenAiResponses,+ &events,+ )?;+ let done_items: Vec<Value> = streamed+ .iter()+ .filter(|event| event["type"] == "response.output_item.done")+ .map(|event| event["item"].clone())+ .collect();+ let completed = streamed+ .iter()+ .find(|event| event["type"] == "response.completed")+ .unwrap();+ for output in [+ &buffered["output"],+ &json!(done_items),+ &completed["response"]["output"],+ ] {+ let mut input = output.as_array().unwrap().clone();+ for item in &mut input {+ if item["type"] == "reasoning" {+ assert!(item["encrypted_content"].is_string());+ // Replay must use the signed original, not a client's edited summary.+ item["summary"] = json!([]);+ }+ }+ input.push(+ json!({"type": "function_call_output", "call_id": "toolu_weather", "output": "sunny"}),+ );+ let replay = engine+ .translate_request(+ WireFormat::OpenAiResponses,+ WireFormat::AnthropicMessages,+ &json!({"model": "claude", "input": input}),+ &policy,+ )?+ .body;+ assert_eq!(+ replay["messages"][0]["content"],+ json!([thinking, empty_thinking, tool])+ );+ assert_eq!(replay["messages"][1]["content"][0]["type"], "tool_result");+ }+ Ok(())+}+// Translates a whole source stream into `target` events, including the encoder's finish.fn translate_stream(engine: &TranslationEngine,