Skip to main content

switchyard_translation/codecs/responses/
mod.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! OpenAI Responses buffered and streaming codecs.
5
6mod buffered;
7mod stream;
8
9pub use buffered::OpenAiResponsesCodec;
10pub use stream::OpenAiResponsesStreamCodec;
11
12// Temperature range: 0.0..=2.0.
13// https://developers.openai.com/api/reference/resources/responses/methods/create
14pub(crate) const MAX_TEMPERATURE: f64 = 2.0;
15
16pub(crate) fn is_native_output(kind: &str) -> bool {
17    matches!(
18        kind,
19        "apply_patch_call" | "shell_call" | "computer_call" | "image_generation_call"
20    )
21}
22
23pub(crate) fn validate_response_output(
24    response: &crate::AggLlmResponse,
25    target: crate::WireFormat,
26) -> crate::Result<()> {
27    for block in response.outputs.iter().flat_map(|output| &output.content) {
28        if let crate::ContentBlock::Unknown { provider, raw } = block
29            && provider.as_str() == crate::WireFormat::OpenAiResponses.as_str()
30            && raw["type"].as_str().is_some_and(is_native_output)
31        {
32            return Err(crate::TranslationError::UnsupportedTranslation {
33                from: provider.clone(),
34                to: target.into(),
35            });
36        }
37    }
38    Ok(())
39}
40
41// Native outputs have no neutral streaming representation; inspect them before discarding raw events.
42pub(crate) fn validate_stream_output(
43    source: &crate::FormatId,
44    target: &crate::FormatId,
45    event: &serde_json::Value,
46) -> crate::Result<()> {
47    if source.as_str() == crate::WireFormat::OpenAiResponses.as_str() && source != target {
48        let mut items = event
49            .get("item")
50            .into_iter()
51            .chain(event["response"]["output"].as_array().into_iter().flatten());
52        if items.any(|item| item["type"].as_str().is_some_and(is_native_output)) {
53            return Err(crate::TranslationError::UnsupportedTranslation {
54                from: source.clone(),
55                to: target.clone(),
56            });
57        }
58    }
59    Ok(())
60}
61
62// This is a transport envelope, not encryption. Keep it distinct from native OpenAI
63// ciphertext, and retain the original text because the signature covers that text.
64const ANTHROPIC_THINKING_PREFIX: &str = "switchyard:anthropic-thinking:v1:";
65
66fn encode_anthropic_thinking(text: &str, signature: &str) -> String {
67    format!(
68        "{ANTHROPIC_THINKING_PREFIX}{}",
69        serde_json::json!([text, signature])
70    )
71}
72
73fn decode_anthropic_thinking(payload: &str) -> Option<(String, String)> {
74    serde_json::from_str(payload.strip_prefix(ANTHROPIC_THINKING_PREFIX)?).ok()
75}
76
77/// Returns true when a Responses `encrypted_content` value holds Anthropic thinking that
78/// Switchyard packed for a Responses client, rather than ciphertext from an OpenAI provider.
79/// Only Switchyard can read this value, so an OpenAI provider cannot decrypt it.
80pub fn is_anthropic_thinking_envelope(encrypted_content: &str) -> bool {
81    encrypted_content.starts_with(ANTHROPIC_THINKING_PREFIX)
82}