Skip to content

Commit 3f40332

Browse files
feat(core): support output_schema on chat wire
1 parent fb3414f commit 3f40332

5 files changed

Lines changed: 39 additions & 16 deletions

File tree

‎codex-rs/codex-api/src/endpoint/chat.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl<T: HttpTransport, A: AuthProvider> ChatClient<T, A> {
6161

6262
let request =
6363
ChatRequestBuilder::new(model, &prompt.instructions, &prompt.input, &prompt.tools)
64+
.output_schema(prompt.output_schema.clone())
6465
.conversation_id(conversation_id)
6566
.session_source(session_source)
6667
.build(self.streaming.provider())?;

‎codex-rs/codex-api/src/requests/chat.rs‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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);

‎codex-rs/core/src/client.rs‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,6 @@ impl ModelClient {
190190
/// This path is only used when the provider is configured with
191191
/// `WireApi::Chat`; it does not support `output_schema` today.
192192
async fn stream_chat_completions(&self, prompt: &Prompt) -> Result<ApiResponseStream> {
193-
if prompt.output_schema.is_some() {
194-
return Err(CodexErr::UnsupportedOperation(
195-
"output_schema is not supported for Chat Completions API".to_string(),
196-
));
197-
}
198-
199193
let auth_manager = self.auth_manager.clone();
200194
let model_info = self.get_model_info();
201195
let instructions = prompt.get_full_instructions(&model_info).into_owned();

‎codex-rs/core/src/model_provider_info.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ pub fn built_in_model_providers() -> HashMap<String, ModelProviderInfo> {
285285
env_key: None,
286286
env_key_instructions: None,
287287
experimental_bearer_token: None,
288+
// Copilot's gpt-4.1 models are served via Chat Completions; keep the default as Chat.
289+
// Note: Structured output (output_schema) is not supported via Chat Completions.
288290
wire_api: WireApi::Chat,
289291
query_params: None,
290292
http_headers: Some(copilot_headers),

‎sdk/native/package.json‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codex-native/sdk",
3-
"version": "0.0.30",
3+
"version": "0.0.31",
44
"description": "Native NAPI-based Codex SDK - complete standalone implementation.",
55
"main": "dist/index.cjs",
66
"module": "dist/index.mjs",
@@ -47,14 +47,14 @@
4747
]
4848
},
4949
"optionalDependencies": {
50-
"@codex-native/sdk-darwin-arm64": "0.0.30",
51-
"@codex-native/sdk-darwin-x64": "0.0.30",
52-
"@codex-native/sdk-linux-arm64-gnu": "0.0.30",
53-
"@codex-native/sdk-linux-x64-gnu": "0.0.30",
54-
"@codex-native/sdk-linux-arm64-musl": "0.0.30",
55-
"@codex-native/sdk-linux-x64-musl": "0.0.30",
56-
"@codex-native/sdk-win32-arm64-msvc": "0.0.30",
57-
"@codex-native/sdk-win32-x64-msvc": "0.0.30"
50+
"@codex-native/sdk-darwin-arm64": "0.0.31",
51+
"@codex-native/sdk-darwin-x64": "0.0.31",
52+
"@codex-native/sdk-linux-arm64-gnu": "0.0.31",
53+
"@codex-native/sdk-linux-x64-gnu": "0.0.31",
54+
"@codex-native/sdk-linux-arm64-musl": "0.0.31",
55+
"@codex-native/sdk-linux-x64-musl": "0.0.31",
56+
"@codex-native/sdk-win32-arm64-msvc": "0.0.31",
57+
"@codex-native/sdk-win32-x64-msvc": "0.0.31"
5858
},
5959
"dependencies": {
6060
"@modelcontextprotocol/sdk": "^1.22.0",

0 commit comments

Comments
 (0)