diff --git a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs index f3a816cca..c7bd1358a 100644 --- a/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs +++ b/crates/switchyard-translation/src/codecs/openai_chat/buffered.rs @@ -398,9 +398,41 @@ impl FormatCodec for OpenAiChatCodec { }); } let output = response.first_output(); - let content = output - .map(|output| text_from_blocks(&output.content, "")) + let mut content = String::new(); + let mut refusal = None::; + let mut url_citations = output + .map(|output| output.url_citations.clone()) .unwrap_or_default(); + let mut content_offset = 0; + if let Some(output) = output { + for block in &output.content { + match block { + ContentBlock::Text { text } => { + content.push_str(text); + content_offset += text.chars().count(); + } + ContentBlock::Refusal { text } => { + refusal.get_or_insert_with(String::new).push_str(text); + let refusal_len = text.chars().count(); + // IR offsets include refusal text; Chat content excludes it. + for citation in &mut url_citations { + citation.start_index -= refusal_len + .min(citation.start_index.saturating_sub(content_offset)); + citation.end_index -= + refusal_len.min(citation.end_index.saturating_sub(content_offset)); + } + url_citations.retain(|citation| citation.start_index < citation.end_index); + } + ContentBlock::Unknown { raw, .. } => { + if let Some(text) = raw.as_str() { + content.push_str(text); + content_offset += text.chars().count(); + } + } + _ => {} + } + } + } let tool_calls = output .map(|output| { output @@ -422,16 +454,18 @@ impl FormatCodec for OpenAiChatCodec { .unwrap_or_default(); let mut message = json!({ "role": "assistant", - "content": if content.is_empty() && !tool_calls.is_empty() { + "content": if content.is_empty() && (!tool_calls.is_empty() || refusal.is_some()) { Value::Null } else { Value::String(content) }, }); - if let Some(output) = output.filter(|output| !output.url_citations.is_empty()) { + if let Some(refusal) = refusal { + message["refusal"] = Value::String(refusal); + } + if !url_citations.is_empty() { message["annotations"] = Value::Array( - output - .url_citations + url_citations .iter() .map(|citation| json!({"type": "url_citation", "url_citation": citation})) .collect(), diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rs index d920f38d3..aff7d8acb 100644 --- a/crates/switchyard-translation/src/codecs/responses/buffered.rs +++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs @@ -1802,12 +1802,23 @@ fn decode_responses_output_item( return Ok(None); }; match item.get("type").and_then(Value::as_str) { - Some("message") => Ok(Some(ResponseOutput { - url_citations: decode_responses_url_citations(item.get("content")), - role: role_from_responses(item.get("role").and_then(Value::as_str)), - content: decode_responses_content(item.get("content").unwrap_or(&Value::Null)), - stop_reason: Some(StopReason::EndTurn), - })), + Some("message") => { + let content = decode_responses_content(item.get("content").unwrap_or(&Value::Null)); + let stop_reason = if content + .iter() + .any(|block| matches!(block, ContentBlock::Refusal { .. })) + { + StopReason::ContentFilter + } else { + StopReason::EndTurn + }; + Ok(Some(ResponseOutput { + url_citations: decode_responses_url_citations(item.get("content")), + role: role_from_responses(item.get("role").and_then(Value::as_str)), + content, + stop_reason: Some(stop_reason), + })) + } Some("function_call") => Ok(Some(ResponseOutput { url_citations: Vec::new(), role: Role::Assistant, @@ -1919,7 +1930,43 @@ fn encode_responses_output(outputs: &[ResponseOutput]) -> Value { if provider.as_str() == WireFormat::OpenAiResponses.as_str() && raw["type"].as_str().is_some_and(super::is_native_output)) }); - let text = text_from_blocks(&output.content, ""); + let mut content = Vec::new(); + let mut text_offset = 0; + for block in &output.content { + match block { + ContentBlock::Text { text } => { + let text_end = text_offset + text.chars().count(); + let annotations = output + .url_citations + .iter() + .filter(|citation| { + citation.start_index >= text_offset + && citation.end_index <= text_end + }) + .map(|citation| { + json!({ + "type": "url_citation", + "start_index": citation.start_index - text_offset, + "end_index": citation.end_index - text_offset, + "url": citation.url, + "title": citation.title, + }) + }) + .collect::>(); + content.push(json!({ + "type": "output_text", + "text": text, + "annotations": annotations, + })); + text_offset = text_end; + } + ContentBlock::Refusal { text } => { + content.push(json!({"type": "refusal", "refusal": text})); + text_offset += text.chars().count(); + } + _ => {} + } + } let reasoning = reasoning_text_from_blocks(&output.content, "\n"); let status = if matches!(output.stop_reason, Some(StopReason::MaxTokens)) { "incomplete" @@ -1974,23 +2021,16 @@ fn encode_responses_output(outputs: &[ResponseOutput]) -> Value { )); } - if !text.is_empty() || (!has_output_items && reasoning.is_empty()) { + if content.is_empty() && !has_output_items && reasoning.is_empty() { + content.push(json!({"type": "output_text", "text": "", "annotations": []})); + } + if !content.is_empty() { items.push(json!({ "type": "message", "id": "msg_switchyard", "status": status, "role": role_to_responses(output.role), - "content": [{ - "type": "output_text", - "text": text, - "annotations": output.url_citations.iter().map(|citation| json!({ - "type": "url_citation", - "start_index": citation.start_index, - "end_index": citation.end_index, - "url": citation.url, - "title": citation.title, - })).collect::>(), - }], + "content": content, })); } diff --git a/crates/switchyard-translation/tests/response_translation.rs b/crates/switchyard-translation/tests/response_translation.rs index d11910fd6..ae7837883 100644 --- a/crates/switchyard-translation/tests/response_translation.rs +++ b/crates/switchyard-translation/tests/response_translation.rs @@ -295,11 +295,12 @@ fn openai_chat_response_translates_to_anthropic_message() -> TestResult { Ok(()) } -// Verifies a Chat Completions refusal survives both neutral decoding and Anthropic encoding. #[test] -fn openai_chat_refusal_decodes_and_translates_to_anthropic() -> TestResult { +fn buffered_refusals_survive_translation() -> TestResult { let engine = TranslationEngine::default(); - let body = json!({ + let policy = normalized_policy(); + let refusal = "I cannot help with that request."; + let chat = json!({ "id": "chatcmpl-refusal", "model": "gpt-4o", "choices": [{ @@ -307,40 +308,77 @@ fn openai_chat_refusal_decodes_and_translates_to_anthropic() -> TestResult { "message": { "role": "assistant", "content": null, - "refusal": "I cannot help with that request." + "refusal": refusal }, "finish_reason": "stop" }], "usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15} }); - - let decoded = - engine.decode_response(WireFormat::OpenAiChat, &body, &TranslationPolicy::default())?; - let output = decoded.response.first_output().ok_or("missing output")?; - assert_eq!( - output.content, - vec![ContentBlock::Refusal { - text: "I cannot help with that request.".to_string() + let responses = json!({ + "id": "resp_refusal", + "model": "gpt-4o", + "status": "completed", + "output": [{ + "type": "message", + "role": "assistant", + "status": "completed", + "content": [{"type": "refusal", "refusal": refusal}] }] - ); - assert_eq!(output.stop_reason, Some(StopReason::ContentFilter)); + }); - let translated = engine - .encode_response( - WireFormat::AnthropicMessages, - &decoded.response, - &TranslationPolicy::default(), - )? - .body; - assert_eq!( - translated["content"], - json!([{"type": "text", "text": "I cannot help with that request."}]) - ); - assert_eq!(translated["stop_reason"], "refusal"); - assert_eq!( - translated["stop_details"], - json!({"type": "refusal", "category": null, "explanation": null}) - ); + for (source, body) in [ + (WireFormat::OpenAiChat, chat), + (WireFormat::OpenAiResponses, responses), + ] { + let decoded = engine.decode_response(source, &body, &policy)?.response; + let output = decoded.first_output().ok_or("missing output")?; + assert_eq!( + output.content, + vec![ContentBlock::Refusal { + text: refusal.to_string() + }], + "{source:?} decoded content" + ); + assert_eq!( + output.stop_reason, + Some(StopReason::ContentFilter), + "{source:?} decoded stop reason" + ); + + let chat = engine + .encode_response(WireFormat::OpenAiChat, &decoded, &policy)? + .body; + assert_eq!( + chat["choices"][0]["message"], + json!({"role": "assistant", "content": null, "refusal": refusal}), + "{source:?} to Chat" + ); + assert_eq!(chat["choices"][0]["finish_reason"], "content_filter"); + + let responses = engine + .encode_response(WireFormat::OpenAiResponses, &decoded, &policy)? + .body; + assert_eq!( + responses["output"][0]["content"], + json!([{"type": "refusal", "refusal": refusal}]), + "{source:?} to Responses" + ); + assert_eq!(responses["status"], "completed"); + + let anthropic = engine + .encode_response(WireFormat::AnthropicMessages, &decoded, &policy)? + .body; + assert_eq!( + anthropic["content"], + json!([{"type": "text", "text": refusal}]), + "{source:?} to Anthropic" + ); + assert_eq!(anthropic["stop_reason"], "refusal"); + assert_eq!( + anthropic["stop_details"], + json!({"type": "refusal", "category": null, "explanation": null}) + ); + } Ok(()) }