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): stop mapping cross-provider identity fields (#810)

Graham King86ed2acac4aa
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
index 67f03ebc4..646c33e47 100644
--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
@@ -159,7 +159,7 @@ impl FormatCodec for AnthropicMessagesCodec {
"output_config",
"output_format",
"stream",
- // OpenAI's opaque abuse-attribution ID; derive it from Anthropic metadata below.
+ // OpenAI-only identity fields must not leak through Anthropic decoding.
"safety_identifier",
],
);
@@ -173,17 +173,6 @@ impl FormatCodec for AnthropicMessagesCodec {
.fields
.insert("parallel_tool_calls".to_string(), Value::Bool(!is_disabled));
}
- // 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
@@ -278,13 +267,6 @@ 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 af96d4d10..35e963de5 100644
--- a/crates/switchyard-translation/tests/request_translation.rs
+++ b/crates/switchyard-translation/tests/request_translation.rs
@@ -252,7 +252,7 @@ fn request_media_survives_reencoding_or_is_rejected() -> TestResult {
}
#[test]
-fn abuse_identity_survives_request_translation() -> TestResult {
+fn native_abuse_identity_is_preserved_but_not_mapped() -> TestResult {
let engine = TranslationEngine::default();
let formats = [
WireFormat::OpenAiChat,
@@ -261,7 +261,12 @@ fn abuse_identity_survives_request_translation() -> TestResult {
];
for policy in [TranslationPolicy::default(), normalized_policy()] {
for source in formats {
- for identity in [Some("opaque-user-6cc3d8d5"), None] {
+ let long_identity = "u".repeat(150);
+ for identity in [
+ Some("opaque-user-6cc3d8d5"),
+ Some(long_identity.as_str()),
+ None,
+ ] {
let mut body = match source {
WireFormat::OpenAiChat => json!({
"messages": [{"role": "user", "content": "hi"}]
@@ -287,11 +292,20 @@ fn abuse_identity_survives_request_translation() -> TestResult {
for target in formats {
let output = engine.encode_request(target, &request, &policy)?.body;
let actual = if target == WireFormat::AnthropicMessages {
- &output["metadata"]["user_id"]
+ output
+ .get("metadata")
+ .and_then(|metadata| metadata.get("user_id"))
} else {
- &output["safety_identifier"]
+ output.get("safety_identifier")
};
- assert_eq!(actual, &json!(identity), "{source:?} -> {target:?}");
+ let crosses_anthropic = (source == WireFormat::AnthropicMessages)
+ != (target == WireFormat::AnthropicMessages);
+ let expected = if crosses_anthropic { None } else { identity };
+ assert_eq!(
+ actual,
+ expected.map(|value| json!(value)).as_ref(),
+ "{source:?} -> {target:?}"
+ );
if source != WireFormat::AnthropicMessages
&& target != WireFormat::AnthropicMessages
{