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): flatten Responses JSON schema format (#377)

Bryan Bednarski2bef154970d2
diff --git a/crates/switchyard-translation/src/codecs/responses/buffered.rs b/crates/switchyard-translation/src/codecs/responses/buffered.rs
index 5d3b9e888..025dbe35f 100644
--- a/crates/switchyard-translation/src/codecs/responses/buffered.rs
+++ b/crates/switchyard-translation/src/codecs/responses/buffered.rs
@@ -161,7 +161,10 @@ impl FormatCodec for OpenAiResponsesCodec {
body.insert("max_output_tokens".to_string(), json!(max_output_tokens));
}
if let Some(response_format) = &request.output.response_format {
- body.insert("text".to_string(), json!({"format": response_format}));
+ body.insert(
+ "text".to_string(),
+ json!({"format": encode_responses_text_format(response_format)}),
+ );
}
if let Some(effort) = &request.reasoning.effort {
body.insert("reasoning".to_string(), json!({"effort": effort}));
@@ -855,6 +858,43 @@ fn decode_responses_text_format(value: Option<&Value>) -> Option<Value> {
}
}
+// Flattens Chat-compatible JSON schema fields into the Responses text format shape.
+fn encode_responses_text_format(response_format: &Value) -> Value {
+ let Some(format) = response_format.as_object() else {
+ return response_format.clone();
+ };
+ let Some(response_type) = format
+ .get("type")
+ .filter(|value| value.as_str() == Some("json_schema"))
+ else {
+ return response_format.clone();
+ };
+
+ let Some(Value::Object(json_schema)) = format.get("json_schema") else {
+ return response_format.clone();
+ };
+ if !matches!(json_schema.get("name"), Some(Value::String(_)))
+ || !matches!(json_schema.get("schema"), Some(Value::Object(_)))
+ || json_schema
+ .get("description")
+ .is_some_and(|value| !value.is_string())
+ || json_schema
+ .get("strict")
+ .is_some_and(|value| !value.is_boolean())
+ {
+ return response_format.clone();
+ }
+
+ let mut output = Map::new();
+ output.insert("type".to_string(), response_type.clone());
+ for field in ["name", "description", "schema", "strict"] {
+ if let Some(value) = json_schema.get(field) {
+ output.insert(field.to_string(), value.clone());
+ }
+ }
+ Value::Object(output)
+}
+
// Encodes normalized messages into the Responses `input` shape.
fn encode_responses_input(
messages: &[Message],
diff --git a/crates/switchyard-translation/tests/request_translation.rs b/crates/switchyard-translation/tests/request_translation.rs
index bfcf78625..71c765d01 100644
--- a/crates/switchyard-translation/tests/request_translation.rs
+++ b/crates/switchyard-translation/tests/request_translation.rs
@@ -933,6 +933,104 @@ fn responses_json_schema_text_format_maps_to_chat_response_format() -> TestResul
Ok(())
}
+// Verifies Chat response_format JSON schema fields flatten into Responses text.format.
+#[test]
+fn chat_json_schema_response_format_maps_to_responses_text_format() -> TestResult {
+ let engine = TranslationEngine::default();
+ let body = json!({
+ "model": "gpt-5",
+ "messages": [{"role": "user", "content": "Return JSON"}],
+ "response_format": {
+ "type": "json_schema",
+ "json_schema": {
+ "name": "answer",
+ "description": "A structured answer",
+ "schema": {"type": "object"},
+ "strict": true
+ }
+ }
+ });
+
+ let output = engine
+ .translate_request(
+ WireFormat::OpenAiChat,
+ WireFormat::OpenAiResponses,
+ &body,
+ &TranslationPolicy::default(),
+ )?
+ .body;
+
+ assert_eq!(
+ output["text"]["format"],
+ json!({
+ "type": "json_schema",
+ "name": "answer",
+ "description": "A structured answer",
+ "schema": {"type": "object"},
+ "strict": true
+ })
+ );
+ Ok(())
+}
+
+// Verifies nested fields cannot override the Responses format discriminator.
+#[test]
+fn chat_json_schema_response_format_ignores_nested_type_override() -> TestResult {
+ let output = translate_chat_response_format_to_responses(json!({
+ "type": "json_schema",
+ "json_schema": {
+ "type": "text",
+ "name": "answer",
+ "description": "A structured answer",
+ "schema": {"type": "object"},
+ "strict": true
+ }
+ }))?;
+
+ assert_eq!(
+ output,
+ json!({
+ "type": "json_schema",
+ "name": "answer",
+ "description": "A structured answer",
+ "schema": {"type": "object"},
+ "strict": true
+ })
+ );
+ Ok(())
+}
+
+// Verifies incomplete JSON schema wrappers remain unchanged instead of being partially flattened.
+#[test]
+fn chat_empty_json_schema_wrapper_is_preserved() -> TestResult {
+ let response_format = json!({"type": "json_schema", "json_schema": {}});
+
+ assert_eq!(
+ translate_chat_response_format_to_responses(response_format.clone())?,
+ response_format
+ );
+ Ok(())
+}
+
+fn translate_chat_response_format_to_responses(
+ response_format: Value,
+) -> std::result::Result<Value, Box<dyn std::error::Error + Send + Sync>> {
+ let body = json!({
+ "model": "gpt-5",
+ "messages": [{"role": "user", "content": "Return JSON"}],
+ "response_format": response_format
+ });
+ let output = TranslationEngine::default()
+ .translate_request(
+ WireFormat::OpenAiChat,
+ WireFormat::OpenAiResponses,
+ &body,
+ &TranslationPolicy::default(),
+ )?
+ .body;
+ Ok(output["text"]["format"].clone())
+}
+
// Verifies OpenAI system/developer/reasoning fields map to Anthropic request fields.
#[test]
fn openai_request_translates_system_developer_and_reasoning_to_anthropic() -> TestResult {