Skip to content
Closed
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 @@ -887,9 +887,7 @@ fn encode_openai_message_plaintext_reasoning(message: &mut Value, content: &[Con
})
.collect::<Vec<_>>()
.join("\n");
if !reasoning.is_empty() {
message["reasoning"] = Value::String(reasoning);
}
set_openai_reasoning_text(message, reasoning);
}

// Adds exact provider details and text that cannot be recovered from those details.
Expand All @@ -913,9 +911,16 @@ fn encode_openai_message_structured_reasoning(
})
.collect::<Vec<_>>()
.join("\n");
if !fallback.is_empty() {
message["reasoning"] = Value::String(fallback);
set_openai_reasoning_text(message, fallback);
}

// Reasoning-required upstreams reject a replayed turn without `reasoning_content`, so both spellings are written.
fn set_openai_reasoning_text(message: &mut Value, reasoning: String) {
if reasoning.is_empty() {
return;
}
message["reasoning"] = Value::String(reasoning.clone());
message["reasoning_content"] = Value::String(reasoning);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

// Checks whether any block in a message is a tool result.
Expand Down
58 changes: 56 additions & 2 deletions crates/switchyard-translation/tests/request_translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub mod common;
use pretty_assertions::assert_eq;
use serde_json::{Value, json};
use switchyard_translation::{
FormatId, LossyConversionPolicy, TranslationEngine, TranslationPolicy, WireFormat,
prepare_request_for_target,
FormatId, LossyConversionPolicy, PreservationPolicy, TranslationEngine, TranslationPolicy,
WireFormat, prepare_request_for_target,
};

use common::{REASONING_MODEL, normalized_policy, shell_tool_call};
Expand Down Expand Up @@ -1212,6 +1212,58 @@ fn responses_reasoning_items_attach_to_tool_call_turn_for_openai_chat() -> TestR
Ok(())
}

// Both spellings carry replayed reasoning, and a turn without reasoning gains neither.
#[test]
fn openai_chat_replays_reasoning_under_both_spellings() -> TestResult {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets combine 2 tests into one, and make the comments one line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — the two cases are one test now (openai_chat_replays_reasoning_under_both_spellings),
with the no-reasoning turn appended to the same conversation so both assertions run off one
translation, and the comment cut to one line.

let engine = TranslationEngine::default();
let body = json!({
"model": "deepseek-reasoner",
"messages": [
{"role": "user", "content": "What is 2+2? Use the calculator."},
{
"role": "assistant",
"content": "I'll call the tool.",
"reasoning_content": "The user wants 2+2. Call the calculator.",
"tool_calls": [{
"id": "call_abc123",
"type": "function",
"function": {"name": "calculator", "arguments": "{\"a\": 2, \"b\": 2}"}
}]
},
{"role": "tool", "tool_call_id": "call_abc123", "content": "4"},
{"role": "assistant", "content": "It is 4."}
]
});

let output = engine
.translate_request(
WireFormat::OpenAiChat,
WireFormat::OpenAiChat,
&body,
&TranslationPolicy {
preservation: PreservationPolicy::Disabled,
..TranslationPolicy::default()
},
)?
.body;

let reasoned = &output["messages"][1];
assert_eq!(
reasoned["reasoning_content"],
"The user wants 2+2. Call the calculator."
);
assert_eq!(reasoned["reasoning"], reasoned["reasoning_content"]);
assert_eq!(reasoned["content"], "I'll call the tool.");
assert_eq!(reasoned["tool_calls"][0]["id"], "call_abc123");
assert_eq!(output["messages"][2]["role"], "tool");
assert_eq!(output["messages"][2]["tool_call_id"], "call_abc123");

let without_reasoning = &output["messages"][3];
assert!(without_reasoning.get("reasoning").is_none());
assert!(without_reasoning.get("reasoning_content").is_none());
Ok(())
}

// Verifies a reasoning item merges into the assistant message that follows it.
#[test]
fn responses_reasoning_item_merges_into_next_assistant_message_for_openai_chat() -> TestResult {
Expand Down Expand Up @@ -1245,6 +1297,7 @@ fn responses_reasoning_item_merges_into_next_assistant_message_for_openai_chat()
{
"role": "assistant",
"content": "Let me check.",
"reasoning_content": "Reading.",
"reasoning": "Reading."
}
])
Expand Down Expand Up @@ -1335,6 +1388,7 @@ fn openai_chat_encrypted_reasoning_details_retain_fallback() -> TestResult {

assert_eq!(output["messages"][0]["reasoning_details"], details);
assert_eq!(output["messages"][0]["reasoning"], "fallback text");
assert_eq!(output["messages"][0]["reasoning_content"], "fallback text");
Ok(())
}

Expand Down