Expand description
Structured output: owned --output-schema files and typed decoding.
codex exec, exec resume and exec review take a JSON Schema as a file
path (--output-schema <path>). OutputSchema serializes a
serde_json::Value to a private temporary file and removes it on drop,
so a caller can build the schema in code:
use codex_wrapper::structured::OutputSchema;
use codex_wrapper::{Codex, ExecCommand};
#[derive(serde::Deserialize)]
struct Person {
name: String,
}
let codex = Codex::builder().build()?;
let schema = OutputSchema::new(&serde_json::json!({
"type": "object",
"properties": {"name": {"type": "string"}},
"required": ["name"],
"additionalProperties": false
}))?;
let result = ExecCommand::new("Extract the name from: Hi, I'm Ada Lovelace.")
.output_schema(schema.path().to_string_lossy())
.execute_json(&codex)
.await?;
let person: Person = result.decode_structured()?;
// `schema` is dropped here, after the process has exited.The file lives exactly as long as the OutputSchema value, so keep it in
scope until the awaited call or the stream that uses it has finished. The
builders only store the path; building or formatting argv never creates a
file.
§Decoding
Codex enforces the schema natively. This crate does not re-validate the
decoded value against it; decoding into a caller-chosen
DeserializeOwned type is the caller-side check.
QueryResult::decode_structured
reads the final agent message, the last completed agent_message item
after the last turn.started, and applies these rules in order:
- The last terminal turn event is
turn.failed:StructuredOutputError::TurnFailed, even when an agent message was produced before the failure. - No
turn.completedevent:StructuredOutputError::NoTerminal. - No final agent message, or its text is empty or
null:StructuredOutputError::Missing. - The text is not JSON:
StructuredOutputError::InvalidJson. - The JSON is neither an object nor an array:
StructuredOutputError::NotObjectOrArray. - It does not deserialize into
T:StructuredOutputError::Decode.
Process failures (a non-zero exit, unparseable JSONL, timeouts,
cancellation) are reported by the execution call as crate::Error
before any decoding happens. Decoding never starts a process.
Structs§
- Output
Schema - A JSON Schema written to a private temporary file for
--output-schema.
Enums§
- Structured
Output Error - Why a run’s structured output could not be decoded.