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): Translate allowed_tools between codecs, reject when not possible (#782)
Graham King04c862557a91
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rsindex 5a6d47d54..8a1bd44c6 100644--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs@@ -210,6 +210,11 @@ impl FormatCodec for AnthropicMessagesCodec {}let mut diagnostics = Vec::new();validate_request_capabilities(request, &mut diagnostics, policy)?;+ let allowed = crate::codecs::common::allowed_function_tools(request)?;+ let (tools, tool_choice) = allowed.as_ref().map_or(+ (request.tools.as_slice(), request.tool_choice.as_ref()),+ |(tools, choice)| (tools.as_slice(), Some(choice)),+ );let mut body = Map::new();if let Some(model) = &request.model {body.insert("model".to_string(), Value::String(model.clone()));@@ -237,20 +242,16 @@ impl FormatCodec for AnthropicMessagesCodec {)?),);- if !request.tools.is_empty() {- body.insert("tools".to_string(), encode_anthropic_tools(&request.tools));+ if !tools.is_empty() {+ body.insert("tools".to_string(), encode_anthropic_tools(tools));}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 tool_choice.is_some() || (parallel_tool_calls.is_some() && !tools.is_empty()) {+ let mut choice = encode_anthropic_tool_choice(tool_choice.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")diff --git a/crates/switchyard-translation/src/codecs/common.rs b/crates/switchyard-translation/src/codecs/common.rsindex bfcce5f41..e5b9b7dbe 100644--- a/crates/switchyard-translation/src/codecs/common.rs+++ b/crates/switchyard-translation/src/codecs/common.rs@@ -5,11 +5,75 @@use serde_json::{Map, Value};-use crate::llm::ContentBlock;+use crate::error::{Result, TranslationError};+use crate::llm::{ContentBlock, LlmRequest, ToolChoice, ToolDefinition};// Internal provenance survives mutations that invalidate exact request replay.pub(crate) const ANTHROPIC_REQUEST_KEY: &str = "switchyard_anthropic_request";+/// Converts an OpenAI allowed-tools policy to a restricted function list and mode.+pub(crate) fn allowed_function_tools(+ request: &LlmRequest,+) -> Result<Option<(Vec<ToolDefinition>, ToolChoice)>> {+ let Some(ToolChoice::Raw(choice)) = &request.tool_choice else {+ return Ok(None);+ };+ if choice.get("type").and_then(Value::as_str) != Some("allowed_tools") {+ return Ok(None);+ }+ let error = || {+ TranslationError::LossyConversion(+ "allowed_tools translation requires auto or required mode \+ and a nonempty subset of known function tools"+ .to_string(),+ )+ };+ let policy = choice.get("allowed_tools").unwrap_or(choice);+ let mode = match policy.get("mode").and_then(Value::as_str) {+ Some("auto") => ToolChoice::Auto,+ Some("required") => ToolChoice::Required,+ _ => return Err(error()),+ };+ let allowed = policy+ .get("tools")+ .and_then(Value::as_array)+ .ok_or_else(error)?;+ if allowed.is_empty() {+ return Err(error());+ }+ let mut names = Vec::with_capacity(allowed.len());+ for tool in allowed {+ if tool.get("type").and_then(Value::as_str) != Some("function") {+ return Err(error());+ }+ let function = tool.get("function").unwrap_or(tool);+ let name = function+ .get("name")+ .and_then(Value::as_str)+ .filter(|name| !name.is_empty())+ .ok_or_else(error)?;+ let name = match function.get("namespace").and_then(Value::as_str) {+ Some(namespace) if !namespace.is_empty() => {+ crate::codex_namespaces::qualified_tool_name(namespace, name)+ }+ _ => name.to_string(),+ };+ if !request.tools.iter().any(|tool| tool.name == name) {+ return Err(error());+ }+ names.push(name);+ }+ // Filter the catalog, rather than rebuilding it from selectors, to retain definitions+ // and avoid duplicating tools when the allowed list repeats a name.+ let tools = request+ .tools+ .iter()+ .filter(|tool| names.contains(&tool.name))+ .cloned()+ .collect();+ Ok(Some((tools, mode)))+}+/// Returns whether a role name is recognized by a supported provider API.pub(crate) fn is_known_role_name(name: &str) -> bool {matches!(diff --git a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rsindex 65727a16c..5a3d8d316 100644--- a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs+++ b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs@@ -196,6 +196,19 @@ impl FormatCodec for OpenAiChatCodec {}let mut diagnostics = Vec::new();validate_request_capabilities(request, &mut diagnostics, policy)?;+ let allowed = match &request.tool_choice {+ Some(ToolChoice::Raw(choice))+ if choice.get("type").and_then(Value::as_str) == Some("allowed_tools")+ && choice.get("allowed_tools").is_some() =>+ {+ None+ }+ _ => crate::codecs::common::allowed_function_tools(request)?,+ };+ let (tools, tool_choice) = allowed.as_ref().map_or(+ (request.tools.as_slice(), request.tool_choice.as_ref()),+ |(tools, choice)| (tools.as_slice(), Some(choice)),+ );let mut body = Map::new();if let Some(model) = &request.model {body.insert("model".to_string(), Value::String(model.clone()));@@ -217,9 +230,9 @@ impl FormatCodec for OpenAiChatCodec {}body.insert("messages".to_string(), Value::Array(messages));- if !request.tools.is_empty() {- body.insert("tools".to_string(), encode_openai_tools(&request.tools));- if let Some(choice) = &request.tool_choice {+ if !tools.is_empty() {+ body.insert("tools".to_string(), encode_openai_tools(tools));+ if let Some(choice) = tool_choice {body.insert("tool_choice".to_string(), encode_openai_tool_choice(choice));}}diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rsindex 115f808d2..d98b88eb9 100644--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs@@ -1151,6 +1151,11 @@ fn decode_responses_tool_choice(value: &Value) -> Option<ToolChoice> {}})}+ Value::Object(object)+ if object.get("type").and_then(Value::as_str) == Some("allowed_tools") =>+ {+ Some(ToolChoice::Raw(value.clone()))+ }Value::Object(_) => None,_ => Some(ToolChoice::Raw(value.clone())),}diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rsindex d10f15d83..d51c6ae3c 100644--- a/crates/switchyard-translation/tests/request_translation.rs+++ b/crates/switchyard-translation/tests/request_translation.rs@@ -16,6 +16,118 @@ use common::{REASONING_MODEL, normalized_policy, shell_tool_call};type TestResult<T = ()> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;+#[test]+fn responses_allowed_tools_is_translated_or_rejected() -> TestResult {+ let engine = TranslationEngine::default();+ for mode in ["auto", "required"] {+ let body = json!({+ "model": "route",+ "input": "Only inspect tool policy.",+ "tools": [+ {"type": "function", "name": "safe_lookup", "description": "Read a record", "parameters": {"type": "object"}},+ {"type": "function", "name": "delete_all_records", "description": "Excluded tool", "parameters": {"type": "object"}}+ ],+ "tool_choice": {+ "type": "allowed_tools", "mode": mode,+ "tools": [{"type": "function", "name": "safe_lookup"}]+ }+ });+ for policy in [TranslationPolicy::default(), normalized_policy()] {+ for target in [WireFormat::OpenAiChat, WireFormat::AnthropicMessages] {+ let output = engine.translate_request(+ WireFormat::OpenAiResponses,+ target,+ &body,+ &policy,+ )?;+ let expected_tool = match target {+ WireFormat::OpenAiChat => json!({+ "type": "function", "function": {+ "name": "safe_lookup", "description": "Read a record",+ "parameters": {"type": "object"}+ }+ }),+ _ => json!({+ "name": "safe_lookup", "description": "Read a record",+ "input_schema": {"type": "object"}+ }),+ };+ assert_eq!(output.body["tools"], json!([expected_tool]));+ let expected_choice = match target {+ WireFormat::OpenAiChat => json!(mode),+ _ => json!({"type": if mode == "required" { "any" } else { "auto" }}),+ };+ assert_eq!(output.body["tool_choice"], expected_choice);++ for allowed in [+ json!([]),+ json!([{"type": "web_search"}]),+ json!([{"type": "function", "name": "unknown"}]),+ ] {+ let mut unsupported = body.clone();+ unsupported["tool_choice"]["tools"] = allowed;+ let error = engine+ .translate_request(+ WireFormat::OpenAiResponses,+ target,+ &unsupported,+ &policy,+ )+ .expect_err("unrepresentable restrictions must be rejected");+ assert!(matches!(+ error,+ switchyard_translation::TranslationError::LossyConversion(_)+ ));+ }+ }+ let output = engine.translate_request(+ WireFormat::OpenAiResponses,+ WireFormat::OpenAiResponses,+ &body,+ &policy,+ )?;+ assert_eq!(output.body["tool_choice"], body["tool_choice"]);+ assert_eq!(output.body["tools"], body["tools"]);++ let chat = json!({+ "model": "route",+ "messages": [{"role": "user", "content": "Inspect tool policy."}],+ "tools": [+ {"type": "function", "function": {"name": "safe_lookup", "parameters": {"type": "object"}}},+ {"type": "function", "function": {"name": "delete_all_records", "parameters": {"type": "object"}}}+ ],+ "tool_choice": {+ "type": "allowed_tools",+ "allowed_tools": {+ "mode": mode,+ "tools": [{"type": "function", "function": {"name": "safe_lookup"}}]+ }+ }+ });+ let output = engine.translate_request(+ WireFormat::OpenAiChat,+ WireFormat::OpenAiChat,+ &chat,+ &policy,+ )?;+ assert_eq!(output.body["tool_choice"], chat["tool_choice"]);+ let output = engine.translate_request(+ WireFormat::OpenAiChat,+ WireFormat::AnthropicMessages,+ &chat,+ &policy,+ )?;+ assert_eq!(output.body["tools"].as_array().map(Vec::len), Some(1));+ assert_eq!(output.body["tools"][0]["name"], "safe_lookup");+ assert_eq!(+ output.body["tool_choice"],+ json!({"type": if mode == "required" { "any" } else { "auto" }})+ );+ }+ }+ Ok(())+}+#[test]fn request_media_survives_reencoding_or_is_rejected() -> TestResult {use WireFormat::{