From c931c8c85d071971b3f5662d5ade5f27f42dcf10 Mon Sep 17 00:00:00 2001 From: Ryan Lempka Date: Fri, 13 Nov 2026 09:45:54 -0600 Subject: [PATCH 1/2] fix(translation): decode buffered Responses tool arguments Signed-off-by: Ryan Lempka --- .../src/codecs/responses/buffered.rs | 10 ++- .../tests/response_translation.rs | 88 ++++++++++++++++++- 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rs index d6f495bee0..8e0797f36e 100644 --- a/crates/switchyard-translation/src/codecs/responses/buffered.rs +++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs @@ -1822,7 +1822,15 @@ fn decode_responses_output_item( .and_then(Value::as_str) .unwrap_or_default() .to_string(), - arguments: item.get("arguments").cloned().unwrap_or_else(|| json!({})), + arguments: item + .get("arguments") + .map(|value| { + value + .as_str() + .and_then(|text| serde_json::from_str(text).ok()) + .unwrap_or_else(|| value.clone()) + }) + .unwrap_or_else(|| json!({})), })], stop_reason: Some(StopReason::ToolUse), })), diff --git a/crates/switchyard-translation/tests/response_translation.rs b/crates/switchyard-translation/tests/response_translation.rs index 121159222b..e2b12c0636 100644 --- a/crates/switchyard-translation/tests/response_translation.rs +++ b/crates/switchyard-translation/tests/response_translation.rs @@ -5,11 +5,12 @@ pub mod common; +use futures::{TryStreamExt, executor::block_on}; use pretty_assertions::assert_eq; -use serde_json::json; +use serde_json::{Value, json}; use switchyard_translation::{ ContentBlock, PreservationPolicy, ProviderExtensions, StopReason, TranslationEngine, - TranslationPolicy, WireFormat, + TranslationPolicy, WireFormat, encode_stream, }; use common::{ @@ -18,6 +19,89 @@ use common::{ type TestResult = std::result::Result<(), Box>; +#[test] +fn responses_tool_arguments_survive_buffered_and_synthetic_encoding() -> TestResult { + let engine = TranslationEngine::default(); + let source = WireFormat::OpenAiResponses; + let policy = TranslationPolicy::default(); + let body = json!({"id": "resp_1", "model": "fixture", "status": "completed", + "output": [{"type": "function_call", "call_id": "call_1", "name": "lookup", + "arguments": "{\"city\": \"Paris\"}"}]}); + let response = engine.decode_response(source, &body, &policy)?.response; + assert_eq!( + engine.encode_response(source, &response, &policy)?.body, + body + ); + + for target in [ + source, + WireFormat::OpenAiChat, + WireFormat::AnthropicMessages, + ] { + let output = engine + .encode_response(target, &response, &normalized_policy())? + .body; + let arguments = match target { + WireFormat::OpenAiResponses => { + serde_json::from_value::(output["output"][0]["arguments"].clone())? + } + WireFormat::OpenAiChat => serde_json::from_value::( + output["choices"][0]["message"]["tool_calls"][0]["function"]["arguments"].clone(), + )?, + WireFormat::AnthropicMessages => output["content"][0]["input"].to_string(), + }; + assert_eq!( + serde_json::from_str::(&arguments)?, + json!({"city": "Paris"}), + "{target:?}" + ); + + let events = block_on( + encode_stream(response.clone().into_stream(), target, None)?.try_collect::>(), + )?; + let arguments: String = events + .iter() + .filter_map(|event| match target { + WireFormat::OpenAiResponses => (event["type"] + == "response.function_call_arguments.delta") + .then(|| event["delta"].as_str()) + .flatten(), + WireFormat::OpenAiChat => { + event["choices"][0]["delta"]["tool_calls"][0]["function"]["arguments"].as_str() + } + WireFormat::AnthropicMessages => event["delta"]["partial_json"].as_str(), + }) + .collect(); + assert_eq!( + serde_json::from_str::(&arguments)?, + json!({"city": "Paris"}), + "{target:?}" + ); + } + Ok(()) +} + +#[test] +fn responses_tool_arguments_preserve_malformed_and_structured_values() -> TestResult { + let engine = TranslationEngine::default(); + for arguments in [json!("not JSON"), json!({"city": "Paris"})] { + let body = json!({"output": [{"type": "function_call", "call_id": "call_1", + "name": "lookup", "arguments": arguments}]}); + let response = engine + .decode_response( + WireFormat::OpenAiResponses, + &body, + &TranslationPolicy::default(), + )? + .response; + let ContentBlock::ToolCall(call) = &response.outputs[0].content[0] else { + return Err("expected a tool call".into()); + }; + assert_eq!(call.arguments, arguments); + } + Ok(()) +} + #[test] fn responses_native_output_is_preserved_or_rejected() -> TestResult { use switchyard_translation::{StreamTranslationState, TranslationError}; From 4fd4bc24bb59c13b5b1a0d692dec14ec3a3abe68 Mon Sep 17 00:00:00 2001 From: Ryan Lempka Date: Fri, 13 Nov 2026 10:41:16 -0600 Subject: [PATCH 2/2] test(translation): trim Responses argument regression Signed-off-by: Ryan Lempka --- .../tests/response_translation.rs | 93 +++++-------------- 1 file changed, 24 insertions(+), 69 deletions(-) diff --git a/crates/switchyard-translation/tests/response_translation.rs b/crates/switchyard-translation/tests/response_translation.rs index e2b12c0636..b37b58f67f 100644 --- a/crates/switchyard-translation/tests/response_translation.rs +++ b/crates/switchyard-translation/tests/response_translation.rs @@ -20,84 +20,39 @@ use common::{ type TestResult = std::result::Result<(), Box>; #[test] -fn responses_tool_arguments_survive_buffered_and_synthetic_encoding() -> TestResult { +fn responses_tool_arguments_are_not_double_encoded() -> TestResult { let engine = TranslationEngine::default(); - let source = WireFormat::OpenAiResponses; + let format = WireFormat::OpenAiResponses; let policy = TranslationPolicy::default(); - let body = json!({"id": "resp_1", "model": "fixture", "status": "completed", - "output": [{"type": "function_call", "call_id": "call_1", "name": "lookup", - "arguments": "{\"city\": \"Paris\"}"}]}); - let response = engine.decode_response(source, &body, &policy)?.response; - assert_eq!( - engine.encode_response(source, &response, &policy)?.body, - body - ); - - for target in [ - source, - WireFormat::OpenAiChat, - WireFormat::AnthropicMessages, + for (arguments, expected) in [ + (json!(r#"{"city": "Paris"}"#), json!({"city": "Paris"})), + (json!("not JSON"), json!("not JSON")), + (json!({"city": "Paris"}), json!({"city": "Paris"})), ] { - let output = engine - .encode_response(target, &response, &normalized_policy())? - .body; - let arguments = match target { - WireFormat::OpenAiResponses => { - serde_json::from_value::(output["output"][0]["arguments"].clone())? - } - WireFormat::OpenAiChat => serde_json::from_value::( - output["choices"][0]["message"]["tool_calls"][0]["function"]["arguments"].clone(), - )?, - WireFormat::AnthropicMessages => output["content"][0]["input"].to_string(), - }; + let body = json!({"output": [{"type": "function_call", "call_id": "call_1", + "name": "lookup", "arguments": arguments}]}); + let response = engine.decode_response(format, &body, &policy)?.response; assert_eq!( - serde_json::from_str::(&arguments)?, - json!({"city": "Paris"}), - "{target:?}" + engine.encode_response(format, &response, &policy)?.body, + body ); - let events = block_on( - encode_stream(response.clone().into_stream(), target, None)?.try_collect::>(), - )?; + let output = engine + .encode_response(format, &response, &normalized_policy())? + .body; + let arguments = output["output"][0]["arguments"] + .as_str() + .ok_or("missing arguments")?; + assert_eq!(serde_json::from_str::(arguments)?, expected); + + let events = + block_on(encode_stream(response.into_stream(), format, None)?.try_collect::>())?; let arguments: String = events .iter() - .filter_map(|event| match target { - WireFormat::OpenAiResponses => (event["type"] - == "response.function_call_arguments.delta") - .then(|| event["delta"].as_str()) - .flatten(), - WireFormat::OpenAiChat => { - event["choices"][0]["delta"]["tool_calls"][0]["function"]["arguments"].as_str() - } - WireFormat::AnthropicMessages => event["delta"]["partial_json"].as_str(), - }) + .filter(|event| event["type"] == "response.function_call_arguments.delta") + .filter_map(|event| event["delta"].as_str()) .collect(); - assert_eq!( - serde_json::from_str::(&arguments)?, - json!({"city": "Paris"}), - "{target:?}" - ); - } - Ok(()) -} - -#[test] -fn responses_tool_arguments_preserve_malformed_and_structured_values() -> TestResult { - let engine = TranslationEngine::default(); - for arguments in [json!("not JSON"), json!({"city": "Paris"})] { - let body = json!({"output": [{"type": "function_call", "call_id": "call_1", - "name": "lookup", "arguments": arguments}]}); - let response = engine - .decode_response( - WireFormat::OpenAiResponses, - &body, - &TranslationPolicy::default(), - )? - .response; - let ContentBlock::ToolCall(call) = &response.outputs[0].content[0] else { - return Err("expected a tool call".into()); - }; - assert_eq!(call.arguments, arguments); + assert_eq!(serde_json::from_str::(&arguments)?, expected); } Ok(()) }