From 974c2b24d07adf869e1c3298e7186cff57284121 Mon Sep 17 00:00:00 2001 From: Travis Addair Date: Wed, 16 Oct 2024 12:48:12 -0700 Subject: [PATCH] Added backwards compatible field to OpenAI json_object API (#648) --- router/src/lib.rs | 4 ++++ router/src/server.rs | 21 ++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/router/src/lib.rs b/router/src/lib.rs index f8bdb8f3f..0a64ec6ce 100644 --- a/router/src/lib.rs +++ b/router/src/lib.rs @@ -523,6 +523,10 @@ struct OpenAiResponseFormat { #[serde(rename(deserialize = "type"))] response_format_type: ResponseFormatType, json_schema: Option, + + // For backwards compatibility + #[serde(default = "default_json_schema")] + schema: Option, } #[derive(Clone, Deserialize, ToSchema, Serialize, Debug, PartialEq)] diff --git a/router/src/server.rs b/router/src/server.rs index 7a2480c3d..b0f1d7f7d 100644 --- a/router/src/server.rs +++ b/router/src/server.rs @@ -272,11 +272,22 @@ async fn chat_completions_v1( // Ignore when type is text ResponseFormatType::Text => None, - // For json_object, use the fixed schema - ResponseFormatType::JsonObject => Some(ResponseFormat { - r#type: response_format_type.clone(), - schema: default_json_schema(), - }), + // For json_object, use the fixed schema. + // For backwards compatibility, also support non-standard `schema` field + ResponseFormatType::JsonObject => openai_format.schema.map_or_else( + || { + Some(ResponseFormat { + r#type: response_format_type.clone(), + schema: default_json_schema(), + }) + }, + |schema_value: serde_json::Value| { + Some(ResponseFormat { + r#type: response_format_type.clone(), + schema: Some(schema_value), + }) + }, + ), // For json_schema, use schema_value if available, otherwise fallback to the fixed schema ResponseFormatType::JsonSchema => openai_format