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): omit deprecated Anthropic temperature (#1305)
David Gardnerd333a047e5a9
diff --git a/crates/protocol/src/llm.rs b/crates/protocol/src/llm.rsindex 1e4e6e9c3..9713e920f 100644--- a/crates/protocol/src/llm.rs+++ b/crates/protocol/src/llm.rs@@ -15,6 +15,9 @@ use serde_json::{Map, Value};use crate::format::FormatId;+/// Maximum temperature on the shared IR's zero-based scale.+pub const MAX_IR_TEMPERATURE: f64 = 2.0;+/// Actor role normalized across provider APIs.#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, strum_macros::Display)]#[serde(rename_all = "snake_case")]@@ -457,7 +460,9 @@ pub enum ToolChoice {/// Provider sampling parameters with common cross-provider names.#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]pub struct SamplingParams {- /// Sampling temperature.+ /// Sampling temperature on the `0.0..=MAX_IR_TEMPERATURE` scale.+ /// Directly constructed requests use this scale; omission retains provider defaults.+ /// See [`MAX_IR_TEMPERATURE`].pub temperature: Option<f64>,/// Nucleus-sampling probability mass.pub top_p: Option<f64>,diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rsindex 07863e5fd..ce8b50066 100644--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs@@ -4,10 +4,12 @@//! Buffered codec for Anthropic Messages request and response JSON.use serde_json::{Map, Value, json};+use switchyard_protocol::MAX_IR_TEMPERATURE;+use super::{DEFAULT_TEMPERATURE, MAX_TEMPERATURE};use crate::codecs::common::{ANTHROPIC_REQUEST_KEY, is_anthropic_request, is_known_role_name, provider_extensions,- text_from_blocks,+ scale_temperature, text_from_blocks,};use crate::codecs::openai_chat::{decode_file_source, decode_image_source};use crate::codecs::structured_output::decode_openai_schema_enforcement;@@ -79,7 +81,10 @@ impl FormatCodec for AnthropicMessagesCodec {response_format,},sampling: SamplingParams {- temperature: body.get("temperature").and_then(Value::as_f64),+ temperature: body+ .get("temperature")+ .and_then(Value::as_f64)+ .map(|value| scale_temperature(value, MAX_TEMPERATURE, MAX_IR_TEMPERATURE)),top_p: body.get("top_p").and_then(Value::as_f64),top_k: body.get("top_k").and_then(Value::as_i64),},@@ -327,8 +332,19 @@ impl FormatCodec for AnthropicMessagesCodec {} else {body.insert("max_tokens".to_string(), json!(64_000));}- if let Some(value) = request.sampling.temperature {- body.insert("temperature".to_string(), json!(value));+ if let Some(value) = request+ .sampling+ .temperature+ .map(|value| scale_temperature(value, MAX_IR_TEMPERATURE, MAX_TEMPERATURE))+ && value != DEFAULT_TEMPERATURE+ {+ push_lossy(+ &mut diagnostics,+ policy,+ format!(+ "deprecated temperature parameter with non-default value {value} is omitted from Anthropic Messages requests"+ ),+ )?;}if let Some(value) = request.sampling.top_p {body.insert("top_p".to_string(), json!(value));diff --git a/crates/switchyard-translation/src/codecs/anthropic/mod.rs b/crates/switchyard-translation/src/codecs/anthropic/mod.rsindex d990cd988..5c0439485 100644--- a/crates/switchyard-translation/src/codecs/anthropic/mod.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/mod.rs@@ -8,3 +8,10 @@ mod stream;pub use buffered::AnthropicMessagesCodec;pub use stream::AnthropicMessagesStreamCodec;++// Temperature range: 0.0..=1.0 for legacy models supporting this parameter.+// https://platform.claude.com/docs/en/api/messages/create?version=2023-06-01+// API version 2026-10-21 removes temperature; sending it returns HTTP 400.+// https://platform.claude.com/docs/en/api/migration-2026-10-21#sampling-parameters-are-removed+pub(crate) const MAX_TEMPERATURE: f64 = 1.0;+pub(crate) const DEFAULT_TEMPERATURE: f64 = 1.0;diff --git a/crates/switchyard-translation/src/codecs/common.rs b/crates/switchyard-translation/src/codecs/common.rsindex 303c90321..eb77bb23b 100644--- a/crates/switchyard-translation/src/codecs/common.rs+++ b/crates/switchyard-translation/src/codecs/common.rs@@ -11,6 +11,14 @@ use crate::llm::{ContentBlock, LlmRequest, Tool, ToolChoice};// Internal provenance survives mutations that invalidate exact request replay.pub(crate) const ANTHROPIC_REQUEST_KEY: &str = "switchyard_anthropic_request";+/// Scales temperature between ranges with a minimum of zero.+pub(crate) fn scale_temperature(temperature: f64, source_max: f64, target_max: f64) -> f64 {+ if source_max == target_max {+ return temperature;+ }+ (temperature / source_max) * target_max+}+/// 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))diff --git a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rsindex 802a0dcf8..923bb2578 100644--- a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs+++ b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs@@ -4,10 +4,12 @@//! Buffered codec for OpenAI Chat Completions request and response JSON.use serde_json::{Map, Value, json};+use switchyard_protocol::MAX_IR_TEMPERATURE;+use super::MAX_TEMPERATURE;use crate::codecs::common::{first_nonempty_string, is_known_role_name, provider_extensions, reasoning_text_from_blocks,- reasoning_text_from_details, text_from_blocks,+ reasoning_text_from_details, scale_temperature, text_from_blocks,};use crate::codecs::openai_media::{ImagePayload, file_payload, file_source_text, image_payload, image_source_text,@@ -50,7 +52,10 @@ impl FormatCodec for OpenAiChatCodec {.map(ToOwned::to_owned),stream: body.get("stream").and_then(Value::as_bool).unwrap_or(false),sampling: SamplingParams {- temperature: body.get("temperature").and_then(Value::as_f64),+ temperature: body+ .get("temperature")+ .and_then(Value::as_f64)+ .map(|value| scale_temperature(value, MAX_TEMPERATURE, MAX_IR_TEMPERATURE)),top_p: body.get("top_p").and_then(Value::as_f64),top_k: None,},@@ -245,6 +250,7 @@ impl FormatCodec for OpenAiChatCodec {body.insert("max_completion_tokens".to_string(), json!(value));}if let Some(value) = request.sampling.temperature {+ let value = scale_temperature(value, MAX_IR_TEMPERATURE, MAX_TEMPERATURE);body.insert("temperature".to_string(), json!(value));}if let Some(value) = request.sampling.top_p {diff --git a/crates/switchyard-translation/src/codecs/openai_chat/mod.rs b/crates/switchyard-translation/src/codecs/openai_chat/mod.rsindex 766f312c6..75aed3e95 100644--- a/crates/switchyard-translation/src/codecs/openai_chat/mod.rs+++ b/crates/switchyard-translation/src/codecs/openai_chat/mod.rs@@ -10,3 +10,7 @@ pub use buffered::OpenAiChatCodec;pub use stream::OpenAiChatStreamCodec;pub(crate) use buffered::{decode_file_source, decode_image_source};++// Temperature range: 0.0..=2.0.+// https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create+pub(crate) const MAX_TEMPERATURE: f64 = 2.0;diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rsindex 8f7d47af9..d6f495bee 100644--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs@@ -6,11 +6,13 @@use std::collections::HashMap;use serde_json::{Map, Value, json};+use switchyard_protocol::MAX_IR_TEMPERATURE;+use super::MAX_TEMPERATURE;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,+ scale_temperature, text_from_blocks,};use crate::codecs::openai_chat::{decode_file_source, decode_image_source};use crate::codecs::openai_media::{@@ -68,7 +70,10 @@ impl FormatCodec for OpenAiResponsesCodec {raw: body.get("reasoning").cloned(),},sampling: SamplingParams {- temperature: body.get("temperature").and_then(Value::as_f64),+ temperature: body+ .get("temperature")+ .and_then(Value::as_f64)+ .map(|value| scale_temperature(value, MAX_TEMPERATURE, MAX_IR_TEMPERATURE)),top_p: body.get("top_p").and_then(Value::as_f64),top_k: None,},@@ -288,6 +293,7 @@ impl FormatCodec for OpenAiResponsesCodec {body.insert("reasoning".to_string(), Value::Object(reasoning));}if let Some(value) = request.sampling.temperature {+ let value = scale_temperature(value, MAX_IR_TEMPERATURE, MAX_TEMPERATURE);body.insert("temperature".to_string(), json!(value));}if let Some(value) = request.sampling.top_p {diff --git a/crates/switchyard-translation/src/codecs/responses/mod.rs b/crates/switchyard-translation/src/codecs/responses/mod.rsindex 8f18e26fb..91985549e 100644--- a/crates/switchyard-translation/src/codecs/responses/mod.rs+++ b/crates/switchyard-translation/src/codecs/responses/mod.rs@@ -9,6 +9,10 @@ mod stream;pub use buffered::OpenAiResponsesCodec;pub use stream::OpenAiResponsesStreamCodec;+// Temperature range: 0.0..=2.0.+// https://developers.openai.com/api/reference/resources/responses/methods/create+pub(crate) const MAX_TEMPERATURE: f64 = 2.0;+pub(crate) fn is_native_output(kind: &str) -> bool {matches!(kind,diff --git a/crates/switchyard-translation/tests/lossless_roundtrip.rs b/crates/switchyard-translation/tests/lossless_roundtrip.rsindex db99a71fd..b507c19b4 100644--- a/crates/switchyard-translation/tests/lossless_roundtrip.rs+++ b/crates/switchyard-translation/tests/lossless_roundtrip.rs@@ -73,6 +73,18 @@ fn embedded_preservation_roundtrips_requests_for_every_source_target_pair_exactl}).body;+ let expected_temperature = match (source, target) {+ (WireFormat::AnthropicMessages, WireFormat::AnthropicMessages) => Some(0.2),+ (WireFormat::AnthropicMessages, _) => Some(0.4),+ (_, WireFormat::AnthropicMessages) => None,+ _ => Some(0.2),+ };+ assert_eq!(+ translated.get("temperature"),+ expected_temperature.map(Value::from).as_ref(),+ "request {source:?} -> {target:?} temperature"+ );+if source == target {assert_eq!(translated, original, "request {source:?} self-translation");} else {diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rsindex d025b4182..d1a4654ce 100644--- a/crates/switchyard-translation/tests/request_translation.rs+++ b/crates/switchyard-translation/tests/request_translation.rs@@ -3869,25 +3869,56 @@ fn openai_tool_strictness_is_preserved_for_anthropic() -> TestResult {Ok(())}-// Verifies Anthropic-bound OpenAI requests get the required max_tokens fallback.+// Verifies Anthropic defaults and the policy for discarded non-default temperatures.#[test]-fn openai_request_to_anthropic_adds_required_default_max_tokens() -> TestResult {+fn openai_request_to_anthropic_parameters() -> TestResult {let engine = TranslationEngine::default();let body = json!({"model": "gpt-5","messages": [{"role": "user", "content": "hi"}]});- let output = engine- .translate_request(- WireFormat::OpenAiChat,- WireFormat::AnthropicMessages,- &body,- &TranslationPolicy::default(),- )?- .body;-- assert_eq!(output["max_tokens"], 64_000);+ for (temperature, is_lossy) in [+ (None, false),+ (Some(1.0), true),+ (Some(0.5), true),+ (Some(2.0), false),+ ] {+ let mut body = body.clone();+ if let Some(temperature) = temperature {+ body["temperature"] = json!(temperature);+ }+ for lossy_conversion_policy in [+ LossyConversionPolicy::AllowWithDiagnostics,+ LossyConversionPolicy::Reject,+ ] {+ let policy = TranslationPolicy {+ lossy_conversion_policy,+ ..normalized_policy()+ };+ let result = engine.translate_request(+ WireFormat::OpenAiChat,+ WireFormat::AnthropicMessages,+ &body,+ &policy,+ );+ if is_lossy && lossy_conversion_policy == LossyConversionPolicy::Reject {+ assert!(+ matches!(result, Err(TranslationError::LossyConversion(message))+ if message.contains("temperature"))+ );+ continue;+ }+ let output = result?;+ assert_eq!(output.body["max_tokens"], 64_000);+ assert!(output.body.get("temperature").is_none());+ assert_eq!(output.diagnostics.len(), usize::from(is_lossy));+ if is_lossy {+ assert_eq!(output.diagnostics[0].code, "lossy_conversion");+ assert!(output.diagnostics[0].message.contains("temperature"));+ }+ }+ }Ok(())}diff --git a/docs/reference/protocols/llm_requests.md b/docs/reference/protocols/llm_requests.mdindex d64ecb734..f2432d5d4 100644--- a/docs/reference/protocols/llm_requests.md+++ b/docs/reference/protocols/llm_requests.md@@ -69,7 +69,7 @@ providers apply their own defaults and accepted ranges.| Protocol field and type | Meaning | Anthropic Messages | OpenAI Responses | Chat Completions ||---|---|---|---|---|-| <a href="../../../rust/switchyard_protocol/llm/struct.SamplingParams.html#structfield.temperature"><code>SamplingParams.temperature: Option<f64></code></a> | Sampling temperature. | `temperature` | `temperature` | `temperature` |+| <a href="../../../rust/switchyard_protocol/llm/struct.SamplingParams.html#structfield.temperature"><code>SamplingParams.temperature: Option<f64></code></a> | Sampling temperature. | (omitted; refer to the note below) | `temperature` | `temperature` || <a href="../../../rust/switchyard_protocol/llm/struct.SamplingParams.html#structfield.top_p"><code>SamplingParams.top_p: Option<f64></code></a> | Nucleus-sampling probability mass. | `top_p` | `top_p` | `top_p` || <a href="../../../rust/switchyard_protocol/llm/struct.SamplingParams.html#structfield.top_k"><code>SamplingParams.top_k: Option<i64></code></a> | Maximum candidate-token count. | `top_k` | No standard field | No standard field || <a href="../../../rust/switchyard_protocol/llm/struct.OutputParams.html#structfield.max_output_tokens"><code>OutputParams.max_output_tokens: Option<u64></code></a> | Output token budget. | `max_tokens` | `max_output_tokens` | `max_completion_tokens`, or legacy `max_tokens` |@@ -78,6 +78,32 @@ providers apply their own defaults and accepted ranges.| <a href="../../../rust/switchyard_protocol/llm/struct.ReasoningParams.html#structfield.effort"><code>ReasoningParams.effort: Option<String></code></a> | Requested reasoning effort. | `output_config.effort` | `reasoning.effort` | `reasoning_effort` || <a href="../../../rust/switchyard_protocol/llm/struct.ReasoningParams.html#structfield.raw"><code>ReasoningParams.raw: Option<Value></code></a> | Provider-specific reasoning configuration. | `thinking` controls | `reasoning` object | No standard equivalent for the whole object |+**Anthropic temperature:** Switchyard omits `temperature` when rebuilding Anthropic+requests. Older models still support this parameter under API version `2023-06-01`,+but models released after Claude Opus 4.6 reject values other than the default+`1.0`. For model restrictions, refer to the [Anthropic parameter documentation](https://platform.claude.com/docs/en/api/messages/create?version=2023-06-01).+API version `2026-10-21` removes the parameter entirely and rejects it even at its+default value. For removal details, refer to [Anthropic's migration guide](https://platform.claude.com/docs/en/api/migration-2026-10-21#sampling-parameters-are-removed).+Switchyard scales OpenAI temperatures from the `0.0–2.0` range to Anthropic's+`0.0–1.0` range before comparing with the default. An OpenAI temperature of `2.0`+scales to Anthropic's default of `1.0` and is omitted without diagnostics or rejection.+Omitting a non-default value after scaling to Anthropic's range follows the+[lossy-conversion policy](translation.md#policies). Exact preserved Anthropic+requests can still retain `temperature`.++For older Anthropic models that support non-default temperatures under API version+`2023-06-01`, add these settings to the target's TOML configuration:++```toml+omit_body_fields = ["temperature"]+extra_body = { temperature = 0.5 }+```++After translation, Switchyard removes any existing `temperature` and sets it to+`0.5` in Anthropic's units, without further scaling. This also overrides temperature+in preserved requests. Refer to the [target configuration reference](../toml_schema.md#targetsname)+for these settings.+Preserved same-format bodies take precedence over edits to these settings,including schema enforcement. See **[encoding an edited request](translation.md#preservation-and-edits)**.