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): omit text blocks for absent Chat response content (#776)

Graham King5a50ef211a63
diff --git a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
index a864814a6..65727a16c 100644
--- a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
@@ -287,13 +287,17 @@ impl FormatCodec for OpenAiChatCodec {
.and_then(Value::as_object)
.cloned()
.unwrap_or_default();
- let mut content = decode_openai_content(
- message.get("content").unwrap_or(&Value::Null),
- WireFormat::OpenAiChat,
- &mut Vec::new(),
- &TranslationPolicy::default(),
- "$.choices[0].message.content",
- )?;
+ let mut content = match message.get("content") {
+ // decode_openai_content would turn absent response text into an empty text block.
+ None | Some(Value::Null) => Vec::new(),
+ Some(content) => decode_openai_content(
+ content,
+ WireFormat::OpenAiChat,
+ &mut Vec::new(),
+ &TranslationPolicy::default(),
+ "$.choices[0].message.content",
+ )?,
+ };
prepend_openai_reasoning_blocks(&mut content, &message);
if let Some(tool_calls) = message.get("tool_calls").and_then(Value::as_array) {
for (index, tool_call) in tool_calls.iter().enumerate() {
diff --git a/crates/switchyard-translation/tests/response_translation.rs b/crates/switchyard-translation/tests/response_translation.rs
index 86276fcd7..43a7ee602 100644
--- a/crates/switchyard-translation/tests/response_translation.rs
+++ b/crates/switchyard-translation/tests/response_translation.rs
@@ -559,6 +559,47 @@ fn openai_reasoning_response_translates_to_responses_reasoning_item() -> TestRes
Ok(())
}
+#[test]
+fn openai_reasoning_response_preserves_visible_content_presence() -> TestResult {
+ let engine = TranslationEngine::default();
+ for content in [json!(null), json!(""), json!("Answer")] {
+ let mut body = json!({
+ "id": "chatcmpl-test",
+ "model": "gpt-reasoning",
+ "choices": [{
+ "index": 0,
+ "message": {
+ "role": "assistant",
+ "reasoning_content": "private reasoning"
+ },
+ "finish_reason": "length"
+ }]
+ });
+ let mut expected = vec![json!({
+ "type": "thinking",
+ "thinking": "private reasoning",
+ "signature": ""
+ })];
+ if let Some(text) = content.as_str() {
+ expected.push(json!({"type": "text", "text": text}));
+ }
+ body["choices"][0]["message"]["content"] = content;
+
+ let output = engine
+ .translate_response(
+ WireFormat::OpenAiChat,
+ WireFormat::AnthropicMessages,
+ &body,
+ &normalized_policy(),
+ )?
+ .body;
+
+ assert_eq!(output["content"], json!(expected), "input: {body}");
+ assert_eq!(output["stop_reason"], "max_tokens");
+ }
+ Ok(())
+}
+
#[test]
fn openai_chat_response_round_trips_reasoning_details() -> TestResult {
let engine = TranslationEngine::default();