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 Anthropic schema property names (#1288)
Rainaxyzf997c5601df7
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rsindex 5639f1765..9437e33ad 100644--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs@@ -605,8 +605,37 @@ fn strip_anthropic_unsupported_constraints(value: &mut Value) -> bool {for key in ["minimum", "maximum", "minLength", "maxLength"] {removed |= object.remove(key).is_some();}- for value in object.values_mut() {- removed |= strip_anthropic_unsupported_constraints(value);+ // Property/definition names and literal values are not schema keywords.+ for (key, value) in object.iter_mut() {+ match key.as_str() {+ "properties" | "patternProperties" | "$defs" | "definitions"+ | "dependentSchemas" | "dependencies" => {+ if let Value::Object(schemas) = value {+ for schema in schemas.values_mut() {+ removed |= strip_anthropic_unsupported_constraints(schema);+ }+ }+ }+ "items"+ | "additionalItems"+ | "prefixItems"+ | "contains"+ | "additionalProperties"+ | "unevaluatedItems"+ | "unevaluatedProperties"+ | "propertyNames"+ | "allOf"+ | "anyOf"+ | "oneOf"+ | "not"+ | "if"+ | "then"+ | "else"+ | "contentSchema" => {+ removed |= strip_anthropic_unsupported_constraints(value);+ }+ _ => {}+ }}removed}diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rsindex b613215c5..d025b4182 100644--- a/crates/switchyard-translation/tests/request_translation.rs+++ b/crates/switchyard-translation/tests/request_translation.rs@@ -3679,6 +3679,72 @@ fn openai_data_uri_images_translate_to_anthropic_sources() -> TestResult {Ok(())}+#[test]+fn anthropic_output_schema_preserves_keyword_names_and_literal_values() -> TestResult {+ let engine = TranslationEngine::default();+ let schema = json!({+ "type": "object",+ "properties": {+ "minimum": {"type": "integer"},+ "maximum": {"type": "integer"},+ "minLength": {"type": "string"},+ "maxLength": {"type": "string"},+ "nested": {+ "type": "array",+ "items": {+ "type": "object",+ "properties": {"minimum": {"type": "integer"}},+ "required": ["minimum"],+ "additionalProperties": false+ }+ },+ "literal": {+ "enum": [{"minimum": 1, "maximum": 2}],+ "const": {"minimum": 1, "maximum": 2},+ "default": {"maxLength": 4}+ }+ },+ "$defs": {"minimum": {"type": "integer"}},+ "required": ["minimum", "maximum", "minLength", "maxLength"],+ "additionalProperties": false+ });+ for source in [WireFormat::OpenAiChat, WireFormat::OpenAiResponses] {+ let body = if source == WireFormat::OpenAiChat {+ json!({+ "model": "route", "messages": [{"role": "user", "content": "Return JSON."}],+ "response_format": {"type": "json_schema", "json_schema": {+ "name": "result", "strict": true, "schema": schema+ }}+ })+ } else {+ json!({+ "model": "route", "input": "Return JSON.",+ "text": {"format": {+ "type": "json_schema", "name": "result", "strict": true, "schema": schema+ }}+ })+ };+ for lossy_conversion_policy in [+ LossyConversionPolicy::AllowWithDiagnostics,+ LossyConversionPolicy::Reject,+ ] {+ let policy = TranslationPolicy {+ lossy_conversion_policy,+ ..TranslationPolicy::default()+ };+ let translated =+ engine.translate_request(source, WireFormat::AnthropicMessages, &body, &policy)?;+ assert_eq!(translated.body["output_config"]["format"]["schema"], schema);+ assert!(+ translated.diagnostics.is_empty(),+ "{:?}",+ translated.diagnostics+ );+ }+ }+ Ok(())+}+// Verifies Anthropic receives its supported schema subset without mutating the neutral contract.#[test]fn openai_schema_constraints_are_removed_from_anthropic_output_format() -> TestResult {@@ -3695,8 +3761,17 @@ fn openai_schema_constraints_are_removed_from_anthropic_output_format() -> TestR"type": "object","properties": {"crux": {"type": "string", "minLength": 1, "maxLength": 80},- "p_solve": {"type": "number", "minimum": 0, "maximum": 1}+ "p_solve": {"type": "number", "minimum": 0, "maximum": 1},+ "nested": {+ "type": "array",+ "items": {"anyOf": [+ {"type": "number", "minimum": 0},+ {"type": "string", "maxLength": 80}+ ]}+ }},+ "$defs": {"maximum": {"type": "number", "maximum": 1}},+ "allOf": [{"properties": {"crux": {"minLength": 1}}}],"required": ["crux", "p_solve"],"additionalProperties": false}@@ -3717,8 +3792,14 @@ fn openai_schema_constraints_are_removed_from_anthropic_output_format() -> TestR"type": "object","properties": {"crux": {"type": "string"},- "p_solve": {"type": "number"}+ "p_solve": {"type": "number"},+ "nested": {+ "type": "array",+ "items": {"anyOf": [{"type": "number"}, {"type": "string"}]}+ }},+ "$defs": {"maximum": {"type": "number"}},+ "allOf": [{"properties": {"crux": {}}}],"required": ["crux", "p_solve"],"additionalProperties": false})@@ -3733,6 +3814,20 @@ fn openai_schema_constraints_are_removed_from_anthropic_output_format() -> TestRbody["response_format"]["json_schema"]["schema"]["properties"]["p_solve"]["minimum"],0);+ let strict = TranslationPolicy {+ lossy_conversion_policy: LossyConversionPolicy::Reject,+ ..TranslationPolicy::default()+ };+ let error = engine+ .translate_request(+ WireFormat::OpenAiChat,+ WireFormat::AnthropicMessages,+ &body,+ &strict,+ )+ .err()+ .ok_or("unsupported schema constraints should be rejected")?;+ assert_eq!(error.kind(), "LossyConversion");Ok(())}