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 forced Responses custom tool choice (#1287)
Rainaxyz2d7665516891
diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rsindex 2a0acc6ca..8f7d47af9 100644--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs@@ -241,6 +241,7 @@ impl FormatCodec for OpenAiResponsesCodec {&& let Some(choice) = encode_responses_tool_choice(choice,crate::codex_namespaces::tool_namespaces(&request.extensions),+ crate::codex_custom_tools::custom_tools(&request.extensions),){body.insert("tool_choice".to_string(), choice);@@ -1166,6 +1167,14 @@ fn decode_responses_tool_choice(value: &Value) -> Option<ToolChoice> {Value::String(text) if text == "auto" => Some(ToolChoice::Auto),Value::String(text) if text == "required" => Some(ToolChoice::Required),Value::String(text) if text == "none" => Some(ToolChoice::None),+ Value::Object(object) if object.get("type").and_then(Value::as_str) == Some("custom") => {+ object+ .get("name")+ .and_then(Value::as_str)+ .map(|name| ToolChoice::Tool {+ name: name.to_string(),+ })+ }Value::Object(object) if object.get("type").and_then(Value::as_str) == Some("function") => {// A forced tool has to name the same thing its definition does, or// the upstream rejects a choice naming a tool it was never offered.@@ -1748,11 +1757,15 @@ fn encode_responses_tools(fn encode_responses_tool_choice(choice: &ToolChoice,namespaces: Option<&Map<String, Value>>,+ custom_tools: Option<&Map<String, Value>>,) -> Option<Value> {match choice {ToolChoice::Auto => Some(Value::String("auto".to_string())),ToolChoice::Required => Some(Value::String("required".to_string())),ToolChoice::None => Some(Value::String("none".to_string())),+ ToolChoice::Tool { name } if custom_tools.is_some_and(|tools| tools.contains_key(name)) => {+ Some(json!({"type": "custom", "name": name}))+ }ToolChoice::Tool { name } => {let split = namespaces.and_then(|namespaces| {crate::codex_namespaces::split_qualified_name(namespaces, name)diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rsindex 72e78a451..b613215c5 100644--- a/crates/switchyard-translation/tests/request_translation.rs+++ b/crates/switchyard-translation/tests/request_translation.rs@@ -4609,6 +4609,75 @@ fn responses_request_round_trips_custom_tools_and_custom_tool_calls() -> TestResOk(())}+#[test]+fn forced_custom_tool_choice_translates_to_named_tool() -> TestResult {+ let engine = TranslationEngine::default();+ for stream in [false, true] {+ let body = json!({+ "model": "route",+ "input": "Use optional_lookup.",+ "tools": [+ {"type": "custom", "name": "required_lookup", "format": {"type": "text"}},+ {"type": "function", "name": "optional_lookup", "parameters": {"type": "object"}}+ ],+ "tool_choice": {"type": "custom", "name": "required_lookup"},+ "stream": stream+ });+ for (target, expected) in [+ (+ WireFormat::OpenAiChat,+ json!({"type": "function", "function": {"name": "required_lookup"}}),+ ),+ (+ WireFormat::AnthropicMessages,+ json!({"type": "tool", "name": "required_lookup"}),+ ),+ ] {+ let output = engine.translate_request(+ WireFormat::OpenAiResponses,+ target,+ &body,+ &TranslationPolicy::default(),+ )?;+ assert_eq!(output.body["tool_choice"], expected, "{target:?}");+ assert_eq!(output.body["stream"].as_bool().unwrap_or(false), stream);+ }+ }+ Ok(())+}++#[test]+fn forced_custom_tool_choice_survives_responses_rebuild() -> TestResult {+ let engine = TranslationEngine::default();+ let policy = normalized_policy();+ let body = json!({+ "model": "route",+ "input": "Use optional_lookup.",+ "tools": [+ {"type": "custom", "name": "required_lookup", "format": {"type": "text"}},+ {"type": "function", "name": "optional_lookup", "parameters": {"type": "object"}}+ ],+ "tool_choice": {"type": "custom", "name": "required_lookup"}+ });+ let mut request = engine+ .decode_request(WireFormat::OpenAiResponses, &body, &policy)?+ .request;+ switchyard_translation::append_note(&mut request, "Check the result.");+ let output = engine.encode_request(WireFormat::OpenAiResponses, &request, &policy)?;+ assert_eq!(output.body["tool_choice"], body["tool_choice"]);+ assert_eq!(output.body["tools"][0], body["tools"][0]);++ request.tool_choice = Some(switchyard_protocol::ToolChoice::Tool {+ name: "optional_lookup".into(),+ });+ let output = engine.encode_request(WireFormat::OpenAiResponses, &request, &policy)?;+ assert_eq!(+ output.body["tool_choice"],+ json!({"type": "function", "name": "optional_lookup"})+ );+ Ok(())+}+// Codex sends GPT-5 requests in the Responses-lite shape: no top-level `tools`, empty// `instructions`, the tool definitions inside `input[0]` as an `additional_tools` developer// item, and the base instructions as a developer message. Those definitions are the request's