diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs index ce8b500667..722f05e761 100644 --- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs +++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs @@ -799,7 +799,7 @@ fn decode_anthropic_content_block( is_error: block.get("is_error").and_then(Value::as_bool), })], Some("image") => vec![ContentBlock::Image { - source: ImageSource::Raw(Value::Object(block.clone())), + source: decode_anthropic_image_source(block), }], Some("input_image") | Some("image_url") => decode_image_source(block) .map(|source| vec![ContentBlock::Image { source }]) @@ -834,7 +834,7 @@ fn decode_tool_result_content(value: &Value) -> Vec { .to_string(), }), Some("image") => content.push(ContentBlock::Image { - source: ImageSource::Raw(Value::Object(block.clone())), + source: decode_anthropic_image_source(block), }), Some("document") => content.push(ContentBlock::File { source: decode_anthropic_file_source(block), @@ -857,6 +857,44 @@ fn decode_tool_result_content(value: &Value) -> Vec { } } +// Normalizes image payloads when every field fits the typed source; otherwise uses ImageSource::Raw. +fn decode_anthropic_image_source(block: &Map) -> ImageSource { + let source = (|| { + // This also preserves cache_control and transformations, even when null. + if block + .keys() + .any(|key| !matches!(key.as_str(), "type" | "source")) + { + return None; + } + let source = block.get("source")?.as_object()?; + match source.get("type")?.as_str()? { + "url" + if source + .keys() + .all(|key| matches!(key.as_str(), "type" | "url")) => + { + Some(ImageSource::Url { + url: source.get("url")?.as_str()?.to_string(), + detail: None, + }) + } + "base64" + if source + .keys() + .all(|key| matches!(key.as_str(), "type" | "media_type" | "data")) => + { + Some(ImageSource::Base64 { + media_type: Some(source.get("media_type")?.as_str()?.to_string()), + data: source.get("data")?.as_str()?.to_string(), + }) + } + _ => None, + } + })(); + source.unwrap_or_else(|| ImageSource::Raw(Value::Object(block.clone()))) +} + // Keeps Anthropic document fields together for same-format re-encoding. fn decode_anthropic_file_source(block: &Map) -> FileSource { 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 d1a4654cef..f6f0e3f2ab 100644 --- a/crates/switchyard-translation/tests/request_translation.rs +++ b/crates/switchyard-translation/tests/request_translation.rs @@ -1587,9 +1587,9 @@ fn anthropic_tool_result_followup_text_splits_to_openai_messages() -> TestResult Ok(()) } -// Verifies Anthropic multimodal blocks retain provider fields inside tool results. +// Verifies Anthropic multimodal blocks decode into the expected IR and retain provider fields. #[test] -fn anthropic_tool_result_multimodal_blocks_round_trip_complete() -> TestResult { +fn anthropic_multimodal_blocks_decode_and_round_trip() -> TestResult { let engine = TranslationEngine::default(); let policy = TranslationPolicy { preservation: switchyard_translation::PreservationPolicy::Disabled, @@ -1606,48 +1606,101 @@ fn anthropic_tool_result_multimodal_blocks_round_trip_complete() -> TestResult { "data": "ZG9jdW1lbnQ=" } }); - let image = json!({ - "type": "image", - "source": { - "type": "base64", - "media_type": "image/png", - "data": "aW1hZ2U=" - } + let mut images = vec![ + ( + json!({ + "type": "image", + "source": {"type": "url", "url": "https://example.test/image.png"} + }), + ImageSource::Url { + url: "https://example.test/image.png".into(), + detail: None, + }, + ), + ( + json!({ + "type": "image", + "source": { + "type": "base64", + "media_type": "image/png", + "data": "aW1hZ2U=" + } + }), + ImageSource::Base64 { + media_type: Some("image/png".into()), + data: "aW1hZ2U=".into(), + }, + ), + ]; + for raw in [ + json!({ + "type": "image", + "source": {"type": "file", "file_id": "file_anthropic_123"} + }), + json!({ + "type": "image", + "source": {"type": "url", "url": "https://example.test/cached.png"}, + "cache_control": {"type": "ephemeral", "ttl": "5m"} + }), + json!({ + "type": "image", + "source": {"type": "base64", "media_type": "image/png", "data": "aW1hZ2U="}, + "transformations": {"oversized_image": "error"} + }), + ] { + images.push((raw.clone(), ImageSource::Raw(raw))); + } + let mut content = vec![json!({"type": "text", "text": "content ready"})]; + content.extend(images.iter().map(|(image, _)| image.clone())); + content.push(document.clone()); + let mut expected_content = vec![ContentBlock::Text { + text: "content ready".into(), + }]; + expected_content.extend( + images + .into_iter() + .map(|(_, source)| ContentBlock::Image { source }), + ); + expected_content.push(ContentBlock::File { + source: switchyard_translation::FileSource::Raw(document), }); - let body = json!({ - "model": "claude-sonnet-4-20250514", - "messages": [{ - "role": "user", - "content": [{ + + for nested in [false, true] { + let message_content = if nested { + json!([{ "type": "tool_result", "tool_use_id": "toolu_document", - "content": [ - {"type": "text", "text": "content ready"}, - image.clone(), - document.clone() - ] - }] - }], - "max_tokens": 1024 - }); - - let output = engine - .translate_request( - WireFormat::AnthropicMessages, - WireFormat::AnthropicMessages, - &body, - &policy, - )? - .body; + "content": content + }]) + } else { + json!(content) + }; + let body = json!({ + "model": "claude-sonnet-4-20250514", + "messages": [{"role": "user", "content": message_content}], + "max_tokens": 1024 + }); + let request = engine + .decode_request(WireFormat::AnthropicMessages, &body, &policy)? + .request; + let decoded_content = if nested { + let ContentBlock::ToolResult(result) = &request.messages[0].content[0] else { + return Err("expected a tool result".into()); + }; + &result.content + } else { + &request.messages[0].content + }; + assert_eq!(decoded_content, &expected_content, "nested={nested}"); - assert_eq!( - output["messages"][0]["content"][0]["content"], - json!([ - {"type": "text", "text": "content ready"}, - image, - document - ]) - ); + let output = engine + .encode_request(WireFormat::AnthropicMessages, &request, &policy)? + .body; + assert_eq!( + output["messages"][0]["content"], message_content, + "nested={nested}" + ); + } Ok(()) }