Skip to content

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 OpenAI tool strictness for Anthropic (#600)

Alex Fourniercb98af05775f
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
index 394d2b977..dd453218d 100644
--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
@@ -1044,11 +1044,15 @@ fn encode_anthropic_tools(tools: &[ToolDefinition]) -> Value {
tools
.iter()
.map(|tool| {
- json!({
+ let mut item = json!({
"name": tool.name,
"description": tool.description.clone().unwrap_or_default(),
"input_schema": tool.parameters,
- })
+ });
+ if let Some(strict) = tool.strict {
+ item["strict"] = Value::Bool(strict);
+ }
+ item
})
.collect(),
)
diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rs
index 153868b76..0c3565cb1 100644
--- a/crates/switchyard-translation/tests/request_translation.rs
+++ b/crates/switchyard-translation/tests/request_translation.rs
@@ -1992,6 +1992,44 @@ fn openai_schema_constraints_are_removed_from_anthropic_output_format() -> TestR
Ok(())
}
+// Verifies OpenAI tool strictness survives translation to Anthropic.
+#[test]
+fn openai_tool_strictness_is_preserved_for_anthropic() -> TestResult {
+ let engine = TranslationEngine::default();
+ let body = json!({
+ "model": "gpt-5",
+ "messages": [{"role": "user", "content": "Use a tool."}],
+ "tools": [
+ {
+ "type": "function",
+ "function": {"name": "strict", "parameters": {}, "strict": true}
+ },
+ {
+ "type": "function",
+ "function": {"name": "non_strict", "parameters": {}, "strict": false}
+ },
+ {
+ "type": "function",
+ "function": {"name": "unspecified", "parameters": {}}
+ }
+ ]
+ });
+
+ let output = engine
+ .translate_request(
+ WireFormat::OpenAiChat,
+ WireFormat::AnthropicMessages,
+ &body,
+ &TranslationPolicy::default(),
+ )?
+ .body;
+
+ assert_eq!(output["tools"][0]["strict"], true);
+ assert_eq!(output["tools"][1]["strict"], false);
+ assert!(output["tools"][2].get("strict").is_none());
+ Ok(())
+}
+
// Verifies Anthropic-bound OpenAI requests get the required max_tokens fallback.
#[test]
fn openai_request_to_anthropic_adds_required_default_max_tokens() -> TestResult {