Skip to content

Commit ea2a5eb

Browse files
authored
feat(server): support json_object response format (#2265)
1 parent 419febb commit ea2a5eb

4 files changed

Lines changed: 30 additions & 9 deletions

File tree

docs/openapi.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4689,6 +4689,21 @@
46894689
}
46904690
}
46914691
},
4692+
{
4693+
"type": "object",
4694+
"description": "Structured response as any JSON object",
4695+
"required": [
4696+
"type"
4697+
],
4698+
"properties": {
4699+
"type": {
4700+
"type": "string",
4701+
"enum": [
4702+
"json_object"
4703+
]
4704+
}
4705+
}
4706+
},
46924707
{
46934708
"type": "object",
46944709
"description": "Structured response following a JSON schema",

mistralrs-server-core/src/chat_completion.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ pub async fn parse_request(
10081008
Some(ResponseFormat::JsonSchema {
10091009
json_schema: JsonSchemaResponseFormat { name: _, schema },
10101010
}) => Constraint::JsonSchema(schema),
1011+
Some(ResponseFormat::JsonObject) => Constraint::JsonSchema(json!({"type": "object"})),
10111012
Some(ResponseFormat::Text) => Constraint::None,
10121013
None => Constraint::None,
10131014
},

mistralrs-server-core/src/openai.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,9 @@ pub enum ResponseFormat {
561561
/// Free-form text response
562562
#[serde(rename = "text")]
563563
Text,
564+
/// Structured response as any JSON object
565+
#[serde(rename = "json_object")]
566+
JsonObject,
564567
/// Structured response following a JSON schema
565568
#[serde(rename = "json_schema")]
566569
JsonSchema {
@@ -1848,6 +1851,16 @@ mod tests {
18481851
tool
18491852
}
18501853

1854+
#[test]
1855+
fn chat_response_format_accepts_json_object() {
1856+
let format: ResponseFormat = serde_json::from_value(json!({
1857+
"type": "json_object"
1858+
}))
1859+
.unwrap();
1860+
1861+
assert!(matches!(format, ResponseFormat::JsonObject));
1862+
}
1863+
18511864
#[test]
18521865
fn openai_tool_deserializes_chat_function_tool() {
18531866
let tool = assert_tool_roundtrips(json!({

mistralrs-server-core/src/responses.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,15 +1666,7 @@ async fn parse_openresponses_request(
16661666
schema: schema.unwrap_or(serde_json::Value::Object(Default::default())),
16671667
},
16681668
},
1669-
TextFormat::JsonObject => {
1670-
// JsonObject is treated as a schema with empty object
1671-
crate::openai::ResponseFormat::JsonSchema {
1672-
json_schema: crate::openai::JsonSchemaResponseFormat {
1673-
name: "json_object".to_string(),
1674-
schema: serde_json::json!({"type": "object"}),
1675-
},
1676-
}
1677-
}
1669+
TextFormat::JsonObject => crate::openai::ResponseFormat::JsonObject,
16781670
})
16791671
} else {
16801672
oairequest.response_format

0 commit comments

Comments
 (0)