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..b37b58f67f 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,44 @@ use common::{ type TestResult = std::result::Result<(), Box>; +#[test] +fn responses_tool_arguments_are_not_double_encoded() -> TestResult { + let engine = TranslationEngine::default(); + let format = WireFormat::OpenAiResponses; + let policy = TranslationPolicy::default(); + for (arguments, expected) in [ + (json!(r#"{"city": "Paris"}"#), json!({"city": "Paris"})), + (json!("not JSON"), json!("not JSON")), + (json!({"city": "Paris"}), json!({"city": "Paris"})), + ] { + 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!( + engine.encode_response(format, &response, &policy)?.body, + body + ); + + 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(|event| event["type"] == "response.function_call_arguments.delta") + .filter_map(|event| event["delta"].as_str()) + .collect(); + assert_eq!(serde_json::from_str::(&arguments)?, expected); + } + Ok(()) +} + #[test] fn responses_native_output_is_preserved_or_rejected() -> TestResult { use switchyard_translation::{StreamTranslationState, TranslationError};