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): decode flat Responses input_file payloads (#590)

Oleksandr Proninbb011ca452c2
diff --git a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
index 4b0394f43..988cd9768 100644
--- a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
@@ -588,6 +588,11 @@ pub(crate) fn decode_image_source(block: &Map<String, Value>) -> Option<ImageSou
}
/// Decodes OpenAI file block shapes into normalized file sources.
+///
+/// Accepts both the Chat shape, which nests the payload under a `file` object,
+/// and the Responses shape, which carries `file_id`/`file_data`/`filename`
+/// directly on the block. Returns `FileSource::Raw` when neither shape carries a
+/// field this can normalize.
pub(crate) fn decode_file_source(block: &Map<String, Value>) -> FileSource {
if let Some(file) = block.get("file").and_then(Value::as_object) {
if let Some(file_id) = file.get("file_id").and_then(Value::as_str) {
@@ -604,9 +609,20 @@ pub(crate) fn decode_file_source(block: &Map<String, Value>) -> FileSource {
}
return FileSource::Raw(Value::Object(file.clone()));
}
+ // A Chat block never reaches here with these keys, having matched the nested
+ // branch above.
if let Some(file_id) = block.get("file_id").and_then(Value::as_str) {
return FileSource::FileId(file_id.to_string());
}
+ if let Some(file_data) = block.get("file_data").and_then(Value::as_str) {
+ return FileSource::FileData {
+ data: file_data.to_string(),
+ filename: block
+ .get("filename")
+ .and_then(Value::as_str)
+ .map(ToOwned::to_owned),
+ };
+ }
FileSource::Raw(Value::Object(block.clone()))
}
diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rs
index 49ae5184a..22b710b05 100644
--- a/crates/switchyard-translation/tests/request_translation.rs
+++ b/crates/switchyard-translation/tests/request_translation.rs
@@ -2301,3 +2301,50 @@ fn anthropic_thinking_is_dropped_from_responses_input() -> TestResult {
);
Ok(())
}
+
+// Verifies a flat Responses `input_file` decodes instead of being dropped.
+//
+// The Responses wire carries `file_data`/`filename` directly on the block, while
+// `decode_file_source` read a direct `file_id` but not a direct `file_data`, so a
+// Responses file fell through to `FileSource::Raw`. Chat's raw file encoder maps
+// only Anthropic `document` blocks and returns `None` otherwise -- so the file did
+// not merely lose its filename, it vanished from the request entirely.
+#[test]
+fn responses_flat_file_data_survives_into_chat() -> TestResult {
+ let engine = TranslationEngine::default();
+ let body = json!({
+ "model": "gpt-5.4-mini",
+ "input": [{
+ "type": "message",
+ "role": "user",
+ "content": [
+ {"type": "input_text", "text": "read this"},
+ {
+ "type": "input_file",
+ "file_data": "JVBERi0xLjQK",
+ "filename": "report.pdf"
+ }
+ ]
+ }]
+ });
+
+ let output = engine
+ .translate_request(
+ WireFormat::OpenAiResponses,
+ WireFormat::OpenAiChat,
+ &body,
+ &TranslationPolicy::default(),
+ )?
+ .body;
+
+ let content = output["messages"][0]["content"]
+ .as_array()
+ .ok_or("Chat content should be an array")?;
+ let file = content
+ .iter()
+ .find(|block| block["type"] == "file")
+ .ok_or("the file must survive into Chat, not be dropped as unmappable raw")?;
+ assert_eq!(file["file"]["file_data"], "JVBERi0xLjQK");
+ assert_eq!(file["file"]["filename"], "report.pdf");
+ Ok(())
+}