Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 13 additions & 4 deletions crates/switchyard-translation/src/codecs/anthropic/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,19 @@ impl FormatCodec for AnthropicMessagesCodec {
{
body.insert("thinking".to_string(), thinking.clone());
}
if let Some(effort) = &request.reasoning.effort {
body.entry("thinking".to_string())
.or_insert_with(|| json!({"type": "adaptive"}));
body.insert("output_config".to_string(), json!({"effort": effort}));
// OpenAI also accepts "none" and "minimal", which Anthropic rejects as efforts.
match request.reasoning.effort.as_deref() {
Some("none") => {
body.entry("thinking".to_string())
.or_insert_with(|| json!({"type": "disabled"}));
}
Some(effort) => {
let effort = if effort == "minimal" { "low" } else { effort };
body.entry("thinking".to_string())
.or_insert_with(|| json!({"type": "adaptive"}));
body.insert("output_config".to_string(), json!({"effort": effort}));
}
None => {}
}
if let Some(response_format) = &request.output.response_format
&& let Some(format) = encode_anthropic_output_format(
Expand Down
52 changes: 52 additions & 0 deletions crates/switchyard-translation/tests/request_translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2882,6 +2882,58 @@ fn openai_request_translates_system_developer_and_reasoning_to_anthropic() -> Te
Ok(())
}

// OpenAI accepts "none" and "minimal" reasoning effort, which Anthropic does not.
#[test]
fn openai_reasoning_effort_maps_to_anthropic_effort() -> 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.

can you check if there is an existing test you can extend rather than creating a new one? thank you!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sure, folded it into openai_request_translates_system_developer_and_reasoning_to_anthropic in 88116c7. It reuses that request and just loops over none and minimal at the end. Sorry for the delay.

let engine = TranslationEngine::default();
for (effort, thinking, output_config) in [
("none", json!({"type": "disabled"}), None),
(
"minimal",
json!({"type": "adaptive"}),
Some(json!({"effort": "low"})),
),
(
"low",
json!({"type": "adaptive"}),
Some(json!({"effort": "low"})),
),
(
"high",
json!({"type": "adaptive"}),
Some(json!({"effort": "high"})),
),
] {
let messages = json!([{"role": "user", "content": "hi"}]);
for (format, body) in [
(
WireFormat::OpenAiChat,
json!({"model": "gpt-5", "messages": messages, "reasoning_effort": effort}),
),
(
WireFormat::OpenAiResponses,
json!({"model": "gpt-5", "input": messages, "reasoning": {"effort": effort}}),
),
] {
let output = engine
.translate_request(
format,
WireFormat::AnthropicMessages,
&body,
&TranslationPolicy::default(),
)?
.body;
assert_eq!(output["thinking"], thinking, "{format:?} {effort}");
assert_eq!(
output.get("output_config"),
output_config.as_ref(),
"{format:?} {effort}"
);
}
}
Ok(())
}

// Builds an Anthropic request whose structured output uses the given field shape.
fn anthropic_structured_output_request(output: Value) -> Value {
let mut body = json!({
Expand Down
Loading