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): preserve Codex compaction triggers (#1224)

ryan-lempka993d9c5486bf
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
index 18658bf69..ca2f42587 100644
--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs
@@ -29,7 +29,7 @@ use crate::util::{
sanitize_anthropic_tool_use_id,
};
use crate::util::{
- json_string, push_lossy, reject_responses_builtin_tool_item, stable_id, string_value,
+ json_string, push_lossy, reject_responses_native_input_item, stable_id, string_value,
validate_request_capabilities,
};
use base64::{Engine as _, engine::general_purpose::STANDARD};
@@ -937,7 +937,7 @@ fn encode_anthropic_content_with_policy(
crate::codecs::openai_media::validate_media(block, WireFormat::AnthropicMessages)?;
match block {
ContentBlock::Unknown { provider, raw } => {
- reject_responses_builtin_tool_item(provider, raw, WireFormat::AnthropicMessages)?;
+ reject_responses_native_input_item(provider, raw, WireFormat::AnthropicMessages)?;
push_lossy(
diagnostics,
policy,
diff --git a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
index 7ce8519c5..71877834e 100644
--- a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
@@ -28,7 +28,7 @@ use crate::policy::{DeterministicIdPolicy, TranslationPolicy};
use crate::util::{
capture_request_preservation, capture_response_preservation, embed_preservation,
exact_preserved_request, exact_preserved_response, json_string, object, push_lossy,
- reject_responses_builtin_tool_item, stable_id, string_value, validate_request_capabilities,
+ reject_responses_native_input_item, stable_id, string_value, validate_request_capabilities,
};
/// Format codec for OpenAI Chat Completions payloads.
@@ -1092,7 +1092,7 @@ pub(crate) fn encode_openai_content(
if role != Role::User {
for block in content {
if let ContentBlock::Unknown { provider, raw } = block {
- reject_responses_builtin_tool_item(provider, raw, WireFormat::OpenAiChat)?;
+ reject_responses_native_input_item(provider, raw, WireFormat::OpenAiChat)?;
}
}
push_lossy(
@@ -1147,7 +1147,7 @@ pub(crate) fn encode_openai_content(
));
}
ContentBlock::Unknown { provider, raw } => {
- reject_responses_builtin_tool_item(provider, raw, WireFormat::OpenAiChat)?;
+ reject_responses_native_input_item(provider, raw, WireFormat::OpenAiChat)?;
push_lossy(
diagnostics,
policy,
diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rs
index a3558c147..18b173891 100644
--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs
@@ -31,7 +31,7 @@ use crate::llm::{
use crate::policy::{DeterministicIdPolicy, TranslationPolicy};
use crate::util::{
capture_request_preservation, capture_response_preservation, embed_preservation,
- exact_preserved_request, exact_preserved_response, is_responses_builtin_tool_item, json_string,
+ exact_preserved_request, exact_preserved_response, is_responses_native_input_item, json_string,
push_lossy, stable_id, string_value, validate_request_capabilities,
};
@@ -45,11 +45,6 @@ impl FormatCodec for OpenAiResponsesCodec {
fn decode_request(&self, body: &Value, policy: &TranslationPolicy) -> Result<DecodedRequest> {
let body = crate::util::object(body, "$")?;
- // Codex marks remote-compact requests with a `compaction_trigger` input
- // item. It is codex-internal protocol that strict upstream parsers
- // reject, so drop it before preservation capture and normalization.
- let sanitized = strip_codex_compaction_markers(body);
- let body = sanitized.as_ref().unwrap_or(body);
let mut diagnostics = Vec::new();
let response_format = decode_responses_text_format(body.get("text"));
let mut request = LlmRequest {
@@ -515,22 +510,20 @@ fn decode_responses_input(
&format!("$.input[{index}].role"),
)?;
let content_value = item.get("content").unwrap_or(&Value::Null);
- if is_responses_builtin_tool_item(content_value) {
+ if is_responses_native_input_item(content_value) {
return Err(TranslationError::InvalidValue {
path: format!("$.input[{index}].content.type"),
- message:
- "built-in tool items must be top-level Responses input items"
- .to_string(),
+ message: "native Responses items must be top-level input items"
+ .to_string(),
});
}
if let Some(content_index) = content_value.as_array().and_then(|blocks| {
- blocks.iter().position(is_responses_builtin_tool_item)
+ blocks.iter().position(is_responses_native_input_item)
}) {
return Err(TranslationError::InvalidValue {
path: format!("$.input[{index}].content[{content_index}].type"),
- message:
- "built-in tool items must be top-level Responses input items"
- .to_string(),
+ message: "native Responses items must be top-level input items"
+ .to_string(),
});
}
let content = decode_responses_content(content_value);
@@ -1299,11 +1292,11 @@ fn encode_responses_input(
}
}
for message in messages {
- // The decoder carries each valid top-level built-in tool item as one
+ // The decoder carries each native top-level input item as one
// provider-qualified block, so replay that block in place.
if let [ContentBlock::Unknown { provider, raw }] = message.content.as_slice()
&& provider.as_str() == WireFormat::OpenAiResponses.as_str()
- && is_responses_builtin_tool_item(raw)
+ && is_responses_native_input_item(raw)
{
encoded.push(raw.clone());
continue;
@@ -1444,23 +1437,6 @@ fn pair_tool_calls_with_outputs(items: &mut Vec<Value>) {
}
}
-// Returns the body without Codex `compaction_trigger` input items, or `None`
-// when there are none (the common case, sparing the clone).
-fn strip_codex_compaction_markers(body: &Map<String, Value>) -> Option<Map<String, Value>> {
- fn is_marker(item: &Value) -> bool {
- item.get("type").and_then(Value::as_str) == Some("compaction_trigger")
- }
- let input = body.get("input")?.as_array()?;
- if !input.iter().any(is_marker) {
- return None;
- }
- let mut sanitized = body.clone();
- if let Some(Value::Array(items)) = sanitized.get_mut("input") {
- items.retain(|item| !is_marker(item));
- }
- Some(sanitized)
-}
-
// Encodes IR blocks that Responses represents as top-level input items.
fn encode_responses_special_input(
block: &ContentBlock,
diff --git a/crates/switchyard-translation/src/compaction.rs b/crates/switchyard-translation/src/compaction.rs
index cc51b64ed..d45bf54e6 100644
--- a/crates/switchyard-translation/src/compaction.rs
+++ b/crates/switchyard-translation/src/compaction.rs
@@ -46,18 +46,18 @@ fn is_compaction_message(message: &Message) -> bool {
for block in &message.content {
match block {
ContentBlock::Image { .. } => {}
- _ if is_compaction_summary(block) => has_summary = true,
+ _ if is_compaction_block(block) => has_summary = true,
_ => return false,
}
}
has_summary
}
-fn is_compaction_summary(block: &ContentBlock) -> bool {
+fn is_compaction_block(block: &ContentBlock) -> bool {
match block {
ContentBlock::Unknown { raw, .. } => matches!(
raw.get("type").and_then(serde_json::Value::as_str),
- Some("compaction" | "context_compaction")
+ Some("compaction" | "context_compaction" | "compaction_trigger")
),
ContentBlock::Text { text } => {
let text = text.trim().to_ascii_lowercase();
diff --git a/crates/switchyard-translation/src/util.rs b/crates/switchyard-translation/src/util.rs
index 38bec12c3..b9006045d 100644
--- a/crates/switchyard-translation/src/util.rs
+++ b/crates/switchyard-translation/src/util.rs
@@ -100,7 +100,7 @@ pub fn push_lossy(
}
// These Responses history items are valid only as top-level `input` items.
-pub(crate) fn is_responses_builtin_tool_item(item: &Value) -> bool {
+pub(crate) fn is_responses_native_input_item(item: &Value) -> bool {
matches!(
item.get("type").and_then(Value::as_str),
Some(
@@ -111,18 +111,19 @@ pub(crate) fn is_responses_builtin_tool_item(item: &Value) -> bool {
| "computer_call"
| "computer_call_output"
| "image_generation_call"
+ | "compaction_trigger"
)
)
}
-// Prevent provider-specific tool history from being downgraded to target-visible prose.
-pub(crate) fn reject_responses_builtin_tool_item(
+// Prevent provider-specific input from being downgraded to target-visible prose.
+pub(crate) fn reject_responses_native_input_item(
provider: &FormatId,
item: &Value,
target: WireFormat,
) -> Result<()> {
if provider.as_str() == WireFormat::OpenAiResponses.as_str()
- && is_responses_builtin_tool_item(item)
+ && is_responses_native_input_item(item)
{
return Err(TranslationError::UnsupportedTranslation {
from: WireFormat::OpenAiResponses.into(),
diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rs
index 8817102fd..07e3d3ec2 100644
--- a/crates/switchyard-translation/tests/request_translation.rs
+++ b/crates/switchyard-translation/tests/request_translation.rs
@@ -4193,43 +4193,32 @@ fn responses_parallel_tool_calls_pair_with_their_outputs() -> TestResult {
Ok(())
}
-// Verifies Codex `compaction_trigger` marker items never reach the upstream.
#[test]
-fn responses_codex_compaction_markers_are_stripped() -> TestResult {
+fn responses_codex_compaction_trigger_survives_round_trip() -> TestResult {
let engine = TranslationEngine::default();
- let policy = TranslationPolicy {
- preservation: switchyard_translation::PreservationPolicy::Disabled,
- ..TranslationPolicy::default()
- };
let body = json!({
"model": "gpt-5",
"input": [
{"type": "message", "role": "user", "content": "Continue"},
- {"type": "compaction_trigger"},
- {"type": "function_call", "name": "shell", "call_id": "call-a", "arguments": "{}"},
- {"type": "function_call_output", "call_id": "call-a", "output": "ok"}
- ]
+ {"type": "compaction_trigger"}
+ ],
+ "stream": true
});
-
- let output = engine
- .translate_request(
- WireFormat::OpenAiResponses,
- WireFormat::OpenAiResponses,
- &body,
- &policy,
- )?
- .body;
-
- let types = output["input"]
- .as_array()
- .ok_or("input is not an array")?
- .iter()
- .filter_map(|item| item["type"].as_str())
- .collect::<Vec<_>>();
- assert_eq!(
- types,
- vec!["message", "function_call", "function_call_output"]
- );
+ for policy in [TranslationPolicy::default(), normalized_policy()] {
+ let request = engine
+ .decode_request(WireFormat::OpenAiResponses, &body, &policy)?
+ .request;
+ assert!(request.is_compaction);
+ assert!(!request.has_compaction);
+ let output = engine.encode_request(WireFormat::OpenAiResponses, &request, &policy)?;
+ assert_eq!(output.body["input"][1], body["input"][1]);
+ for target in [WireFormat::OpenAiChat, WireFormat::AnthropicMessages] {
+ assert!(matches!(
+ engine.encode_request(target, &request, &policy),
+ Err(TranslationError::UnsupportedTranslation { .. })
+ ));
+ }
+ }
Ok(())
}
diff --git a/crates/switchyard-translation/tests/stream_translation.rs b/crates/switchyard-translation/tests/stream_translation.rs
index 2e4f3e53c..22899b029 100644
--- a/crates/switchyard-translation/tests/stream_translation.rs
+++ b/crates/switchyard-translation/tests/stream_translation.rs
@@ -152,6 +152,14 @@ fn preserved_same_format_events_replay_unknown_fields_exactly() -> TestResult {
"provider_extension": {"exact": true}
}),
),
+ (
+ WireFormat::OpenAiResponses,
+ json!({
+ "type": "response.output_item.done",
+ "output_index": 0,
+ "item": {"type": "compaction", "id": "cmp_1", "encrypted_content": "opaque"}
+ }),
+ ),
(
WireFormat::AnthropicMessages,
json!({