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 chat reasoning details (#415)

Alex Steinerf9b2df61befe
diff --git a/crates/libsy-llm-client/tests/observability.rs b/crates/libsy-llm-client/tests/observability.rs
index dd5afa35e..ec0882a88 100644
--- a/crates/libsy-llm-client/tests/observability.rs
+++ b/crates/libsy-llm-client/tests/observability.rs
@@ -568,6 +568,7 @@ async fn affinity_warns_once_when_request_has_no_usable_identity() -> switchyard
content: vec![ContentBlock::Reasoning {
text: "provider reasoning".to_string(),
signature: None,
+ details: Vec::new(),
}],
}],
..LlmRequest::default()
diff --git a/crates/libsy/src/algorithms/util/affinity.rs b/crates/libsy/src/algorithms/util/affinity.rs
index b95f91f15..5f5db53e7 100644
--- a/crates/libsy/src/algorithms/util/affinity.rs
+++ b/crates/libsy/src/algorithms/util/affinity.rs
@@ -507,6 +507,7 @@ mod tests {
ContentBlock::Reasoning {
text: "Internal provider reasoning.".to_string(),
signature: Some("provider-signature".to_string()),
+ details: Vec::new(),
},
],
});
diff --git a/crates/libsy/src/algorithms/util/llm_judge.rs b/crates/libsy/src/algorithms/util/llm_judge.rs
index 4d5cfb564..6657207e6 100644
--- a/crates/libsy/src/algorithms/util/llm_judge.rs
+++ b/crates/libsy/src/algorithms/util/llm_judge.rs
@@ -419,6 +419,7 @@ mod tests {
ContentBlock::Reasoning {
text: r#"{"ok":false}"#.to_string(),
signature: None,
+ details: Vec::new(),
},
);
}
diff --git a/crates/protocol/src/llm.rs b/crates/protocol/src/llm.rs
index c2386c23f..78a101f8e 100644
--- a/crates/protocol/src/llm.rs
+++ b/crates/protocol/src/llm.rs
@@ -87,6 +87,10 @@ pub enum ContentBlock {
text: String,
/// Provider signature used to validate or continue the reasoning block.
signature: Option<String>,
+ /// Structured reasoning details, such as an encrypted `{ "type":
+ /// "reasoning.encrypted", "data": "..." }` object, replayed without modification.
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ details: Vec<Value>,
},
/// Image content.
Image {
diff --git a/crates/protocol/src/stream.rs b/crates/protocol/src/stream.rs
index e51b68dce..550010058 100644
--- a/crates/protocol/src/stream.rs
+++ b/crates/protocol/src/stream.rs
@@ -193,11 +193,19 @@ impl AggLlmResponse {
text,
});
}
- ContentBlock::Reasoning { text, .. } => {
- chunks.push(LlmResponseChunk::ReasoningDelta {
- index: output_index,
- text,
- });
+ ContentBlock::Reasoning { text, details, .. } => {
+ if !details.is_empty() {
+ chunks.push(LlmResponseChunk::ReasoningDetailsDelta {
+ index: output_index,
+ details,
+ text,
+ });
+ } else {
+ chunks.push(LlmResponseChunk::ReasoningDelta {
+ index: output_index,
+ text,
+ });
+ }
}
ContentBlock::ToolCall(tool) => {
let args = serde_json::to_string(&tool.arguments).unwrap_or_default();
@@ -272,6 +280,15 @@ pub enum LlmResponseChunk {
/// Reasoning fragment.
text: String,
},
+ /// Adds structured reasoning details to one output index.
+ ReasoningDetailsDelta {
+ /// Provider output index.
+ index: usize,
+ /// Reasoning detail objects in provider order.
+ details: Vec<Value>,
+ /// Normalized reasoning text represented by or accompanying the details.
+ text: String,
+ },
/// Adds or updates a tool call at one index.
ToolCallDelta {
/// Tool-call index within the response.
@@ -322,6 +339,7 @@ pub struct ResponseAccumulator {
model: Option<String>,
text: String,
reasoning: Option<String>,
+ reasoning_details: Vec<Value>,
tool_calls: BTreeMap<usize, PartialToolCall>,
usage: Usage,
stop_reason: Option<StopReason>,
@@ -359,6 +377,14 @@ impl ResponseAccumulator {
.get_or_insert_with(String::new)
.push_str(&text);
}
+ LlmResponseChunk::ReasoningDetailsDelta { details, text, .. } => {
+ self.reasoning_details.extend(details);
+ if !text.is_empty() {
+ self.reasoning
+ .get_or_insert_with(String::new)
+ .push_str(&text);
+ }
+ }
LlmResponseChunk::ToolCallDelta {
index,
id,
@@ -388,10 +414,11 @@ impl ResponseAccumulator {
/// tool calls (by ascending delta index) — a single assistant output.
pub fn finish(self) -> AggLlmResponse {
let mut content = Vec::new();
- if let Some(reasoning) = self.reasoning {
+ if self.reasoning.is_some() || !self.reasoning_details.is_empty() {
content.push(ContentBlock::Reasoning {
- text: reasoning,
+ text: self.reasoning.unwrap_or_default(),
signature: None,
+ details: self.reasoning_details,
});
}
if !self.text.is_empty() {
@@ -611,6 +638,7 @@ mod tests {
ContentBlock::Reasoning {
text: "think".to_string(),
signature: None,
+ details: Vec::new(),
},
ContentBlock::Text {
text: "answer".to_string(),
@@ -649,6 +677,39 @@ mod tests {
assert_eq!(recovered.outputs[0].content, original.outputs[0].content);
}
+ #[test]
+ fn into_stream_retains_encrypted_reasoning_and_text() {
+ let details = vec![json!({
+ "type": "reasoning.encrypted",
+ "data": "opaque-encrypted-reasoning"
+ })];
+ let original = AggLlmResponse {
+ outputs: vec![ResponseOutput {
+ role: Role::Assistant,
+ content: vec![ContentBlock::Reasoning {
+ text: "fallback reasoning".to_string(),
+ signature: None,
+ details: details.clone(),
+ }],
+ stop_reason: Some(StopReason::EndTurn),
+ }],
+ ..AggLlmResponse::default()
+ };
+
+ let recovered = block_on(LlmResponse::Stream(original.into_stream()).into_agg())
+ .expect("into_agg failed");
+ let ContentBlock::Reasoning {
+ text,
+ details: recovered_details,
+ ..
+ } = &recovered.outputs[0].content[0]
+ else {
+ panic!("expected reasoning block");
+ };
+ assert_eq!(text, "fallback reasoning");
+ assert_eq!(recovered_details, &details);
+ }
+
#[test]
fn into_agg_preserves_stream_item_error() {
let response = LlmResponse::Stream(Box::pin(stream::once(async {
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
index fe5573039..75f4d6848 100644
--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
@@ -525,6 +525,7 @@ fn decode_anthropic_content_block(
.and_then(Value::as_str)
.filter(|signature| !signature.is_empty())
.map(ToOwned::to_owned),
+ details: Vec::new(),
}],
Some("tool_use") => vec![ContentBlock::ToolCall(ToolCall {
id: block
@@ -794,6 +795,7 @@ fn encode_one_anthropic_response_block(block: &ContentBlock) -> Vec<Value> {
ContentBlock::Reasoning {
text,
signature: None,
+ ..
} => vec![json!({
"type": "thinking",
"thinking": text,
@@ -812,6 +814,7 @@ fn encode_one_anthropic_block(block: &ContentBlock) -> Vec<Value> {
ContentBlock::Reasoning {
text,
signature: Some(signature),
+ ..
} if !signature.is_empty() => vec![json!({
"type": "thinking",
"thinking": text,
diff --git a/crates/switchyard-translation/src/codecs/anthropic/stream.rs b/crates/switchyard-translation/src/codecs/anthropic/stream.rs
index 450d796e1..e75af65f4 100644
--- a/crates/switchyard-translation/src/codecs/anthropic/stream.rs
+++ b/crates/switchyard-translation/src/codecs/anthropic/stream.rs
@@ -192,6 +192,18 @@ fn encode_anthropic_stream(
}));
out
}
+ LlmResponseChunk::ReasoningDetailsDelta { text, .. } => {
+ if text.is_empty() {
+ return Vec::new();
+ }
+ let mut out = ensure_anthropic_reasoning_block(state);
+ out.push(json!({
+ "type": "content_block_delta",
+ "index": state.reasoning_block_index.unwrap_or(0),
+ "delta": {"type": "thinking_delta", "thinking": text},
+ }));
+ out
+ }
LlmResponseChunk::ToolCallDelta {
index,
id,
diff --git a/crates/switchyard-translation/src/codecs/common.rs b/crates/switchyard-translation/src/codecs/common.rs
index ffd8e34c6..78e5a7f89 100644
--- a/crates/switchyard-translation/src/codecs/common.rs
+++ b/crates/switchyard-translation/src/codecs/common.rs
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
-//! Provider-agnostic helpers shared by buffered wire-format codecs.
+//! Provider-agnostic helpers shared by wire-format codecs.
use serde_json::{Map, Value};
@@ -41,6 +41,40 @@ pub(crate) fn reasoning_text_from_blocks(content: &[ContentBlock], separator: &s
.join(separator)
}
+/// Extracts displayable text from structured reasoning details.
+pub(crate) fn reasoning_text_from_details(details: &[Value]) -> Option<String> {
+ let parts = details
+ .iter()
+ .filter_map(Value::as_object)
+ .filter_map(|detail| {
+ detail
+ .get("text")
+ .and_then(Value::as_str)
+ .filter(|text| !text.is_empty())
+ .or_else(|| {
+ detail
+ .get("summary")
+ .and_then(Value::as_str)
+ .filter(|summary| !summary.is_empty())
+ })
+ })
+ .collect::<Vec<_>>();
+ (!parts.is_empty()).then(|| parts.join("\n"))
+}
+
+/// Returns the first non-empty string stored under the requested keys.
+pub(crate) fn first_nonempty_string<'a>(
+ object: &'a Map<String, Value>,
+ keys: &[&str],
+) -> Option<&'a str> {
+ keys.iter().find_map(|key| {
+ object
+ .get(*key)
+ .and_then(Value::as_str)
+ .filter(|value| !value.is_empty())
+ })
+}
+
/// Copies unknown provider fields into the IR extension map.
pub(crate) fn provider_extensions(
object: &Map<String, Value>,
diff --git a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
index 32d0de1a2..d829ca316 100644
--- a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
@@ -6,7 +6,8 @@
use serde_json::{Map, Value, json};
use crate::codecs::common::{
- is_known_role_name, provider_extensions, reasoning_text_from_blocks, text_from_blocks,
+ first_nonempty_string, is_known_role_name, provider_extensions, reasoning_text_from_blocks,
+ reasoning_text_from_details, text_from_blocks,
};
use crate::codecs::{
DecodedRequest, DecodedResponse, EncodedRequest, EncodedResponse, FormatCodec,
@@ -361,6 +362,12 @@ impl FormatCodec for OpenAiChatCodec {
{
message["reasoning_content"] = Value::String(reasoning);
}
+ if let Some(details) = output
+ .map(|output| reasoning_details_from_blocks(&output.content))
+ .filter(|details| !details.is_empty())
+ {
+ message["reasoning_details"] = Value::Array(details);
+ }
if !tool_calls.is_empty() {
message["tool_calls"] = Value::Array(tool_calls);
}
@@ -389,22 +396,58 @@ impl FormatCodec for OpenAiChatCodec {
// Pulls OpenAI-compatible reasoning fields into private reasoning IR blocks.
fn prepend_openai_reasoning_blocks(content: &mut Vec<ContentBlock>, object: &Map<String, Value>) {
- let reasoning = ["reasoning_content", "reasoning"]
- .into_iter()
- .filter_map(|key| object.get(key).and_then(Value::as_str))
- .filter(|text| !text.is_empty())
- .map(|text| ContentBlock::Reasoning {
- text: text.to_string(),
- signature: None,
- })
- .collect::<Vec<_>>();
- if reasoning.is_empty() {
+ if let Some(details) = object
+ .get("reasoning_details")
+ .and_then(Value::as_array)
+ .filter(|details| !details.is_empty())
+ {
+ let text = reasoning_text_from_details(details)
+ .or_else(|| {
+ first_nonempty_string(object, &["reasoning_content", "reasoning"])
+ .map(ToOwned::to_owned)
+ })
+ .unwrap_or_default();
+ let signature = details.iter().find_map(|detail| {
+ detail
+ .get("signature")
+ .and_then(Value::as_str)
+ .filter(|signature| !signature.is_empty())
+ .map(ToOwned::to_owned)
+ });
+ content.insert(
+ 0,
+ ContentBlock::Reasoning {
+ text,
+ signature,
+ details: details.clone(),
+ },
+ );
return;
}
- let mut merged = reasoning;
- merged.append(content);
- *content = merged;
+ if let Some(text) = first_nonempty_string(object, &["reasoning_content", "reasoning"]) {
+ content.insert(
+ 0,
+ ContentBlock::Reasoning {
+ text: text.to_string(),
+ signature: None,
+ details: Vec::new(),
+ },
+ );
+ }
+}
+
+// Returns structured reasoning details in their original order.
+fn reasoning_details_from_blocks(content: &[ContentBlock]) -> Vec<Value> {
+ content
+ .iter()
+ .filter_map(|block| match block {
+ ContentBlock::Reasoning { details, .. } => Some(details.as_slice()),
+ _ => None,
+ })
+ .flatten()
+ .cloned()
+ .collect()
}
/// Decodes OpenAI role strings into normalized roles.
@@ -804,6 +847,7 @@ fn encode_message_without_tool_results_to_openai(
"role": role,
"content": encode_openai_content(&content_blocks, message.role, diagnostics, policy)?,
});
+ encode_openai_message_reasoning(&mut message_json, &message.content);
if !tool_calls.is_empty() {
message_json["tool_calls"] = Value::Array(tool_calls);
if message_json["content"] == Value::String(String::new()) {
@@ -813,6 +857,61 @@ fn encode_message_without_tool_results_to_openai(
Ok(message_json)
}
+// Adds either plaintext reasoning or structured details to an OpenAI Chat message.
+fn encode_openai_message_reasoning(message: &mut Value, content: &[ContentBlock]) {
+ let details = reasoning_details_from_blocks(content);
+ if details.is_empty() {
+ encode_openai_message_plaintext_reasoning(message, content);
+ } else {
+ encode_openai_message_structured_reasoning(message, content, details);
+ }
+}
+
+// Adds reasoning blocks that have no structured provider representation.
+fn encode_openai_message_plaintext_reasoning(message: &mut Value, content: &[ContentBlock]) {
+ let reasoning = content
+ .iter()
+ .filter_map(|block| match block {
+ ContentBlock::Reasoning {
+ text,
+ signature: None,
+ ..
+ } => Some(text.as_str()),
+ _ => None,
+ })
+ .collect::<Vec<_>>()
+ .join("\n");
+ if !reasoning.is_empty() {
+ message["reasoning"] = Value::String(reasoning);
+ }
+}
+
+// Adds exact provider details and text that cannot be recovered from those details.
+fn encode_openai_message_structured_reasoning(
+ message: &mut Value,
+ content: &[ContentBlock],
+ details: Vec<Value>,
+) {
+ message["reasoning_details"] = Value::Array(details);
+ let fallback = content
+ .iter()
+ .filter_map(|block| match block {
+ ContentBlock::Reasoning { text, details, .. }
+ if !details.is_empty()
+ && reasoning_text_from_details(details).as_deref() != Some(text.as_str())
+ && !text.is_empty() =>
+ {
+ Some(text.as_str())
+ }
+ _ => None,
+ })
+ .collect::<Vec<_>>()
+ .join("\n");
+ if !fallback.is_empty() {
+ message["reasoning"] = Value::String(fallback);
+ }
+}
+
// Checks whether any block in a message is a tool result.
fn message_has_tool_results(message: &Message) -> bool {
message
diff --git a/crates/switchyard-translation/src/codecs/openai_chat/stream.rs b/crates/switchyard-translation/src/codecs/openai_chat/stream.rs
index b4c6273b1..24857effe 100644
--- a/crates/switchyard-translation/src/codecs/openai_chat/stream.rs
+++ b/crates/switchyard-translation/src/codecs/openai_chat/stream.rs
@@ -6,6 +6,7 @@
use serde_json::{Map, Value, json};
use crate::LlmResponseChunk;
+use crate::codecs::common::{first_nonempty_string, reasoning_text_from_details};
use crate::codecs::stream::{
StreamCodec, StreamTranslationState, record_source_identity, state_source_is, string_field,
target_model_or_source_model,
@@ -105,14 +106,32 @@ fn decode_openai_chat_stream(
text: text.to_string(),
});
}
- for reasoning_key in ["reasoning_content", "reasoning"] {
- if let Some(text) = delta.get(reasoning_key).and_then(Value::as_str)
- && !text.is_empty()
- {
- out.push(LlmResponseChunk::ReasoningDelta {
- index: 0,
- text: text.to_string(),
- });
+ if let Some(details) = delta
+ .get("reasoning_details")
+ .and_then(Value::as_array)
+ .filter(|details| !details.is_empty())
+ {
+ let text = reasoning_text_from_details(details)
+ .or_else(|| {
+ first_nonempty_string(delta, &["reasoning_content", "reasoning"])
+ .map(ToOwned::to_owned)
+ })
+ .unwrap_or_default();
+ out.push(LlmResponseChunk::ReasoningDetailsDelta {
+ index: 0,
+ details: details.clone(),
+ text,
+ });
+ } else {
+ for reasoning_key in ["reasoning_content", "reasoning"] {
+ if let Some(text) = delta.get(reasoning_key).and_then(Value::as_str)
+ && !text.is_empty()
+ {
+ out.push(LlmResponseChunk::ReasoningDelta {
+ index: 0,
+ text: text.to_string(),
+ });
+ }
}
}
if let Some(tool_calls) = delta.get("tool_calls").and_then(Value::as_array) {
@@ -194,6 +213,14 @@ fn encode_openai_chat_stream(
None,
)]
}
+ LlmResponseChunk::ReasoningDetailsDelta { details, text, .. } => {
+ let details_text = reasoning_text_from_details(&details);
+ let mut delta = json!({"reasoning_details": details});
+ if !text.is_empty() && details_text.as_deref() != Some(text.as_str()) {
+ delta["reasoning"] = Value::String(text);
+ }
+ vec![openai_stream_chunk(state, delta, None, None)]
+ }
LlmResponseChunk::ToolCallDelta {
index,
id,
diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rs
index 025dbe35f..a31fda0a8 100644
--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs
@@ -585,6 +585,7 @@ fn decode_responses_reasoning_item(item: &Map<String, Value>) -> Vec<ContentBloc
vec![ContentBlock::Reasoning {
text: parts.join("\n"),
signature: None,
+ details: Vec::new(),
}]
}
@@ -649,6 +650,7 @@ fn decode_responses_content(value: &Value) -> Vec<ContentBlock> {
.unwrap_or_default()
.to_string(),
signature: None,
+ details: Vec::new(),
});
}
Some("input_image") => {
@@ -966,6 +968,7 @@ fn encode_responses_special_input(block: &ContentBlock) -> Option<Value> {
ContentBlock::Reasoning {
text,
signature: None,
+ ..
} => Some(json!({
"type": "reasoning",
"content": [{"type": "reasoning_text", "text": text}],
diff --git a/crates/switchyard-translation/src/codecs/responses/stream.rs b/crates/switchyard-translation/src/codecs/responses/stream.rs
index 8a7d324ec..b076e3300 100644
--- a/crates/switchyard-translation/src/codecs/responses/stream.rs
+++ b/crates/switchyard-translation/src/codecs/responses/stream.rs
@@ -170,6 +170,10 @@ fn encode_responses_stream(
LlmResponseChunk::ReasoningDelta { text, .. } => {
encode_responses_reasoning_delta(state, text)
}
+ LlmResponseChunk::ReasoningDetailsDelta { text, .. } if !text.is_empty() => {
+ encode_responses_reasoning_delta(state, text)
+ }
+ LlmResponseChunk::ReasoningDetailsDelta { .. } => Vec::new(),
LlmResponseChunk::ToolCallDelta {
index,
id,
diff --git a/crates/switchyard-translation/tests/common/mod.rs b/crates/switchyard-translation/tests/common/mod.rs
new file mode 100644
index 000000000..241218cf2
--- /dev/null
+++ b/crates/switchyard-translation/tests/common/mod.rs
@@ -0,0 +1,42 @@
+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+use serde_json::{Value, json};
+use switchyard_translation::{PreservationPolicy, TranslationPolicy};
+
+pub const REASONING_MODEL: &str = "z-ai/glm-5.2";
+
+pub fn normalized_policy() -> TranslationPolicy {
+ TranslationPolicy {
+ preservation: PreservationPolicy::Disabled,
+ ..TranslationPolicy::default()
+ }
+}
+
+pub fn text_and_encrypted_reasoning_details() -> Value {
+ json!([
+ {
+ "type": "reasoning.text",
+ "text": "Inspect the tool result.",
+ "signature": "opaque-signature",
+ "id": "reasoning-1",
+ "format": "anthropic-claude-v1",
+ "index": 0
+ },
+ {
+ "type": "reasoning.encrypted",
+ "data": "opaque-encrypted-reasoning",
+ "id": "reasoning-1",
+ "format": "anthropic-claude-v1",
+ "index": 1
+ }
+ ])
+}
+
+pub fn shell_tool_call() -> Value {
+ json!({
+ "id": "call-1",
+ "type": "function",
+ "function": {"name": "shell", "arguments": "{\"command\":\"pwd\"}"}
+ })
+}
diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rs
index e14d38e9b..b7c4121a9 100644
--- a/crates/switchyard-translation/tests/request_translation.rs
+++ b/crates/switchyard-translation/tests/request_translation.rs
@@ -3,12 +3,16 @@
//! Tests for buffered request translation between provider formats.
+pub mod common;
+
use pretty_assertions::assert_eq;
use serde_json::{Value, json};
use switchyard_translation::{
LossyConversionPolicy, TranslationEngine, TranslationPolicy, WireFormat,
};
+use common::{REASONING_MODEL, normalized_policy, shell_tool_call};
+
type TestResult = std::result::Result<(), Box<dyn std::error::Error + Send + Sync>>;
// Verifies Anthropic-only request fields are dropped or mapped for OpenAI Chat.
@@ -1000,6 +1004,7 @@ fn responses_reasoning_items_attach_to_tool_call_turn_for_openai_chat() -> TestR
);
assert_eq!(messages.len(), 4);
assert_eq!(messages[1], json!({"role": "assistant", "content": "\n\n"}));
+ assert_eq!(messages[2]["reasoning"], "Check the python setup.");
assert_eq!(messages[2]["tool_calls"][0]["id"], "call-1");
assert_eq!(messages[3]["role"], "tool");
Ok(())
@@ -1035,12 +1040,102 @@ fn responses_reasoning_item_merges_into_next_assistant_message_for_openai_chat()
output["messages"],
json!([
{"role": "user", "content": "Check the file"},
- {"role": "assistant", "content": "Let me check."}
+ {
+ "role": "assistant",
+ "content": "Let me check.",
+ "reasoning": "Reading."
+ }
])
);
Ok(())
}
+#[test]
+fn openai_chat_reasoning_details_round_trip_in_assistant_history() -> TestResult {
+ let engine = TranslationEngine::default();
+ // Exercise the normalized IR path instead of replaying the original JSON.
+ let policy = normalized_policy();
+ let details = json!([
+ {
+ "type": "reasoning.summary",
+ "summary": "Inspect the environment.",
+ "id": "reasoning-1",
+ "format": "openai-responses-v1",
+ "index": 0
+ },
+ {
+ "type": "reasoning.encrypted",
+ "data": "opaque-encrypted-reasoning",
+ "id": "reasoning-1",
+ "format": "openai-responses-v1",
+ "index": 1
+ }
+ ]);
+ let body = json!({
+ "model": REASONING_MODEL,
+ "messages": [
+ {"role": "user", "content": "Inspect the environment"},
+ {
+ "role": "assistant",
+ "content": null,
+ "reasoning": "fallback text",
+ "reasoning_details": details,
+ "tool_calls": [shell_tool_call()]
+ },
+ {"role": "tool", "tool_call_id": "call-1", "content": "/workspace"}
+ ]
+ });
+
+ let output = engine
+ .translate_request(
+ WireFormat::OpenAiChat,
+ WireFormat::OpenAiChat,
+ &body,
+ &policy,
+ )?
+ .body;
+
+ assert_eq!(output["messages"][1]["reasoning_details"], details);
+ assert!(output["messages"][1].get("reasoning").is_none());
+ assert_eq!(output["messages"][1]["tool_calls"][0]["id"], "call-1");
+ Ok(())
+}
+
+#[test]
+fn openai_chat_encrypted_reasoning_details_retain_fallback() -> TestResult {
+ let engine = TranslationEngine::default();
+ let policy = normalized_policy();
+ let details = json!([{
+ "type": "reasoning.encrypted",
+ "data": "opaque-encrypted-reasoning",
+ "id": "reasoning-1",
+ "format": "openai-responses-v1",
+ "index": 0
+ }]);
+ let body = json!({
+ "model": REASONING_MODEL,
+ "messages": [{
+ "role": "assistant",
+ "content": null,
+ "reasoning": "fallback text",
+ "reasoning_details": details
+ }]
+ });
+
+ let output = engine
+ .translate_request(
+ WireFormat::OpenAiChat,
+ WireFormat::OpenAiChat,
+ &body,
+ &policy,
+ )?
+ .body;
+
+ assert_eq!(output["messages"][0]["reasoning_details"], details);
+ assert_eq!(output["messages"][0]["reasoning"], "fallback text");
+ Ok(())
+}
+
// Verifies merged reasoning re-emerges as a Responses reasoning item ahead of
// the turn's function call when encoding back to the Responses format.
#[test]
diff --git a/crates/switchyard-translation/tests/response_translation.rs b/crates/switchyard-translation/tests/response_translation.rs
index cc863ece3..ac8cd01ec 100644
--- a/crates/switchyard-translation/tests/response_translation.rs
+++ b/crates/switchyard-translation/tests/response_translation.rs
@@ -3,10 +3,16 @@
//! Tests for buffered response translation between provider formats.
+pub mod common;
+
use pretty_assertions::assert_eq;
use serde_json::json;
use switchyard_translation::{TranslationEngine, TranslationPolicy, WireFormat};
+use common::{
+ REASONING_MODEL, normalized_policy, shell_tool_call, text_and_encrypted_reasoning_details,
+};
+
type TestResult = std::result::Result<(), Box<dyn std::error::Error + Send + Sync>>;
// Verifies OpenAI Chat responses map to Anthropic message responses.
@@ -331,6 +337,48 @@ fn openai_reasoning_response_translates_to_responses_reasoning_item() -> TestRes
Ok(())
}
+#[test]
+fn openai_chat_response_round_trips_reasoning_details() -> TestResult {
+ let engine = TranslationEngine::default();
+ // Exercise the normalized IR path instead of replaying the original JSON.
+ let policy = normalized_policy();
+ let details = text_and_encrypted_reasoning_details();
+ let body = json!({
+ "id": "chatcmpl-test",
+ "model": REASONING_MODEL,
+ "choices": [{
+ "index": 0,
+ "message": {
+ "role": "assistant",
+ "content": null,
+ "reasoning": "fallback text",
+ "reasoning_details": details,
+ "tool_calls": [shell_tool_call()]
+ },
+ "finish_reason": "tool_calls"
+ }]
+ });
+
+ let output = engine
+ .translate_response(
+ WireFormat::OpenAiChat,
+ WireFormat::OpenAiChat,
+ &body,
+ &policy,
+ )?
+ .body;
+
+ assert_eq!(
+ output["choices"][0]["message"]["reasoning_details"],
+ details
+ );
+ assert_eq!(
+ output["choices"][0]["message"]["reasoning_content"],
+ "Inspect the tool result."
+ );
+ Ok(())
+}
+
// Verifies reasoning-only responses do not synthesize visible output text.
#[test]
fn openai_reasoning_only_response_translates_to_responses_reasoning_only() -> TestResult {
diff --git a/crates/switchyard-translation/tests/stream_translation.rs b/crates/switchyard-translation/tests/stream_translation.rs
index 3aeed7159..98cf5f1ce 100644
--- a/crates/switchyard-translation/tests/stream_translation.rs
+++ b/crates/switchyard-translation/tests/stream_translation.rs
@@ -3,6 +3,8 @@
//! Tests for translating streaming provider events through the stream IR.
+pub mod common;
+
use pretty_assertions::assert_eq;
use serde_json::json;
use switchyard_protocol::{ResponseAccumulator, StopReason};
@@ -10,6 +12,8 @@ use switchyard_translation::{
LlmResponseChunk, StreamTranslationState, TranslationEngine, WireFormat, decode_stream_event,
};
+use common::{REASONING_MODEL, text_and_encrypted_reasoning_details};
+
type TestResult = std::result::Result<(), Box<dyn std::error::Error + Send + Sync>>;
// Same-format replay returns the same parsed JSON value, including provider-specific fields.
@@ -769,6 +773,120 @@ fn openai_chat_reasoning_stream_fields_do_not_become_anthropic_text() -> TestRes
Ok(())
}
+#[test]
+fn openai_chat_stream_round_trips_reasoning_details() -> TestResult {
+ let engine = TranslationEngine::default();
+ let mut state = StreamTranslationState::new(WireFormat::OpenAiChat, WireFormat::OpenAiChat);
+ let details = text_and_encrypted_reasoning_details();
+ let chunk = json!({
+ "id": "chatcmpl-test",
+ "object": "chat.completion.chunk",
+ "model": REASONING_MODEL,
+ "choices": [{
+ "index": 0,
+ "delta": {
+ "reasoning": "fallback text",
+ "reasoning_details": details
+ },
+ "finish_reason": null
+ }]
+ });
+
+ let events = engine.translate_event(
+ &mut state,
+ WireFormat::OpenAiChat,
+ WireFormat::OpenAiChat,
+ &chunk,
+ )?;
+
+ assert_eq!(events.len(), 1);
+ assert_eq!(
+ events[0]["choices"][0]["delta"]["reasoning_details"],
+ details
+ );
+ assert!(events[0]["choices"][0]["delta"].get("reasoning").is_none());
+ Ok(())
+}
+
+#[test]
+fn openai_chat_stream_retains_encrypted_details_and_fallback() -> TestResult {
+ let engine = TranslationEngine::default();
+ let mut state = StreamTranslationState::new(WireFormat::OpenAiChat, WireFormat::OpenAiChat);
+ let details = json!([{
+ "type": "reasoning.encrypted",
+ "data": "opaque-encrypted-reasoning"
+ }]);
+ let chunk = json!({
+ "id": "chatcmpl-test",
+ "object": "chat.completion.chunk",
+ "model": REASONING_MODEL,
+ "choices": [{
+ "index": 0,
+ "delta": {
+ "reasoning_content": "",
+ "reasoning": "fallback text",
+ "reasoning_details": details
+ },
+ "finish_reason": null
+ }]
+ });
+
+ let events = engine.translate_event(
+ &mut state,
+ WireFormat::OpenAiChat,
+ WireFormat::OpenAiChat,
+ &chunk,
+ )?;
+
+ assert_eq!(events.len(), 1);
+ assert_eq!(
+ events[0]["choices"][0]["delta"]["reasoning_details"],
+ details
+ );
+ assert_eq!(
+ events[0]["choices"][0]["delta"]["reasoning"],
+ "fallback text"
+ );
+ Ok(())
+}
+
+#[test]
+fn openai_chat_stream_uses_summary_when_detail_text_is_empty() -> TestResult {
+ let engine = TranslationEngine::default();
+ let mut state =
+ StreamTranslationState::new(WireFormat::OpenAiChat, WireFormat::AnthropicMessages);
+ let chunk = json!({
+ "id": "chatcmpl-test",
+ "object": "chat.completion.chunk",
+ "model": REASONING_MODEL,
+ "choices": [{
+ "index": 0,
+ "delta": {
+ "reasoning_details": [{
+ "type": "reasoning.summary",
+ "text": "",
+ "summary": "usable summary"
+ }]
+ },
+ "finish_reason": null
+ }]
+ });
+
+ let events = engine.translate_event(
+ &mut state,
+ WireFormat::OpenAiChat,
+ WireFormat::AnthropicMessages,
+ &chunk,
+ )?;
+
+ assert!(events.iter().any(|event| {
+ event["type"] == "content_block_delta"
+ && event["delta"]["type"] == "thinking_delta"
+ && event["delta"]["thinking"] == "usable summary"
+ }));
+ Ok(())
+}
+
// Verifies Anthropic thinking deltas become OpenAI reasoning_content, not content.
#[test]
fn anthropic_thinking_stream_deltas_do_not_become_openai_chat_content() -> TestResult {