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
feat(server): return outcome metadata from /v1/decision (#1122)
Chinmay Gabelfb89e12ba70b
diff --git a/crates/libsy/src/core/outcome_metadata.rs b/crates/libsy/src/core/outcome_metadata.rsindex ee8277727..2ae10f106 100644--- a/crates/libsy/src/core/outcome_metadata.rs+++ b/crates/libsy/src/core/outcome_metadata.rs@@ -6,7 +6,7 @@use serde_json::Value;/// Identity and optional algorithm evidence attached to a successful routing outcome.-#[derive(Clone, Debug, PartialEq, Eq)]+#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]pub struct OutcomeMetadata {outcome_id: String,/// Stable name of the algorithm that produced the outcome.diff --git a/crates/switchyard-runner/src/runner.rs b/crates/switchyard-runner/src/runner.rsindex f69712c29..0ddf249b8 100644--- a/crates/switchyard-runner/src/runner.rs+++ b/crates/switchyard-runner/src/runner.rs@@ -35,6 +35,8 @@ pub struct ModelInfo<'a> {pub struct DecisionDescription {pub selected: DecisionTarget,pub fallbacks: Vec<DecisionTarget>,+ /// Metadata the algorithm attached to the routing outcome, if any.+ pub metadata: Option<libsy::OutcomeMetadata>,}/// Non-secret configured target details returned by the decision endpoint.@@ -163,6 +165,7 @@ impl Runner {Some(DecisionDescription {selected: resolve(model_ids.next()?)?,fallbacks: model_ids.map(resolve).collect::<Option<Vec<_>>>()?,+ metadata: outcome.metadata.clone(),})}}diff --git a/crates/switchyard-server/README.md b/crates/switchyard-server/README.mdindex fd71a5c8f..c3edb7454 100644--- a/crates/switchyard-server/README.md+++ b/crates/switchyard-server/README.md@@ -345,7 +345,9 @@ returns the selected target and ordered fallbacks with their model, format, base`extra_body`. It does not make a post-routing answer call. Routing-time calls still execute, andresponse-dependent algorithms such as escalation and advisor routing may produce an answer whiledeciding. When they do, the endpoint includes the buffered answer as `response`, encoded in-`input_format`; otherwise the field is omitted.+`input_format`; otherwise the field is omitted. When the algorithm attaches outcome metadata, the+endpoint returns it as `metadata`, with the outcome ID, the algorithm name and the algorithm's+evidence.For `stage_router`, `algorithm_stats.stage_router` groups routing decisions by source and semantictarget and summarizes its score, confidence, and input-dimension histograms. These values resetdiff --git a/crates/switchyard-server/src/lib.rs b/crates/switchyard-server/src/lib.rsindex b27dc20b0..d34b5222d 100644--- a/crates/switchyard-server/src/lib.rs+++ b/crates/switchyard-server/src/lib.rs@@ -140,6 +140,8 @@ struct DecisionResponse {selected: DecisionTargetResponse,fallbacks: Vec<DecisionTargetResponse>,#[serde(skip_serializing_if = "Option::is_none")]+ metadata: Option<libsy::OutcomeMetadata>,+ #[serde(skip_serializing_if = "Option::is_none")]response: Option<Value>,}@@ -284,6 +286,7 @@ impl ServerState {Some(DecisionResponse {selected: convert(description.selected),fallbacks: description.fallbacks.into_iter().map(convert).collect(),+ metadata: description.metadata,response,})}diff --git a/crates/switchyard-server/tests/server.rs b/crates/switchyard-server/tests/server.rsindex 51299552f..7261261d8 100644--- a/crates/switchyard-server/tests/server.rs+++ b/crates/switchyard-server/tests/server.rs@@ -2432,8 +2432,13 @@ escalation = {{ confirmations = 1 }}.await?;assert_eq!(response.status, StatusCode::OK);+ let mut body = response.json()?;+ let outcome_id = body["metadata"]+ .as_object_mut()+ .and_then(|metadata| metadata.remove("outcome_id"));+ assert!(outcome_id.is_some_and(|id| id.as_str().is_some_and(|id| !id.is_empty())));assert_eq!(- response.json()?,+ body,json!({"selected": {"target": "economy",@@ -2453,6 +2458,10 @@ escalation = {{ confirmations = 1 }}},"extra_body": {},}],+ "metadata": {+ "algorithm": "llm_task_classifier",+ "evidence": {"source": "llm-classifier", "score": 0.9, "threshold": 0.5},+ },}));assert_eq!(