@@ -24,6 +24,7 @@ pub struct ChatRequestBuilder<'a> {
2424 instructions : & ' a str ,
2525 input : & ' a [ ResponseItem ] ,
2626 tools : & ' a [ Value ] ,
27+ output_schema : Option < Value > ,
2728 conversation_id : Option < String > ,
2829 session_source : Option < SessionSource > ,
2930}
@@ -40,11 +41,19 @@ impl<'a> ChatRequestBuilder<'a> {
4041 instructions,
4142 input,
4243 tools,
44+ output_schema : None ,
4345 conversation_id : None ,
4446 session_source : None ,
4547 }
4648 }
4749
50+ /// Optional output schema (JSON Schema) to enforce structured output via Chat Completions.
51+ /// This is mapped to the OpenAI `response_format: { type: "json_schema", json_schema: ... }`.
52+ pub fn output_schema ( mut self , schema : Option < Value > ) -> Self {
53+ self . output_schema = schema;
54+ self
55+ }
56+
4857 pub fn conversation_id ( mut self , id : Option < String > ) -> Self {
4958 self . conversation_id = id;
5059 self
@@ -291,13 +300,30 @@ impl<'a> ChatRequestBuilder<'a> {
291300 }
292301 }
293302
294- let payload = json ! ( {
303+ let mut payload = json ! ( {
295304 "model" : self . model,
296305 "messages" : messages,
297306 "stream" : true ,
298307 "tools" : self . tools,
299308 } ) ;
300309
310+ // If a schema was provided, request JSON Schema structured output.
311+ if let Some ( schema) = self . output_schema {
312+ if let Some ( obj) = payload. as_object_mut ( ) {
313+ obj. insert (
314+ "response_format" . to_string ( ) ,
315+ json ! ( {
316+ "type" : "json_schema" ,
317+ "json_schema" : {
318+ "name" : "codex_output_schema" ,
319+ "strict" : true ,
320+ "schema" : schema,
321+ }
322+ } ) ,
323+ ) ;
324+ }
325+ }
326+
301327 let mut headers = build_conversation_headers ( self . conversation_id ) ;
302328 if let Some ( subagent) = subagent_header ( & self . session_source ) {
303329 insert_header ( & mut headers, "x-openai-subagent" , & subagent) ;
0 commit comments