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): preserve buffered tool-use stop reasons (#1317)

David Gardnerfcd2a4feeb6a
diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rs
index 8e0797f36..d920f38d3 100644
--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs
@@ -352,7 +352,7 @@ impl FormatCodec for OpenAiResponsesCodec {
})
.sum::<usize>();
content.extend(output.content);
- if output.stop_reason.is_some() {
+ if output.stop_reason.is_some() && stop_reason != Some(StopReason::ToolUse) {
stop_reason = output.stop_reason;
}
}
diff --git a/crates/switchyard-translation/tests/response_translation.rs b/crates/switchyard-translation/tests/response_translation.rs
index b37b58f67..d11910fd6 100644
--- a/crates/switchyard-translation/tests/response_translation.rs
+++ b/crates/switchyard-translation/tests/response_translation.rs
@@ -1056,7 +1056,7 @@ fn openai_chat_response_with_text_and_tool_call_translates_both_to_responses() -
"usage": {"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7}
});
- let output = engine
+ let mut output = engine
.translate_response(
WireFormat::OpenAiChat,
WireFormat::OpenAiResponses,
@@ -1069,6 +1069,22 @@ fn openai_chat_response_with_text_and_tool_call_translates_both_to_responses() -
assert_eq!(output["output"][0]["content"][0]["text"], "Let me check.");
assert_eq!(output["output"][1]["type"], "function_call");
assert_eq!(output["output"][1]["call_id"], "call_1");
+
+ // Check text before the tool call, then reverse the items to check text after it.
+ for _ in 0..2 {
+ let decoded = engine
+ .decode_response(
+ WireFormat::OpenAiResponses,
+ &output,
+ &TranslationPolicy::default(),
+ )?
+ .response;
+ assert_eq!(decoded.outputs[0].stop_reason, Some(StopReason::ToolUse));
+ output["output"]
+ .as_array_mut()
+ .ok_or("output should be an array")?
+ .reverse();
+ }
Ok(())
}