Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})),
Expand Down
43 changes: 41 additions & 2 deletions crates/switchyard-translation/tests/response_translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -18,6 +19,44 @@ use common::{

type TestResult = std::result::Result<(), Box<dyn std::error::Error + Send + Sync>>;

#[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::<Value>(arguments)?, expected);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

let events =
block_on(encode_stream(response.into_stream(), format, None)?.try_collect::<Vec<_>>())?;
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::<Value>(&arguments)?, expected);
}
Ok(())
}

#[test]
fn responses_native_output_is_preserved_or_rejected() -> TestResult {
use switchyard_translation::{StreamTranslationState, TranslationError};
Expand Down
Loading