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 server tools during reconstruction (#1000)
Atharva-Kanherkarfea62de86ad0
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rsindex 9282c71cc..18658bf69 100644--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs@@ -154,10 +154,7 @@ impl FormatCodec for AnthropicMessagesCodec {request.messages.push(Message { role, content });}}- request.tools = decode_anthropic_tools(body.get("tools"))- .into_iter()- .map(Tool::from)- .collect();+ request.tools = decode_anthropic_tools(body.get("tools"));request.tool_choice = body.get("tool_choice").map(decode_anthropic_tool_choice);request.extensions.fields = provider_extensions(body,@@ -786,25 +783,37 @@ fn decode_anthropic_file_source(block: &Map<String, Value>) -> FileSource {}// Decodes Anthropic tool definitions into normalized tool definitions.-fn decode_anthropic_tools(value: Option<&Value>) -> Vec<ToolDefinition> {+fn decode_anthropic_tools(value: Option<&Value>) -> Vec<Tool> {value.and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_object).filter_map(|tool| {+ if tool+ .get("type")+ .and_then(Value::as_str)+ .is_some_and(|kind| kind != "custom")+ {+ return Some(Tool::Provider {+ provider: WireFormat::AnthropicMessages.into(),+ raw: Value::Object(tool.clone()),+ });+ }let name = tool.get("name").and_then(Value::as_str)?.to_string();- (!name.is_empty()).then(|| ToolDefinition {- name,- description: tool- .get("description")- .and_then(Value::as_str)- .map(ToOwned::to_owned),- parameters: tool- .get("input_schema")- .cloned()- .unwrap_or_else(|| json!({})),- strict: tool.get("strict").and_then(Value::as_bool),+ (!name.is_empty()).then(|| {+ Tool::Function(ToolDefinition {+ name,+ description: tool+ .get("description")+ .and_then(Value::as_str)+ .map(ToOwned::to_owned),+ parameters: tool+ .get("input_schema")+ .cloned()+ .unwrap_or_else(|| json!({})),+ strict: tool.get("strict").and_then(Value::as_bool),+ })})}).collect()@@ -1214,6 +1223,11 @@ fn encode_anthropic_tools(tools: &[Tool]) -> Result<Value> {tools.iter().map(|tool| {+ if let Tool::Provider { provider, raw } = tool+ && provider.as_str() == WireFormat::AnthropicMessages.as_str()+ {+ return Ok(raw.clone());+ }let tool = tool.function().ok_or_else(|| {TranslationError::LossyConversion("MCP and provider tool encoding is not implemented".into(),diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rsindex 6131a9494..8817102fd 100644--- a/crates/switchyard-translation/tests/request_translation.rs+++ b/crates/switchyard-translation/tests/request_translation.rs@@ -56,6 +56,106 @@ fn normalized_chat_requests_forward_chat_template_kwargs() -> TestResult {Ok(())}+#[test]+fn anthropic_server_tools_survive_request_reconstruction() -> TestResult {+ use switchyard_translation::{InstructionBlock, Role, Tool};++ let engine = TranslationEngine::default();+ for policy in [TranslationPolicy::default(), normalized_policy()] {+ for stream in [false, true] {+ let mut body = json!({+ "model": "route", "max_tokens": 400, "stream": stream,+ "messages": [{"role": "user", "content": "Search before editing."}],+ "tools": [+ {"type": "web_search_20250305", "name": "web_search", "max_uses": 1,+ "allowed_domains": ["rust-lang.org"]},+ {"type": "custom", "name": "edit", "description": "Edit a file",+ "input_schema": {"type": "object"}},+ {"type": "future_server_tool", "settings": {"enabled": true}}+ ]+ });+ let mut request = engine+ .decode_request(WireFormat::AnthropicMessages, &body, &policy)?+ .request;+ assert!(matches!(&request.tools[0], Tool::Provider { provider, raw }+ if provider.as_str() == WireFormat::AnthropicMessages.as_str()+ && raw == &body["tools"][0]));+ let Tool::Function(function) = &mut request.tools[1] else {+ panic!("custom tools must remain editable functions");+ };+ function.description = Some("Updated edit description".into());+ request.instructions.push(InstructionBlock {+ role: Role::System,+ content: vec![ContentBlock::Text {+ text: "Plan first.".into(),+ }],+ });+ request.preservation.requests.clear();+ let output = engine.encode_request(WireFormat::AnthropicMessages, &request, &policy)?;+ body["tools"][1].as_object_mut().unwrap().remove("type");+ body["tools"][1]["description"] = json!("Updated edit description");+ assert_eq!(output.body["tools"], body["tools"]);+ assert_eq!(output.body["stream"].as_bool().unwrap_or(false), stream);+ assert_eq!(output.body["system"], json!("Plan first."));++ request.tools.remove(0);+ let output = engine.encode_request(WireFormat::AnthropicMessages, &request, &policy)?;+ assert_eq!(+ output.body["tools"],+ json!([body["tools"][1], body["tools"][2]])+ );+ }+ }+ Ok(())+}++#[test]+fn anthropic_server_tools_are_rejected_for_other_formats() -> TestResult {+ let engine = TranslationEngine::default();+ let body = json!({+ "model": "route", "max_tokens": 400,+ "messages": [{"role": "user", "content": "Search the web."}],+ "tools": [{"type": "web_search_20250305", "name": "web_search", "max_uses": 1}]+ });+ for lossy_conversion_policy in [+ LossyConversionPolicy::AllowWithDiagnostics,+ LossyConversionPolicy::Reject,+ ] {+ let policy = TranslationPolicy {+ lossy_conversion_policy,+ ..normalized_policy()+ };+ for target in [WireFormat::OpenAiChat, WireFormat::OpenAiResponses] {+ assert!(matches!(+ engine.translate_request(WireFormat::AnthropicMessages, target, &body, &policy),+ Err(TranslationError::LossyConversion(_))+ ));+ }+ }+ Ok(())+}++#[test]+fn anthropic_rejects_provider_tools_from_other_formats() {+ use switchyard_translation::{LlmRequest, Tool};++ let request = LlmRequest {+ tools: vec![Tool::Provider {+ provider: WireFormat::OpenAiResponses.into(),+ raw: json!({"type": "web_search"}),+ }],+ ..LlmRequest::default()+ };+ assert!(matches!(+ TranslationEngine::default().encode_request(+ WireFormat::AnthropicMessages,+ &request,+ &normalized_policy()+ ),+ Err(TranslationError::LossyConversion(_))+ ));+}+#[test]fn openai_chat_rejects_mcp_history_before_text_only_returns() {use switchyard_translation::{LlmRequest, McpContent, Message, Role};