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(responses): preserve streamed response IDs for conversation continuation (#869)

Graham King15684f957633
diff --git a/crates/switchyard-translation/src/codecs/responses/stream.rs b/crates/switchyard-translation/src/codecs/responses/stream.rs
index a179d4ee7..9a76fb0e0 100644
--- a/crates/switchyard-translation/src/codecs/responses/stream.rs
+++ b/crates/switchyard-translation/src/codecs/responses/stream.rs
@@ -1148,14 +1148,9 @@ fn fnv1a_64(text: &str) -> u64 {
})
}
-// Converts any upstream message ID into a Responses-looking response ID.
+// Keep the response ID aligned with buffered responses and stored conversation history.
fn responses_id(state: &StreamTranslationState) -> String {
- let Some(id) = target_message_id_or_source_message_id(state) else {
- return "resp_switchyard".to_string();
- };
- if id.starts_with("resp_") {
- id.to_string()
- } else {
- format!("resp_{id}")
- }
+ target_message_id_or_source_message_id(state)
+ .unwrap_or("resp_switchyard")
+ .to_string()
}
diff --git a/crates/switchyard-translation/tests/stream_translation.rs b/crates/switchyard-translation/tests/stream_translation.rs
index 01e9cc0da..e2cd468c5 100644
--- a/crates/switchyard-translation/tests/stream_translation.rs
+++ b/crates/switchyard-translation/tests/stream_translation.rs
@@ -828,6 +828,22 @@ fn openai_chat_to_responses_uses_served_model_without_losing_source_model() -> T
Ok(())
}
+// Stored conversation history is keyed by the upstream ID, so clients must see it unchanged.
+#[test]
+fn openai_chat_to_responses_keeps_the_upstream_id() -> TestResult {
+ let mut state =
+ StreamTranslationState::new(WireFormat::OpenAiChat, WireFormat::OpenAiResponses);
+ let events = TranslationEngine::default().translate_event(
+ &mut state,
+ WireFormat::OpenAiChat,
+ WireFormat::OpenAiResponses,
+ &json!({"id": "chatcmpl-test", "choices": [{"index": 0, "delta": {"content": "hi"}}]}),
+ )?;
+
+ assert_eq!(events[0]["response"]["id"], "chatcmpl-test");
+ Ok(())
+}
+
// Verifies OpenAI Chat finish emits a terminal chunk when the source closes without one.
#[test]
fn openai_chat_finish_synthesizes_terminal_chunk_after_incomplete_source() -> TestResult {