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 malformed request fields (#287)
nachiketb-nvidiaeea4e01f4d97
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rsindex b45902a51..37aa7eb3e 100644--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs@@ -39,6 +39,17 @@ impl FormatCodec for AnthropicMessagesCodec {fn decode_request(&self, body: &Value, policy: &TranslationPolicy) -> Result<DecodedRequest> {let body = crate::util::object(body, "$")?;let mut diagnostics = Vec::new();+ let max_output_tokens = body+ .get("max_tokens")+ .map(|value| {+ value+ .as_u64()+ .ok_or_else(|| TranslationError::InvalidValue {+ path: "$.max_tokens".to_string(),+ message: "expected a non-negative integer".to_string(),+ })+ })+ .transpose()?;let mut request = LlmRequest {model: body.get("model")@@ -46,7 +57,7 @@ impl FormatCodec for AnthropicMessagesCodec {.filter(|model| !model.is_empty()).map(ToOwned::to_owned),output: OutputParams {- max_output_tokens: body.get("max_tokens").and_then(Value::as_u64),+ max_output_tokens,response_format: None,},sampling: SamplingParams {@@ -72,7 +83,7 @@ impl FormatCodec for AnthropicMessagesCodec {..LlmRequest::default()};if let Some(system) = body.get("system")- && let Some(content) = decode_anthropic_system(system, &mut diagnostics, policy)?+ && let Some(content) = decode_anthropic_system(system)?{request.instructions.push(InstructionBlock {role: Role::System,@@ -335,11 +346,7 @@ impl FormatCodec for AnthropicMessagesCodec {}// Decodes Anthropic's `system` field into instruction blocks.-fn decode_anthropic_system(- value: &Value,- diagnostics: &mut Vec<TranslationDiagnostic>,- policy: &TranslationPolicy,-) -> Result<Option<Vec<ContentBlock>>> {+fn decode_anthropic_system(value: &Value) -> Result<Option<Vec<ContentBlock>>> {match value {Value::String(text) if !text.is_empty() => {Ok(Some(vec![ContentBlock::Text { text: text.clone() }]))@@ -361,12 +368,10 @@ fn decode_anthropic_system(}Ok((!content.is_empty()).then_some(content))}- other => {- push_lossy(diagnostics, policy, "Anthropic system field was not text")?;- Ok(Some(vec![ContentBlock::Text {- text: string_value(other).unwrap_or_default(),- }]))- }+ _ => Err(TranslationError::InvalidType {+ path: "$.system".to_string(),+ expected: "string or array of text blocks",+ }),}}diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rsindex 466590913..0bc964e19 100644--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs@@ -442,10 +442,10 @@ fn decode_responses_input(}Ok(messages)}- other => Ok(vec![Message::text(- Role::User,- string_value(other).unwrap_or_default(),- )]),+ _ => Err(TranslationError::InvalidType {+ path: "$.input".to_string(),+ expected: "string or array",+ }),}}diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rsindex 37d9fd8ec..164dc5fc0 100644--- a/crates/switchyard-translation/tests/request_translation.rs+++ b/crates/switchyard-translation/tests/request_translation.rs@@ -1091,6 +1091,71 @@ fn json_contains_content_type(value: &Value, expected: &str) -> bool {}}+// Malformed provider fields must fail before normalization can change their meaning.+#[test]+fn malformed_request_fields_are_rejected() {+ let engine = TranslationEngine::default();+ let cases = [+ (+ "Anthropic object system",+ WireFormat::AnthropicMessages,+ json!({"model": "claude", "max_tokens": 8, "system": {}, "messages": []}),+ "expected string or array of text blocks at $.system",+ ),+ (+ "Anthropic boolean system",+ WireFormat::AnthropicMessages,+ json!({"model": "claude", "max_tokens": 8, "system": true, "messages": []}),+ "expected string or array of text blocks at $.system",+ ),+ (+ "Anthropic negative max_tokens",+ WireFormat::AnthropicMessages,+ json!({"model": "claude", "max_tokens": -1, "messages": []}),+ "invalid value at $.max_tokens: expected a non-negative integer",+ ),+ (+ "Anthropic string max_tokens",+ WireFormat::AnthropicMessages,+ json!({"model": "claude", "max_tokens": "8", "messages": []}),+ "invalid value at $.max_tokens: expected a non-negative integer",+ ),+ (+ "Responses boolean input",+ WireFormat::OpenAiResponses,+ json!({"model": "gpt", "input": true}),+ "expected string or array at $.input",+ ),+ (+ "Responses null input",+ WireFormat::OpenAiResponses,+ json!({"model": "gpt", "input": null}),+ "expected string or array at $.input",+ ),+ ];++ for (case, format, body, expected) in cases {+ match engine.decode_request(format, &body, &TranslationPolicy::default()) {+ Ok(_) => panic!("{case} should be rejected"),+ Err(error) => assert_eq!(error.to_string(), expected, "{case}"),+ }+ }++ let valid_empty_output = json!({+ "model": "claude",+ "max_tokens": 0,+ "system": null,+ "messages": []+ });+ if let Err(error) = engine.decode_request(+ WireFormat::AnthropicMessages,+ &valid_empty_output,+ &TranslationPolicy::default(),+ ) {+ panic!("Anthropic null system and zero max_tokens should be accepted: {error}");+ }+}+// --- Invalid-role rejection ----------------------------------// A transparent router must reject the same payloads the upstream provider// would, rather than silently coercing an unknown role (e.g. "api") to `user`