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(server): accept explicitly disabled tool controls (#1283)
Rainaxyz559d02d968c8
diff --git a/crates/switchyard-server/src/capabilities.rs b/crates/switchyard-server/src/capabilities.rsindex 6ac5379d5..488fd0261 100644--- a/crates/switchyard-server/src/capabilities.rs+++ b/crates/switchyard-server/src/capabilities.rs@@ -4,7 +4,7 @@//! Reject inputs that the selected route explicitly disables.use serde_json::Value;-use switchyard_protocol::{ContentBlock, LlmRequest};+use switchyard_protocol::{ContentBlock, LlmRequest, ToolChoice};use switchyard_runner::ModelCapabilities;/// Return the first explicitly disabled capability used by decoded or preserved input.@@ -32,15 +32,22 @@ pub(crate) fn unsupported_capability(}if capabilities.tool_calling == Some(false)&& (!request.tools.is_empty()- || request.tool_choice.is_some()+ || request+ .tool_choice+ .as_ref()+ .is_some_and(|choice| !matches!(choice, ToolChoice::None))|| ["tools", "functions"].iter().any(|key| {body.get(key).and_then(Value::as_array).is_some_and(|tools| !tools.is_empty())})- || ["tool_choice", "parallel_tool_calls", "function_call"]- .iter()- .any(|key| body.get(key).is_some_and(|value| !value.is_null())))+ || body.get("tool_choice").is_some_and(is_tool_choice_enabled)+ || body+ .get("function_call")+ .is_some_and(|value| !value.is_null() && value.as_str() != Some("none"))+ || body+ .get("parallel_tool_calls")+ .is_some_and(|value| !value.is_null() && value.as_bool() != Some(false))){return Some("tool_calling");}@@ -61,6 +68,12 @@ pub(crate) fn unsupported_capability(.or_else(|| unsupported_preserved_content(capabilities, body))}+fn is_tool_choice_enabled(value: &Value) -> bool {+ !value.is_null()+ && value.as_str() != Some("none")+ && value.get("type").and_then(Value::as_str) != Some("none")+}+// Some provider fields survive forwarding without becoming decoded content blocks.// Inspect protocol content and tool outputs, leaving tool arguments and text untouched.fn unsupported_preserved_content(@@ -179,9 +192,9 @@ mod tests {for body in [json!({"input": "hello", "tools": [{"type": "web_search"}]}),json!({"input": "hello", "functions": [{"name": "lookup", "parameters": {"type": "object"}}]}),- json!({"input": "hello", "tool_choice": "none"}),+ json!({"input": "hello", "tool_choice": "auto"}),json!({"input": "hello", "tool_choice": {"type": "web_search"}}),- json!({"input": "hello", "parallel_tool_calls": false}),+ json!({"input": "hello", "parallel_tool_calls": true}),json!({"input": [{"type": "additional_tools", "tools": []}]}),json!({"input": [{"type": "shell_call", "id": "shell_1", "call_id": "call_1", "action": {"commands": ["pwd"]}}]}),json!({"input": [{"type": "function_call_output", "call_id": "call_1", "output": "done"}]}),diff --git a/crates/switchyard-server/tests/server.rs b/crates/switchyard-server/tests/server.rsindex e6d781325..fa703f167 100644--- a/crates/switchyard-server/tests/server.rs+++ b/crates/switchyard-server/tests/server.rs@@ -4414,7 +4414,20 @@ async fn disabled_route_capabilities_reject_requests_before_calling_upstream() -("/v1/chat/completions","tool_calling",- json!({"messages": [{"role": "user", "content": "hello"}], "tools": [{"type": "function", "function": {"name": "exec_command", "parameters": {"type": "object"}}}]}),+ json!({"messages": [{"role": "user", "content": "hello"}], "tools": [{"type": "function", "function": {"name": "exec_command", "parameters": {"type": "object"}}}], "tool_choice": "none", "parallel_tool_calls": false}),+ ),+ (+ "/v1/chat/completions",+ "tool_calling",+ json!({"messages": [{"role": "user", "content": "hello"}], "function_call": {"name": "exec_command", "type": "none"}}),+ ),+ (+ "/v1/chat/completions",+ "tool_calling",+ json!({"messages": [+ {"role": "assistant", "tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "exec_command", "arguments": "{}"}}]},+ {"role": "tool", "tool_call_id": "call_1", "content": "done"}+ ], "tool_choice": "none"}),),("/v1/messages",@@ -4451,6 +4464,37 @@ async fn disabled_route_capabilities_reject_requests_before_calling_upstream() -}assert!(upstream.calls.lock().await.is_empty());+ for (endpoint, mut body) in [+ (+ "/v1/chat/completions",+ json!({"messages": [{"role": "system", "content": "Keep the caller's instructions."}, {"role": "user", "content": "hello"}], "tool_choice": "none", "parallel_tool_calls": false}),+ ),+ (+ "/v1/chat/completions",+ json!({"messages": [{"role": "system", "content": "Keep the caller's instructions."}, {"role": "user", "content": "hello"}], "function_call": "none"}),+ ),+ (+ "/v1/responses",+ json!({"instructions": "Keep the caller's instructions.", "input": "hello", "tool_choice": "none", "parallel_tool_calls": false}),+ ),+ (+ "/v1/messages",+ json!({"system": "Keep the caller's instructions.", "messages": [{"role": "user", "content": "hello"}], "tool_choice": {"type": "none"}}),+ ),+ ] {+ body["model"] = json!("restricted");+ let response = send(&app, "POST", endpoint, Some(body)).await?;+ assert_eq!(response.status, StatusCode::OK, "{endpoint}");+ }+ let calls = upstream.calls.lock().await;+ assert_eq!(calls.len(), 4);+ assert!(+ calls+ .iter()+ .all(|call| call["model"] == "model/efficient" && call.get("tools").is_none())+ );+ drop(calls);+let body = json!({"model": "restricted", "instructions": "Keep the caller's instructions.", "input": "hello"});@@ -4467,13 +4511,13 @@ async fn disabled_route_capabilities_reject_requests_before_calling_upstream() -assert_eq!(response.status, StatusCode::OK);}let calls = upstream.calls.lock().await;- assert_eq!(calls.len(), 3);+ assert_eq!(calls.len(), 7);assert!(calls.iter().all(|call| has_system_prompt(call, "Keep the caller's instructions.")));- for call in &calls[1..] {+ for call in &calls[5..] {assert_eq!(call["reasoning_effort"], "high");assert_eq!(call["tools"][0]["function"]["name"], "exec_command");assert!(call["messages"].as_array().is_some_and(|messages| {diff --git a/docs/reference/toml_schema.md b/docs/reference/toml_schema.mdindex 252fea115..308492d31 100644--- a/docs/reference/toml_schema.md+++ b/docs/reference/toml_schema.md@@ -264,7 +264,7 @@ Every route takes the common keys below, plus the keys for its type.| `id` | Yes | — | Public model ID that callers send in requests. || `type` | Yes | — | Routing algorithm for this route. || `context_window` | No | unset | Positive token count that `GET /v1/models` advertises for this route as `data[].context_length`. Unset values appear as `null`. This does not enforce a request limit. |-| `tool_calling` | No | unset | Tool-calling support advertised by `GET /v1/models`. When `false`, the server rejects tool definitions, tool controls, and tool history with HTTP 400 before dispatch. Unset values appear as `null`. Explicit `true` and unset values do not restrict requests. |+| `tool_calling` | No | unset | Tool-calling support advertised by `GET /v1/models`. When `false`, the server rejects tool definitions, enabled tool controls, and tool history with HTTP 400 before dispatch. Text requests can explicitly disable tools with `tool_choice: "none"` or parallel calls with `parallel_tool_calls: false`. Unset values appear as `null`. Explicit `true` and unset values do not restrict requests. || `reasoning` | No | unset | When `false`, the server rejects reasoning controls with HTTP 400 before dispatch. Explicit `true` and unset values do not restrict requests. The server does not include this declaration in `GET /v1/models`. || `vision` | No | unset | Image-input support advertised in `GET /v1/models` under `data[].capabilities.vision`. When `false`, the server rejects images with HTTP 400 before dispatch, including images in tool results. Unset values appear as `null`. Explicit `true` and unset values do not restrict requests. Declare `true` only when every target the route can select accepts images. |