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 cross-provider abuse attribution identity (#756)

Graham King56828064f3b6
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
index 72f101de4..a0f1be95c 100644
--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
@@ -158,8 +158,21 @@ impl FormatCodec for AnthropicMessagesCodec {
"output_config",
"output_format",
"stream",
+ // OpenAI's opaque abuse-attribution ID; derive it from Anthropic metadata below.
+ "safety_identifier",
],
);
+ // OpenAI codecs share this extension for abuse attribution.
+ if let Some(user_id) = body
+ .get("metadata")
+ .and_then(|metadata| metadata.get("user_id"))
+ .and_then(Value::as_str)
+ {
+ request
+ .extensions
+ .fields
+ .insert("safety_identifier".to_string(), json!(user_id));
+ }
request
.extensions
.fields
@@ -237,6 +250,13 @@ impl FormatCodec for AnthropicMessagesCodec {
body.insert(field.to_string(), value.clone());
}
}
+ } else if let Some(identity) = request
+ .extensions
+ .fields
+ .get("safety_identifier")
+ .and_then(Value::as_str)
+ {
+ body.insert("metadata".to_string(), json!({"user_id": identity}));
}
if let Some(stop_sequences) =
anthropic_stop_sequences_from_extensions(&request.extensions.fields)
diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rs
index 06994f311..9cb06ea40 100644
--- a/crates/switchyard-translation/tests/request_translation.rs
+++ b/crates/switchyard-translation/tests/request_translation.rs
@@ -16,6 +16,59 @@ use common::{REASONING_MODEL, normalized_policy, shell_tool_call};
type TestResult<T = ()> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
+#[test]
+fn abuse_identity_survives_request_translation() -> TestResult {
+ let engine = TranslationEngine::default();
+ let formats = [
+ WireFormat::OpenAiChat,
+ WireFormat::OpenAiResponses,
+ WireFormat::AnthropicMessages,
+ ];
+ for policy in [TranslationPolicy::default(), normalized_policy()] {
+ for source in formats {
+ for identity in [Some("opaque-user-6cc3d8d5"), None] {
+ let mut body = match source {
+ WireFormat::OpenAiChat => json!({
+ "messages": [{"role": "user", "content": "hi"}]
+ }),
+ WireFormat::OpenAiResponses => json!({"input": "hi"}),
+ WireFormat::AnthropicMessages => json!({
+ "messages": [{"role": "user", "content": "hi"}],
+ "max_tokens": 8
+ }),
+ };
+ if source == WireFormat::AnthropicMessages {
+ if let Some(identity) = identity {
+ body["metadata"] = json!({"user_id": identity});
+ }
+ } else {
+ // OpenAI metadata is not an abuse-attribution identifier.
+ body["metadata"] = json!({"user_id": "unrelated", "label": "keep"});
+ if let Some(identity) = identity {
+ body["safety_identifier"] = json!(identity);
+ }
+ }
+ let request = engine.decode_request(source, &body, &policy)?.request;
+ for target in formats {
+ let output = engine.encode_request(target, &request, &policy)?.body;
+ let actual = if target == WireFormat::AnthropicMessages {
+ &output["metadata"]["user_id"]
+ } else {
+ &output["safety_identifier"]
+ };
+ assert_eq!(actual, &json!(identity), "{source:?} -> {target:?}");
+ if source != WireFormat::AnthropicMessages
+ && target != WireFormat::AnthropicMessages
+ {
+ assert_eq!(output["metadata"], body["metadata"]);
+ }
+ }
+ }
+ }
+ }
+ Ok(())
+}
+
// Verifies target preparation restamps the model, prepends native instructions, and preserves all
// other provider fields.
#[test]