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): reject duplicate tool names in Chat and Anthropic requests (#1278)
David Gardnercfcfda1549c6
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rsindex 9437e33ad..07863e5fd 100644--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs@@ -1292,6 +1292,7 @@ fn ensure_anthropic_tool_input_object(arguments: Value) -> Value {// Encodes normalized tool definitions into Anthropic tool JSON.fn encode_anthropic_tools(tools: &[Tool]) -> Result<Value> {+ let mut names = std::collections::HashSet::new();Ok(Value::Array(tools.iter()@@ -1306,6 +1307,12 @@ fn encode_anthropic_tools(tools: &[Tool]) -> Result<Value> {"MCP and provider tool encoding is not implemented".into(),)})?;+ if !names.insert(tool.name.as_str()) {+ return Err(TranslationError::InvalidValue {+ path: "$.tools".to_string(),+ message: format!("duplicate tool name: {:?}", tool.name),+ });+ }let mut item = json!({"name": tool.name,"description": tool.description.clone().unwrap_or_default(),diff --git a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rsindex a6af1d8eb..802a0dcf8 100644--- a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs+++ b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs@@ -1202,6 +1202,7 @@ fn media_source_text(source: &MediaSource) -> String {/// Encodes normalized tool definitions into OpenAI tool JSON.pub(crate) fn encode_openai_tools(tools: &[Tool]) -> Result<Value> {+ let mut names = std::collections::HashSet::new();Ok(Value::Array(tools.iter()@@ -1211,6 +1212,12 @@ pub(crate) fn encode_openai_tools(tools: &[Tool]) -> Result<Value> {"MCP and provider tool encoding is not implemented".into(),)})?;+ if !names.insert(tool.name.as_str()) {+ return Err(TranslationError::InvalidValue {+ path: "$.tools".to_string(),+ message: format!("duplicate tool name: {:?}", tool.name),+ });+ }let mut function = json!({"name": tool.name,"description": tool.description.clone().unwrap_or_default(),diff --git a/tests/test_translation.py b/tests/test_translation.pyindex dd903bf9a..fb1de2cbb 100644--- a/tests/test_translation.py+++ b/tests/test_translation.py@@ -252,6 +252,54 @@ def test_response_ir_restores_request_tool_extensions(method) -> None:ValueError,id="request",),+ pytest.param(+ "translate_request",+ (+ "openai_responses",+ "openai_chat",+ {+ "tools": [+ {+ "type": "namespace",+ "name": "a__b",+ "tools": [{"type": "function", "name": "c", "parameters": {}}],+ },+ {+ "type": "namespace",+ "name": "a",+ "tools": [{"type": "function", "name": "b__c", "parameters": {}}],+ },+ ],+ },+ ),+ {},+ TranslationError,+ id="namespace-tool-name-collision",+ ),+ pytest.param(+ "translate_request",+ (+ "openai_responses",+ "anthropic_messages",+ {+ "tools": [+ {+ "type": "namespace",+ "name": "a__b",+ "tools": [{"type": "function", "name": "c", "parameters": {}}],+ },+ {+ "type": "namespace",+ "name": "a",+ "tools": [{"type": "function", "name": "b__c", "parameters": {}}],+ },+ ],+ },+ ),+ {},+ TranslationError,+ id="namespace-tool-name-collision-anthropic",+ ),pytest.param("encode_response",("openai_chat", {}),