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 parallel tool-call restrictions across Anthropic (#779)
Graham King001842f34568
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rsindex 82722c75d..5a6d47d54 100644--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs@@ -163,6 +163,16 @@ impl FormatCodec for AnthropicMessagesCodec {"safety_identifier",],);+ if let Some(is_disabled) = body+ .get("tool_choice")+ .and_then(|choice| choice.get("disable_parallel_tool_use"))+ .and_then(Value::as_bool)+ {+ request+ .extensions+ .fields+ .insert("parallel_tool_calls".to_string(), Value::Bool(!is_disabled));+ }// OpenAI codecs share this extension for abuse attribution.if let Some(user_id) = body.get("metadata")@@ -230,11 +240,27 @@ impl FormatCodec for AnthropicMessagesCodec {if !request.tools.is_empty() {body.insert("tools".to_string(), encode_anthropic_tools(&request.tools));}- if let Some(choice) = &request.tool_choice {- body.insert(- "tool_choice".to_string(),- encode_anthropic_tool_choice(choice),+ let parallel_tool_calls = request+ .extensions+ .fields+ .get("parallel_tool_calls")+ .and_then(Value::as_bool);+ if request.tool_choice.is_some()+ || (parallel_tool_calls.is_some() && !request.tools.is_empty())+ {+ let mut choice = encode_anthropic_tool_choice(+ request.tool_choice.as_ref().unwrap_or(&ToolChoice::Auto),);+ if let Some(is_enabled) = parallel_tool_calls+ && let Some(object) = choice.as_object_mut()+ && object.get("type").and_then(Value::as_str) != Some("none")+ {+ object.insert(+ "disable_parallel_tool_use".to_string(),+ Value::Bool(!is_enabled),+ );+ }+ body.insert("tool_choice".to_string(), choice);}if request.extensions.fields.get(ANTHROPIC_REQUEST_KEY) == Some(&Value::Bool(true)) {for field in [diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rsindex 13cb69f49..d10f15d83 100644--- a/crates/switchyard-translation/tests/request_translation.rs+++ b/crates/switchyard-translation/tests/request_translation.rs@@ -1660,6 +1660,84 @@ fn responses_deferred_message_stays_after_matching_tool_result_for_openai_chat()Ok(())}+#[test]+fn parallel_tool_policy_survives_anthropic_translations() -> TestResult {+ let engine = TranslationEngine::default();+ let policy = TranslationPolicy::default();+ for format in [WireFormat::OpenAiChat, WireFormat::OpenAiResponses] {+ for choice in [Some("any"), Some("auto"), Some("tool"), None] {+ for disabled in [Some(true), Some(false), None] {+ let mut body = json!({+ "model": "test-model",+ "max_tokens": 256,+ "messages": [{"role": "user", "content": "Use the marker tool."}],+ "tools": [{+ "name": "marker",+ "input_schema": {"type": "object", "properties": {}}+ }]+ });+ if let Some(choice) = choice {+ body["tool_choice"] = json!({"type": choice});+ if choice == "tool" {+ body["tool_choice"]["name"] = json!("marker");+ }+ if let Some(disabled) = disabled {+ body["tool_choice"]["disable_parallel_tool_use"] = json!(disabled);+ }+ }+ let mut openai = engine+ .translate_request(WireFormat::AnthropicMessages, format, &body, &policy)?+ .body;+ if choice.is_some() {+ assert_eq!(+ openai.get("parallel_tool_calls"),+ disabled.map(|value| json!(!value)).as_ref(),+ "{format:?}, {choice:?}, {disabled:?}"+ );+ } else if let Some(disabled) = disabled {+ // OpenAI allows this policy without an explicit tool choice.+ openai["parallel_tool_calls"] = json!(!disabled);+ }+ let anthropic = engine+ .translate_request(format, WireFormat::AnthropicMessages, &openai, &policy)?+ .body;+ assert_eq!(+ anthropic["tool_choice"].get("disable_parallel_tool_use"),+ disabled.map(|value| json!(value)).as_ref(),+ "{format:?}, {choice:?}, {disabled:?}"+ );+ if choice.is_some() || disabled.is_some() {+ assert_eq!(anthropic["tool_choice"]["type"], choice.unwrap_or("auto"));+ if choice == Some("tool") {+ assert_eq!(anthropic["tool_choice"]["name"], "marker");+ }+ } else {+ assert!(anthropic.get("tool_choice").is_none());+ }+ if choice.is_none() {+ for tools in [None, Some(json!([]))] {+ openai.as_object_mut().unwrap().remove("tools");+ if let Some(tools) = tools {+ openai["tools"] = tools;+ }+ let anthropic = engine+ .translate_request(+ format,+ WireFormat::AnthropicMessages,+ &openai,+ &policy,+ )?+ .body;+ assert!(anthropic.get("tools").is_none());+ assert!(anthropic.get("tool_choice").is_none());+ }+ }+ }+ }+ }+ Ok(())+}+// Verifies Responses-compatible extension fields survive a Chat-to-Responses// hop, and that Chat-only fields are excluded rather than passed through.#[test]