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): preserve JSON rejection statuses (#406)
Elias Shieh11a82f98fdd9
diff --git a/crates/switchyard-server/src/lib.rs b/crates/switchyard-server/src/lib.rsindex 24f1026cc..e302e29dd 100644--- a/crates/switchyard-server/src/lib.rs+++ b/crates/switchyard-server/src/lib.rs@@ -572,7 +572,9 @@ async fn anthropic_count_tokens() -> Response {let body = match llm_json_body(body) {Ok(body) => body,- Err(message) => return anthropic_error_response(invalid_body_error(message)),+ Err((status, message)) => {+ return anthropic_error_response(invalid_body_error(status, message));+ }};let (route, request) = match resolve_route(&state,@@ -658,7 +660,7 @@ async fn handle_endpoint_inner().await}- Err(message) => invalid_body_error(message),+ Err((status, message)) => invalid_body_error(status, message),};let response = render_error_response(response, wire_format);metrics::record_client_response(response.status().as_u16());@@ -668,11 +670,17 @@ async fn handle_endpoint_inner(fn llm_json_body(body: std::result::Result<Json<Value>, JsonRejection>,-) -> std::result::Result<Value, String> {+) -> std::result::Result<Value, (StatusCode, String)> {match body {Ok(Json(value)) if value.is_object() => Ok(value),- Ok(_) => Err("Request body must be a JSON object".to_string()),- Err(error) => Err(format!("Request body must be valid JSON: {error}")),+ Ok(_) => Err((+ StatusCode::BAD_REQUEST,+ "Request body must be a JSON object".to_string(),+ )),+ Err(error) => Err((+ error.status(),+ format!("Request body must be valid JSON: {error}"),+ )),}}@@ -690,7 +698,7 @@ fn resolve_route(wire_format: WireFormat,) -> std::result::Result<(&RouteEntry, Request), Response> {let llm_request = decode_request(wire_format, &body)- .map_err(|error| invalid_body_error(error.to_string()))?;+ .map_err(|error| invalid_body_error(StatusCode::BAD_REQUEST, error.to_string()))?;let requested_model = llm_request.model.clone()@@ -1037,13 +1045,8 @@ fn server_error(message: impl Into<String>) -> Response {)}-fn invalid_body_error(message: impl Into<String>) -> Response {- error_response(- StatusCode::BAD_REQUEST,- message,- "invalid_request_error",- "invalid_body",- )+fn invalid_body_error(status: StatusCode, message: impl Into<String>) -> Response {+ error_response(status, message, "invalid_request_error", "invalid_body")}fn error_response(diff --git a/crates/switchyard-server/tests/server.rs b/crates/switchyard-server/tests/server.rsindex 3992837bf..fc15ba3f8 100644--- a/crates/switchyard-server/tests/server.rs+++ b/crates/switchyard-server/tests/server.rs@@ -25,7 +25,7 @@ use switchyard_llm_client::{use switchyard_protocol::ModelId;use switchyard_protocol::RoutedLlmClient;use switchyard_server::config::load_server_state;-use switchyard_server::{ServerState, build_switchyard_router};+use switchyard_server::{DEFAULT_MAX_REQUEST_BODY_BYTES, ServerState, build_switchyard_router};use tokio::net::TcpListener;use tokio::sync::Mutex;use tokio::task::JoinHandle;@@ -771,6 +771,27 @@ async fn send_with_headers(})}+async fn send_raw_json(+ app: &Router,+ path: &str,+ body: Vec<u8>,+ content_type: Option<&str>,+) -> TestResult<Response> {+ let mut builder = HttpRequest::builder().method("POST").uri(path);+ if let Some(content_type) = content_type {+ builder = builder.header("content-type", content_type);+ }+ let response = app.clone().oneshot(builder.body(Body::from(body))?).await?;+ let status = response.status();+ let headers = response.headers().clone();+ let bytes = response.into_body().collect().await?.to_bytes();+ Ok(Response {+ status,+ headers,+ bytes,+ })+}+struct Response {status: StatusCode,headers: axum::http::HeaderMap,@@ -1763,6 +1784,72 @@ async fn routes_dispatch_and_discovery_endpoints_are_stable() -> TestResult {Ok(())}+#[tokio::test]+async fn json_extractor_statuses_keep_api_specific_error_envelopes() -> TestResult {+ let (_upstream, app) = test_app(&[(ROUTE_MODEL, &["model/a"])]).await?;++ for (content_type, expected_status) in [+ (Some("application/json"), StatusCode::BAD_REQUEST),+ (None, StatusCode::UNSUPPORTED_MEDIA_TYPE),+ (Some("text/plain"), StatusCode::UNSUPPORTED_MEDIA_TYPE),+ ] {+ let body = if expected_status == StatusCode::BAD_REQUEST {+ br#"{"model":"broken""#.to_vec()+ } else {+ br#"{"model":"valid-json"}"#.to_vec()+ };+ let response = send_raw_json(&app, "/v1/chat/completions", body, content_type).await?;+ assert_eq!(response.status, expected_status);+ let body = response.json()?;+ assert_eq!(body["error"]["type"], "invalid_request_error");+ assert_eq!(body["error"]["code"], "invalid_body");+ }++ let response = send_raw_json(+ &app,+ "/v1/chat/completions",+ vec![b' '; DEFAULT_MAX_REQUEST_BODY_BYTES + 1],+ Some("application/json"),+ )+ .await?;+ assert_eq!(response.status, StatusCode::PAYLOAD_TOO_LARGE);+ assert_eq!(response.json()?["error"]["code"], "invalid_body");++ for (body, content_type, expected_status, expected_type) in [+ (+ br#"{"model":"broken""#.as_slice(),+ Some("application/json"),+ StatusCode::BAD_REQUEST,+ "invalid_request_error",+ ),+ (+ br#"{"model":"valid-json"}"#.as_slice(),+ None,+ StatusCode::UNSUPPORTED_MEDIA_TYPE,+ "api_error",+ ),+ ] {+ let response = send_raw_json(&app, "/v1/messages", body.to_vec(), content_type).await?;+ assert_eq!(response.status, expected_status);+ let body = response.json()?;+ assert_eq!(body["type"], "error");+ assert_eq!(body["error"]["type"], expected_type);+ }++ let response = send_raw_json(+ &app,+ "/v1/messages",+ vec![b' '; DEFAULT_MAX_REQUEST_BODY_BYTES + 1],+ Some("application/json"),+ )+ .await?;+ assert_eq!(response.status, StatusCode::PAYLOAD_TOO_LARGE);+ let body = response.json()?;+ assert_eq!(body["type"], "error");+ assert_eq!(body["error"]["type"], "request_too_large");+ Ok(())+}+#[tokio::test]async fn models_endpoint_reports_declared_route_capabilities_and_null_when_undeclared() -> TestResult{