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): translate Anthropic thinking controls safely to Responses (#840)

Graham Kingb2904ee4c212
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
index 646c33e47..c6cdcfba5 100644
--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
@@ -6,7 +6,8 @@
use serde_json::{Map, Value, json};
use crate::codecs::common::{
- ANTHROPIC_REQUEST_KEY, is_known_role_name, provider_extensions, text_from_blocks,
+ ANTHROPIC_REQUEST_KEY, is_anthropic_request, is_known_role_name, provider_extensions,
+ text_from_blocks,
};
use crate::codecs::openai_chat::{decode_file_source, decode_image_source};
use crate::codecs::{
@@ -252,7 +253,7 @@ impl FormatCodec for AnthropicMessagesCodec {
}
body.insert("tool_choice".to_string(), choice);
}
- if request.extensions.fields.get(ANTHROPIC_REQUEST_KEY) == Some(&Value::Bool(true)) {
+ if is_anthropic_request(request) {
for field in [
"inference_geo",
"service_tier",
diff --git a/crates/switchyard-translation/src/codecs/common.rs b/crates/switchyard-translation/src/codecs/common.rs
index e5b9b7dbe..b8e0fc20c 100644
--- a/crates/switchyard-translation/src/codecs/common.rs
+++ b/crates/switchyard-translation/src/codecs/common.rs
@@ -11,6 +11,11 @@ use crate::llm::{ContentBlock, LlmRequest, ToolChoice, ToolDefinition};
// Internal provenance survives mutations that invalidate exact request replay.
pub(crate) const ANTHROPIC_REQUEST_KEY: &str = "switchyard_anthropic_request";
+/// Returns true when the request was decoded from an Anthropic Messages body.
+pub(crate) fn is_anthropic_request(request: &LlmRequest) -> bool {
+ request.extensions.fields.get(ANTHROPIC_REQUEST_KEY) == Some(&Value::Bool(true))
+}
+
/// 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/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rs
index 724df4dba..c9bb8828f 100644
--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs
@@ -9,7 +9,8 @@ use serde_json::{Map, Value, json};
use crate::codecs::common::{
collect_responses_reasoning_text, encrypted_reasoning_data, encrypted_reasoning_item_id,
- is_known_role_name, provider_extensions, reasoning_text_from_blocks, text_from_blocks,
+ is_anthropic_request, is_known_role_name, 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::{
@@ -256,13 +257,30 @@ impl FormatCodec for OpenAiResponsesCodec {
json!({"format": encode_responses_text_format(response_format)}),
);
}
- let mut reasoning = request
- .reasoning
- .raw
- .as_ref()
- .and_then(Value::as_object)
- .cloned()
- .unwrap_or_default();
+ // An Anthropic request's raw reasoning is its `thinking` object, which Responses
+ // does not accept. Keep only whether thinking is disabled.
+ 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
+ } else {
+ request
+ .reasoning
+ .raw
+ .as_ref()
+ .and_then(Value::as_object)
+ .cloned()
+ .unwrap_or_default()
+ };
if let Some(effort) = &request.reasoning.effort {
reasoning.insert("effort".to_string(), json!(effort));
}
diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rs
index 6d2574db4..078fa46d1 100644
--- a/crates/switchyard-translation/tests/request_translation.rs
+++ b/crates/switchyard-translation/tests/request_translation.rs
@@ -437,6 +437,47 @@ fn responses_reasoning_survives_rebuild() -> TestResult {
Ok(())
}
+#[test]
+fn anthropic_thinking_to_responses_uses_normalized_effort() -> TestResult {
+ let engine = TranslationEngine::default();
+ let policy = normalized_policy();
+ for (thinking, effort, expected) in [
+ (
+ json!({"type": "adaptive"}),
+ Some("low"),
+ Some(json!({"effort": "low"})),
+ ),
+ (
+ json!({"type": "enabled", "budget_tokens": 2048}),
+ None,
+ None,
+ ),
+ (
+ json!({"type": "disabled"}),
+ None,
+ Some(json!({"effort": "none"})),
+ ),
+ ] {
+ let mut body = json!({
+ "model": "route",
+ "max_tokens": 4096,
+ "messages": [{"role": "user", "content": "hi"}],
+ "thinking": thinking
+ });
+ if let Some(effort) = effort {
+ body["output_config"] = json!({"effort": effort});
+ }
+ let output = engine.translate_request(
+ WireFormat::AnthropicMessages,
+ WireFormat::OpenAiResponses,
+ &body,
+ &policy,
+ )?;
+ assert_eq!(output.body.get("reasoning"), expected.as_ref());
+ }
+ Ok(())
+}
+
#[test]
fn anthropic_target_prompt_preserves_native_request_fields() -> TestResult {
let engine = TranslationEngine::default();