From 3359e4618b61b8ba12dc7f593812fc729c7d8196 Mon Sep 17 00:00:00 2001 From: Bharadwaj Pendyala Date: Tue, 6 Oct 2026 08:42:06 -0500 Subject: [PATCH 1/2] fix(translation): report Anthropic context-window stops as a token limit Signed-off-by: Bharadwaj Pendyala --- .../src/codecs/anthropic/buffered.rs | 2 +- .../src/codecs/anthropic/stream.rs | 5 ++ .../tests/response_translation.rs | 40 +++++++++++++ .../tests/stream_translation.rs | 59 +++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs index de6d7c2108..fe64e70323 100644 --- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs +++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs @@ -1279,7 +1279,7 @@ fn encode_anthropic_usage(usage: &Usage) -> Value { // Maps Anthropic stop reasons to normalized stop reasons. fn map_anthropic_stop_reason(reason: Option<&str>) -> StopReason { match reason { - Some("max_tokens") => StopReason::MaxTokens, + Some("max_tokens" | "model_context_window_exceeded") => StopReason::MaxTokens, Some("tool_use") => StopReason::ToolUse, Some("refusal") => StopReason::ContentFilter, Some("end_turn") | None => StopReason::EndTurn, diff --git a/crates/switchyard-translation/src/codecs/anthropic/stream.rs b/crates/switchyard-translation/src/codecs/anthropic/stream.rs index 908941a89f..8d9f7adb6e 100644 --- a/crates/switchyard-translation/src/codecs/anthropic/stream.rs +++ b/crates/switchyard-translation/src/codecs/anthropic/stream.rs @@ -127,6 +127,11 @@ fn decode_anthropic_stream( .and_then(|delta| delta.get("stop_reason")) .and_then(Value::as_str) { + // A context-window cutoff truncates the answer just like `max_tokens`. + let stop_reason = match stop_reason { + "model_context_window_exceeded" => "max_tokens", + reason => reason, + }; // Remember the provider stop reason: Anthropic delivers it here, on // `message_delta`, while the terminal `message_stop` carries none of its own. state.stop_reason = Some(stop_reason.to_string()); diff --git a/crates/switchyard-translation/tests/response_translation.rs b/crates/switchyard-translation/tests/response_translation.rs index 4280fa3f11..efcd6aefbe 100644 --- a/crates/switchyard-translation/tests/response_translation.rs +++ b/crates/switchyard-translation/tests/response_translation.rs @@ -1029,6 +1029,46 @@ fn incomplete_responses_source_survives_translation() -> TestResult { Ok(()) } +// Verifies Anthropic's context-window stop is reported as a token limit. +#[test] +fn anthropic_context_window_stop_translates_to_token_limit() -> TestResult { + let engine = TranslationEngine::default(); + let body = json!({ + "id": "msg_test", + "type": "message", + "role": "assistant", + "model": "claude-sonnet", + "content": [{"type": "text", "text": "Half an ans"}], + "stop_reason": "model_context_window_exceeded", + "usage": {"input_tokens": 12, "output_tokens": 7} + }); + + let chat = engine + .translate_response( + WireFormat::AnthropicMessages, + WireFormat::OpenAiChat, + &body, + &TranslationPolicy::default(), + )? + .body; + assert_eq!(chat["choices"][0]["finish_reason"], "length"); + + let responses = engine + .translate_response( + WireFormat::AnthropicMessages, + WireFormat::OpenAiResponses, + &body, + &TranslationPolicy::default(), + )? + .body; + assert_eq!(responses["status"], "incomplete"); + assert_eq!( + responses["incomplete_details"], + json!({"reason": "max_output_tokens"}) + ); + Ok(()) +} + #[test] fn failed_responses_return_upstream_failure_with_provider_message() -> TestResult { let engine = TranslationEngine::default(); diff --git a/crates/switchyard-translation/tests/stream_translation.rs b/crates/switchyard-translation/tests/stream_translation.rs index 26d65adae9..867656b25c 100644 --- a/crates/switchyard-translation/tests/stream_translation.rs +++ b/crates/switchyard-translation/tests/stream_translation.rs @@ -2424,6 +2424,65 @@ fn anthropic_max_tokens_stop_translates_to_responses_incomplete_event() -> TestR Ok(()) } +// Verifies a streamed Anthropic context-window stop is reported as a token limit. +#[test] +fn anthropic_context_window_stop_streams_as_token_limit() -> TestResult { + let engine = TranslationEngine::default(); + let delta = json!({ + "type": "message_delta", + "delta": {"stop_reason": "model_context_window_exceeded"}, + "usage": {"output_tokens": 1} + }); + + let mut state = + StreamTranslationState::new(WireFormat::AnthropicMessages, WireFormat::OpenAiChat); + let mut events = engine.translate_event( + &mut state, + WireFormat::AnthropicMessages, + WireFormat::OpenAiChat, + &delta, + )?; + events.extend(engine.finish_stream(&mut state, WireFormat::OpenAiChat)?); + let finish_reasons: Vec<&Value> = events + .iter() + .map(|event| &event["choices"][0]["finish_reason"]) + .filter(|reason| !reason.is_null()) + .collect(); + assert_eq!(finish_reasons, vec!["length"]); + + let mut state = + StreamTranslationState::new(WireFormat::AnthropicMessages, WireFormat::OpenAiResponses); + let mut events = engine.translate_event( + &mut state, + WireFormat::AnthropicMessages, + WireFormat::OpenAiResponses, + &delta, + )?; + events.extend(engine.finish_stream(&mut state, WireFormat::OpenAiResponses)?); + let Some(terminal) = events.last() else { + return Err("finish should emit a terminal Responses event".into()); + }; + assert_eq!(terminal["type"], "response.incomplete"); + assert_eq!( + terminal["response"]["incomplete_details"], + json!({"reason": "max_output_tokens"}) + ); + + // The server buffers a stream through the accumulator before encoding it. + let mut state = + StreamTranslationState::new(WireFormat::AnthropicMessages, WireFormat::AnthropicMessages); + let decoded = engine.decode_stream_event(&mut state, WireFormat::AnthropicMessages, delta)?; + let mut accumulator = ResponseAccumulator::new(); + for chunk in decoded.normalized() { + accumulator.push(chunk.clone()); + } + assert_eq!( + accumulator.finish().outputs[0].stop_reason, + Some(StopReason::MaxTokens) + ); + Ok(()) +} + // Verifies a streamed response.incomplete from a Responses upstream reaches a Chat client. #[test] fn responses_incomplete_event_translates_to_chat_length_finish() -> TestResult { From 981cc86f3ff3f80bffb6f9824da7ef439dd52307 Mon Sep 17 00:00:00 2001 From: Bharadwaj Pendyala Date: Thu, 26 Nov 2026 21:15:10 -0600 Subject: [PATCH 2/2] fix(translation): say the stored Anthropic stop reason is normalized Signed-off-by: Bharadwaj Pendyala --- crates/switchyard-translation/src/codecs/anthropic/stream.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/switchyard-translation/src/codecs/anthropic/stream.rs b/crates/switchyard-translation/src/codecs/anthropic/stream.rs index 9f567dcc96..3fd02cfdc5 100644 --- a/crates/switchyard-translation/src/codecs/anthropic/stream.rs +++ b/crates/switchyard-translation/src/codecs/anthropic/stream.rs @@ -151,7 +151,7 @@ fn decode_anthropic_stream( "model_context_window_exceeded" => "max_tokens", reason => reason, }; - // Remember the provider stop reason: Anthropic delivers it here, on + // Remember the normalized stop reason: Anthropic delivers it here, on // `message_delta`, while the terminal `message_stop` carries none of its own. if stop_reason == "refusal" && state.stop_reason.as_deref() != Some("refusal") { out.push(LlmResponseChunk::TextDelta {