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: re-emit captured provider extensions when encoding to the Responses format (#509)

thossullivan053a61e2c43b
diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rs
index 32d062cb5..b3a7f7f2c 100644
--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs
@@ -194,6 +194,8 @@ impl FormatCodec for OpenAiResponsesCodec {
if request.stream {
body.insert("stream".to_string(), Value::Bool(true));
}
+ copy_responses_request_extensions(&mut body, &request.extensions.fields);
+
let body = embed_preservation(Value::Object(body), &request.preservation, _policy);
Ok(EncodedRequest { body, diagnostics })
}
@@ -1463,3 +1465,31 @@ fn encode_responses_usage(usage: &Usage) -> Value {
"output_tokens_details": {"reasoning_tokens": usage.reasoning_tokens.unwrap_or(0)},
})
}
+
+/// Re-emits captured request extensions that the Responses format accepts.
+///
+/// The allowlist is Responses-specific: Chat-only fields such as
+/// `stream_options` and `top_logprobs` are deliberately absent, so they stay
+/// dropped on a Chat-to-Responses hop. Fields already present in `body` are
+/// left untouched — anything the encoder generated is authoritative over a
+/// captured extension of the same name.
+fn copy_responses_request_extensions(
+ body: &mut Map<String, Value>,
+ extensions: &Map<String, Value>,
+) {
+ for field in [
+ "metadata",
+ "parallel_tool_calls",
+ "prompt_cache_key",
+ "prompt_cache_retention",
+ "safety_identifier",
+ "service_tier",
+ "store",
+ "user",
+ ] {
+ if let Some(value) = extensions.get(field) {
+ body.entry(field.to_string())
+ .or_insert_with(|| value.clone());
+ }
+ }
+}
diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rs
index 4cb546a7c..56ae8d6ea 100644
--- a/crates/switchyard-translation/tests/request_translation.rs
+++ b/crates/switchyard-translation/tests/request_translation.rs
@@ -960,7 +960,49 @@ fn responses_deferred_message_stays_after_matching_tool_result_for_openai_chat()
Ok(())
}
-// Verifies Chat-compatible Responses extension fields survive translation.
+// Verifies Responses-compatible extension fields survive a Chat-to-Responses
+// hop, and that Chat-only fields are excluded rather than passed through.
+#[test]
+fn chat_compatible_extensions_survive_to_responses() -> TestResult {
+ let engine = TranslationEngine::default();
+ let body = json!({
+ "model": "gpt-4",
+ "messages": [{"role": "user", "content": "hi"}],
+ "metadata": {"trace": "abc"},
+ "parallel_tool_calls": false,
+ "prompt_cache_key": "session-1",
+ "prompt_cache_retention": "24h",
+ "safety_identifier": "safe-1",
+ "service_tier": "flex",
+ "store": false,
+ "stream_options": {"include_usage": true},
+ "top_logprobs": 2,
+ "user": "u-123"
+ });
+
+ let output = engine
+ .translate_request(
+ WireFormat::OpenAiChat,
+ WireFormat::OpenAiResponses,
+ &body,
+ &TranslationPolicy::default(),
+ )?
+ .body;
+
+ assert_eq!(output["metadata"], json!({"trace": "abc"}));
+ assert_eq!(output["parallel_tool_calls"], false);
+ assert_eq!(output["prompt_cache_key"], "session-1");
+ assert_eq!(output["prompt_cache_retention"], "24h");
+ assert_eq!(output["safety_identifier"], "safe-1");
+ assert_eq!(output["service_tier"], "flex");
+ assert_eq!(output["store"], false);
+ assert_eq!(output["user"], "u-123");
+ // Chat-only fields are not in the Responses allowlist, so they stay dropped.
+ assert!(output.get("stream_options").is_none());
+ assert!(output.get("top_logprobs").is_none());
+ Ok(())
+}
+
#[test]
fn responses_chat_compatible_extensions_survive_to_openai_chat() -> TestResult {
let engine = TranslationEngine::default();