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): encode OpenAI file data as a valid Anthropic document (#715)

eric-liu-nvidiade2bc5428c98
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 55239be30..ca2b3bf06 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -127,7 +127,9 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
direction a Responses `function_call_output` whose `output` was an array of
`input_text`, `input_image`, and `input_file` parts reached an Anthropic
target as one JSON string. Both directions now carry typed text, image, and
- file blocks; plain-text results are unchanged.
+ file blocks; plain-text results are unchanged. OpenAI `file_data` encodes as
+ a valid Anthropic `document`: raw base64 with a `media_type`, the data-URI
+ prefix stripped, and the file name as `title`.
- **Stored Responses tool continuations stay on the selected model** — a
`function_call_output` sent with `previous_response_id`, where the matching
`function_call` lives in provider state, was decoded as ordinary user text. A
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
index dd453218d..d479c2dfb 100644
--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
@@ -935,14 +935,20 @@ fn encode_one_anthropic_block(block: &ContentBlock) -> Vec<Value> {
FileSource::FileId(file_id) => {
json!({"type": "document", "source": {"type": "file", "file_id": file_id}})
}
- FileSource::FileData { data, filename } => json!({
- "type": "document",
- "source": {
- "type": "base64",
- "data": data,
- "filename": filename,
- },
- }),
+ // OpenAI carries `file_data` as raw base64 or a data URI. Anthropic wants raw
+ // base64 with a media type, and takes the name as the block `title`.
+ FileSource::FileData { data, filename } => {
+ let (media_type, data) = split_base64_data_uri(data)
+ .unwrap_or_else(|| (document_media_type(filename.as_deref()), data.as_str()));
+ let mut block = json!({
+ "type": "document",
+ "source": {"type": "base64", "media_type": media_type, "data": data},
+ });
+ if let Some(filename) = filename {
+ block["title"] = Value::String(filename.clone());
+ }
+ block
+ }
FileSource::Raw(raw) => raw.clone(),
}],
ContentBlock::Audio { source } => vec![match source {
@@ -1000,6 +1006,20 @@ fn encode_one_anthropic_tool_result_block(block: &ContentBlock) -> Vec<Value> {
}
}
+// Anthropic accepts PDF and plain-text documents; a file with no media type is a PDF
+// unless its name says otherwise.
+fn document_media_type(filename: Option<&str>) -> &'static str {
+ match filename
+ .and_then(|name| name.rsplit_once('.'))
+ .map(|(_, ext)| ext)
+ {
+ Some(ext) if ext.eq_ignore_ascii_case("txt") || ext.eq_ignore_ascii_case("md") => {
+ "text/plain"
+ }
+ _ => "application/pdf",
+ }
+}
+
// Splits `data:<media type>[;<parameter>...];base64,<payload>`, the form OpenAI-compatible
// clients use for inline images. A percent-encoded payload has to be decoded before it is
// base64, and a data URI with no media type leaves Anthropic's `media_type` with nothing to
diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rs
index 71c2ff740..f03a1622b 100644
--- a/crates/switchyard-translation/tests/request_translation.rs
+++ b/crates/switchyard-translation/tests/request_translation.rs
@@ -3085,7 +3085,11 @@ fn tool_result_media_survives_anthropic_and_responses_translation() -> TestResul
{"type": "function_call_output", "call_id": "call_1", "output": [
{"type": "input_text", "text": "media attached"},
{"type": "input_image", "image_url": "data:image/png;base64,aW1hZ2U=", "detail": "low"},
- {"type": "input_file", "file_data": "ZG9jdW1lbnQ=", "filename": "report.pdf"}
+ {
+ "type": "input_file",
+ "file_data": "data:application/pdf;base64,ZG9jdW1lbnQ=",
+ "filename": "report.pdf"
+ }
]}
]
});
@@ -3110,7 +3114,8 @@ fn tool_result_media_survives_anthropic_and_responses_translation() -> TestResul
},
{
"type": "document",
- "source": {"type": "base64", "data": "ZG9jdW1lbnQ=", "filename": "report.pdf"}
+ "title": "report.pdf",
+ "source": {"type": "base64", "media_type": "application/pdf", "data": "ZG9jdW1lbnQ="}
}
])
);