From b05c2e73d27e42d234c5af476b7b1db776d4dbdc Mon Sep 17 00:00:00 2001 From: Mark Davison <6168730+markdavison@users.noreply.github.com> Date: Wed, 4 Nov 2026 15:11:15 +0000 Subject: [PATCH 1/2] fix(translation): map Anthropic thinking budgets to OpenAI reasoning effort Anthropic requests that use `thinking: {type: "enabled", budget_tokens}` reached OpenAI Chat and Responses targets with no reasoning control, and `thinking: {type: "disabled"}` only mapped to `none` for Responses. Derive the effort from the budget's share of `max_tokens` (>=95% xhigh, >=80% high, >=50% medium, otherwise low) with a lossy_conversion diagnostic, and map disabled thinking to `none` for both targets. An explicit `output_config.effort` still takes precedence. Signed-off-by: Mark Davison <6168730+markdavison@users.noreply.github.com> --- .../src/codecs/common.rs | 58 +++++++++ .../src/codecs/openai_chat/buffered.rs | 11 +- .../src/codecs/responses/buffered.rs | 21 +-- .../tests/request_translation.rs | 120 +++++++++++++++++- 4 files changed, 186 insertions(+), 24 deletions(-) diff --git a/crates/switchyard-translation/src/codecs/common.rs b/crates/switchyard-translation/src/codecs/common.rs index 303c90321b..5a789ef7b0 100644 --- a/crates/switchyard-translation/src/codecs/common.rs +++ b/crates/switchyard-translation/src/codecs/common.rs @@ -5,8 +5,11 @@ use serde_json::{Map, Value}; +use crate::diagnostic::TranslationDiagnostic; use crate::error::{Result, TranslationError}; use crate::llm::{ContentBlock, LlmRequest, Tool, ToolChoice}; +use crate::policy::TranslationPolicy; +use crate::util::push_lossy; // Internal provenance survives mutations that invalidate exact request replay. pub(crate) const ANTHROPIC_REQUEST_KEY: &str = "switchyard_anthropic_request"; @@ -16,6 +19,61 @@ pub(crate) fn is_anthropic_request(request: &LlmRequest) -> bool { request.extensions.fields.get(ANTHROPIC_REQUEST_KEY) == Some(&Value::Bool(true)) } +// Budget share of `max_tokens` at or above which each effort applies, in percent. +const BUDGET_EFFORT_BANDS: [(u64, &str); 3] = [(95, "xhigh"), (80, "high"), (50, "medium")]; + +/// Returns the OpenAI reasoning effort for an Anthropic request. +/// +/// An explicit `output_config.effort` wins. Otherwise `thinking.type == "disabled"` maps to +/// `none`, and a legacy `enabled` budget maps to an effort band by its share of `max_tokens`. +/// The budget mapping is approximate, so it records a lossy-conversion diagnostic. +pub(crate) fn openai_effort_for_request( + request: &LlmRequest, + diagnostics: &mut Vec, + policy: &TranslationPolicy, +) -> Result> { + if request.reasoning.effort.is_some() || !is_anthropic_request(request) { + return Ok(request.reasoning.effort.clone()); + } + let Some(thinking) = request.reasoning.raw.as_ref() else { + return Ok(None); + }; + match thinking.get("type").and_then(Value::as_str) { + Some("disabled") => Ok(Some("none".to_string())), + Some("enabled") => { + let budget = thinking.get("budget_tokens").and_then(Value::as_u64); + let (Some(budget), Some(max_tokens)) = (budget, request.output.max_output_tokens) + else { + return Ok(None); + }; + if max_tokens == 0 { + return Ok(None); + } + let effort = budget_effort(budget, max_tokens); + push_lossy( + diagnostics, + policy, + format!( + "Anthropic thinking budget_tokens {budget} of max_tokens {max_tokens} \ + approximated as reasoning effort {effort}" + ), + )?; + Ok(Some(effort.to_string())) + } + _ => Ok(None), + } +} + +fn budget_effort(budget: u64, max_tokens: u64) -> &'static str { + let budget = u128::from(budget) * 100; + for (percent, effort) in BUDGET_EFFORT_BANDS { + if budget >= u128::from(max_tokens) * u128::from(percent) { + return effort; + } + } + "low" +} + /// Converts an OpenAI allowed-tools policy to a restricted function list and mode. pub(crate) fn allowed_function_tools( request: &LlmRequest, diff --git a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs index 7ce8519c5c..d13e445a89 100644 --- a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs +++ b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs @@ -6,8 +6,8 @@ use serde_json::{Map, Value, json}; use crate::codecs::common::{ - first_nonempty_string, is_known_role_name, provider_extensions, reasoning_text_from_blocks, - reasoning_text_from_details, text_from_blocks, + first_nonempty_string, is_known_role_name, openai_effort_for_request, provider_extensions, + reasoning_text_from_blocks, reasoning_text_from_details, text_from_blocks, }; use crate::codecs::openai_media::{ ImagePayload, file_payload, file_source_text, image_payload, image_source_text, @@ -253,11 +253,8 @@ impl FormatCodec for OpenAiChatCodec { if request.stream { body.insert("stream".to_string(), Value::Bool(true)); } - if let Some(effort) = &request.reasoning.effort { - body.insert( - "reasoning_effort".to_string(), - Value::String(effort.clone()), - ); + if let Some(effort) = openai_effort_for_request(request, &mut diagnostics, policy)? { + body.insert("reasoning_effort".to_string(), Value::String(effort)); } if let Some(format) = encode_openai_format(&request.output, &mut diagnostics, policy)? { body.insert("response_format".to_string(), format); diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rs index a3558c1477..862e12c090 100644 --- a/crates/switchyard-translation/src/codecs/responses/buffered.rs +++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs @@ -9,8 +9,8 @@ use serde_json::{Map, Value, json}; use crate::codecs::common::{ collect_responses_reasoning_text, encrypted_reasoning_data, encrypted_reasoning_item_id, - is_anthropic_request, is_known_role_name, provider_extensions, reasoning_text_from_blocks, - text_from_blocks, + is_anthropic_request, is_known_role_name, openai_effort_for_request, provider_extensions, + reasoning_text_from_blocks, text_from_blocks, }; use crate::codecs::openai_chat::{decode_file_source, decode_image_source}; use crate::codecs::openai_media::{ @@ -264,20 +264,9 @@ impl FormatCodec for OpenAiResponsesCodec { ); } // An Anthropic request's raw reasoning is its `thinking` object, which Responses - // does not accept. Keep only whether thinking is disabled. + // does not accept; only the effort derived from it is kept. let mut reasoning = if is_anthropic_request(request) { - let mut reasoning = Map::new(); - if request - .reasoning - .raw - .as_ref() - .and_then(|thinking| thinking.get("type")) - .and_then(Value::as_str) - == Some("disabled") - { - reasoning.insert("effort".to_string(), json!("none")); - } - reasoning + Map::new() } else { request .reasoning @@ -287,7 +276,7 @@ impl FormatCodec for OpenAiResponsesCodec { .cloned() .unwrap_or_default() }; - if let Some(effort) = &request.reasoning.effort { + if let Some(effort) = openai_effort_for_request(request, &mut diagnostics, policy)? { reasoning.insert("effort".to_string(), json!(effort)); } if !reasoning.is_empty() { diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rs index 8817102fd8..f42ccb8dce 100644 --- a/crates/switchyard-translation/tests/request_translation.rs +++ b/crates/switchyard-translation/tests/request_translation.rs @@ -635,7 +635,7 @@ fn anthropic_thinking_to_responses_uses_normalized_effort() -> TestResult { ( json!({"type": "enabled", "budget_tokens": 2048}), None, - None, + Some(json!({"effort": "medium"})), ), ( json!({"type": "disabled"}), @@ -722,6 +722,124 @@ fn anthropic_reconstruction_rejects_fallbacks_with_credit_token() { )); } +#[test] +fn anthropic_thinking_budget_maps_to_openai_effort_bands() -> TestResult { + let engine = TranslationEngine::default(); + let policy = normalized_policy(); + for (budget, effort) in [ + (9500, "xhigh"), + (9499, "high"), + (8000, "high"), + (7999, "medium"), + (5000, "medium"), + (4999, "low"), + (1024, "low"), + ] { + let body = json!({ + "model": "route", + "max_tokens": 10000, + "messages": [{"role": "user", "content": "hi"}], + "thinking": {"type": "enabled", "budget_tokens": budget} + }); + let chat = engine.translate_request( + WireFormat::AnthropicMessages, + WireFormat::OpenAiChat, + &body, + &policy, + )?; + assert_eq!( + chat.body["reasoning_effort"], + json!(effort), + "budget {budget}" + ); + assert!( + chat.diagnostics + .iter() + .any(|diagnostic| diagnostic.code == "lossy_conversion"), + "budget {budget} records the approximation" + ); + let responses = engine.translate_request( + WireFormat::AnthropicMessages, + WireFormat::OpenAiResponses, + &body, + &policy, + )?; + assert_eq!( + responses.body["reasoning"], + json!({"effort": effort}), + "budget {budget}" + ); + } + Ok(()) +} + +#[test] +fn anthropic_thinking_controls_to_chat_effort() -> TestResult { + let engine = TranslationEngine::default(); + let policy = normalized_policy(); + for (thinking, output_config, expected) in [ + (json!({"type": "disabled"}), None, Some("none")), + ( + json!({"type": "adaptive"}), + Some(json!({"effort": "low"})), + Some("low"), + ), + (json!({"type": "adaptive"}), None, None), + ( + json!({"type": "enabled", "budget_tokens": 9000}), + Some(json!({"effort": "low"})), + Some("low"), + ), + ] { + let mut body = json!({ + "model": "route", + "max_tokens": 10000, + "messages": [{"role": "user", "content": "hi"}], + "thinking": thinking + }); + if let Some(output_config) = output_config { + body["output_config"] = output_config; + } + let output = engine.translate_request( + WireFormat::AnthropicMessages, + WireFormat::OpenAiChat, + &body, + &policy, + )?; + assert_eq!( + output.body.get("reasoning_effort").and_then(Value::as_str), + expected, + "{body}" + ); + assert!(output.diagnostics.is_empty(), "{body}"); + } + Ok(()) +} + +#[test] +fn anthropic_thinking_budget_is_rejected_under_strict_lossy_policy() { + let engine = TranslationEngine::default(); + let policy = TranslationPolicy { + lossy_conversion_policy: LossyConversionPolicy::Reject, + ..normalized_policy() + }; + let body = json!({ + "model": "route", + "max_tokens": 10000, + "messages": [{"role": "user", "content": "hi"}], + "thinking": {"type": "enabled", "budget_tokens": 4000} + }); + let error = engine + .translate_request( + WireFormat::AnthropicMessages, + WireFormat::OpenAiChat, + &body, + &policy, + ) + .expect_err("approximating a thinking budget is lossy"); + assert!(matches!(error, TranslationError::LossyConversion(_))); +} + #[test] fn anthropic_reconstruction_preserves_thinking() -> TestResult { let engine = TranslationEngine::default(); From 43ab399a353cced9c5e5017f0d71213e6a115931 Mon Sep 17 00:00:00 2001 From: Mark Davison <6168730+markdavison@users.noreply.github.com> Date: Thu, 5 Nov 2026 09:45:43 +0000 Subject: [PATCH 2/2] fix(translation): cap budget-derived effort at high and omit none on chat Every OpenAI reasoning model accepts low, medium and high, so a budget share no longer maps to xhigh. Disabled thinking keeps its existing none effort on Responses and adds no effort on Chat Completions. Signed-off-by: Mark Davison <6168730+markdavison@users.noreply.github.com> --- .../switchyard-translation/src/codecs/common.rs | 12 ++++++------ .../src/codecs/responses/buffered.rs | 15 +++++++++++++-- .../tests/request_translation.rs | 6 +++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/crates/switchyard-translation/src/codecs/common.rs b/crates/switchyard-translation/src/codecs/common.rs index 5a789ef7b0..e93938068c 100644 --- a/crates/switchyard-translation/src/codecs/common.rs +++ b/crates/switchyard-translation/src/codecs/common.rs @@ -19,14 +19,15 @@ pub(crate) fn is_anthropic_request(request: &LlmRequest) -> bool { request.extensions.fields.get(ANTHROPIC_REQUEST_KEY) == Some(&Value::Bool(true)) } -// Budget share of `max_tokens` at or above which each effort applies, in percent. -const BUDGET_EFFORT_BANDS: [(u64, &str); 3] = [(95, "xhigh"), (80, "high"), (50, "medium")]; +// Budget share of `max_tokens` at or above which each effort applies, in percent. The bands stop +// at `high` because every OpenAI reasoning model accepts low, medium and high. +const BUDGET_EFFORT_BANDS: [(u64, &str); 2] = [(80, "high"), (50, "medium")]; /// Returns the OpenAI reasoning effort for an Anthropic request. /// -/// An explicit `output_config.effort` wins. Otherwise `thinking.type == "disabled"` maps to -/// `none`, and a legacy `enabled` budget maps to an effort band by its share of `max_tokens`. -/// The budget mapping is approximate, so it records a lossy-conversion diagnostic. +/// An explicit `output_config.effort` wins. Otherwise a legacy `enabled` budget maps to an effort +/// band by its share of `max_tokens`. The budget mapping is approximate, so it records a +/// lossy-conversion diagnostic. pub(crate) fn openai_effort_for_request( request: &LlmRequest, diagnostics: &mut Vec, @@ -39,7 +40,6 @@ pub(crate) fn openai_effort_for_request( return Ok(None); }; match thinking.get("type").and_then(Value::as_str) { - Some("disabled") => Ok(Some("none".to_string())), Some("enabled") => { let budget = thinking.get("budget_tokens").and_then(Value::as_u64); let (Some(budget), Some(max_tokens)) = (budget, request.output.max_output_tokens) diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rs index 862e12c090..5f4fef1d3b 100644 --- a/crates/switchyard-translation/src/codecs/responses/buffered.rs +++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs @@ -264,9 +264,20 @@ impl FormatCodec for OpenAiResponsesCodec { ); } // An Anthropic request's raw reasoning is its `thinking` object, which Responses - // does not accept; only the effort derived from it is kept. + // does not accept. Keep whether thinking is disabled and the effort derived from it. let mut reasoning = if is_anthropic_request(request) { - Map::new() + let mut reasoning = Map::new(); + if request + .reasoning + .raw + .as_ref() + .and_then(|thinking| thinking.get("type")) + .and_then(Value::as_str) + == Some("disabled") + { + reasoning.insert("effort".to_string(), json!("none")); + } + reasoning } else { request .reasoning diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rs index f42ccb8dce..7e19ecc158 100644 --- a/crates/switchyard-translation/tests/request_translation.rs +++ b/crates/switchyard-translation/tests/request_translation.rs @@ -727,8 +727,8 @@ fn anthropic_thinking_budget_maps_to_openai_effort_bands() -> TestResult { let engine = TranslationEngine::default(); let policy = normalized_policy(); for (budget, effort) in [ - (9500, "xhigh"), - (9499, "high"), + (10000, "high"), + (9500, "high"), (8000, "high"), (7999, "medium"), (5000, "medium"), @@ -778,7 +778,7 @@ fn anthropic_thinking_controls_to_chat_effort() -> TestResult { let engine = TranslationEngine::default(); let policy = normalized_policy(); for (thinking, output_config, expected) in [ - (json!({"type": "disabled"}), None, Some("none")), + (json!({"type": "disabled"}), None, None), ( json!({"type": "adaptive"}), Some(json!({"effort": "low"})),