·

Codex Exec --output-schema: JSON Reports

Prepared and checked using AI. Sources are linked in the article.

codex-cli tutorial workflow ai-coding

For Codex exec output schema structured JSON, pass a JSON Schema file through --output-schema and save the final response with --output-last-message, or -o. Add --json when you also need a stream of execution events: it controls the event format, while --output-schema controls the final report’s structure.

Define the report schema

Start with Codex CLI installed and authenticated, and run from a Git repository. codex exec reuses saved CLI authentication; running outside a repository requires --skip-git-repo-check.

For example, save this schema as report.schema.json, use report.json for the final report, and reserve events.jsonl and diagnostics.log for execution logs:

{
  "type": "object",
  "properties": {
    "summary": {
      "type": "string"
    },
    "findings": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "verification": {
      "type": "string",
      "enum": ["not_run", "passed", "failed"]
    },
    "limitations": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  },
  "required": ["summary", "findings", "verification", "limitations"],
  "additionalProperties": false
}

Keep schemas within the supported Structured Outputs subset: an object at the root, every property required, and additionalProperties: false on every object. For an optional value, keep the property required and allow null in its type. If you later replace the findings strings with objects, apply those same rules to each findings object.

The CLI takes the schema itself in this file. Use the object above rather than an API request envelope containing response_format or text.format.

Run Codex and save the final response

For a source review that should leave repository files alone:

codex exec \
  --sandbox read-only \
  --output-schema ./report.schema.json \
  --output-last-message ./report.json \
  'Review the tracked source code for likely correctness bugs. Do not edit files or run tests. Return a concise summary, findings with file paths and reasons, verification set to not_run, and limitations describing what you could not establish. Return an empty findings array if you find no supported issues.'

--sandbox read-only sets the sandbox policy for model-generated commands. The output file is written by the CLI; you do not need to ask the agent to create it.

Without --json, progress goes to stderr and the final agent message goes to stdout. Saving the final message with -o does not suppress that stdout response.

Describe the meaning of the fields in the prompt as well as their shape in the schema. Here, verification explicitly distinguishes a source review from executed tests. A schema can constrain that field to an allowed value, but structured output can still contain factual mistakes: check the evidence before acting on a finding.

Capture JSON events separately

Use this invocation instead when the surrounding automation also needs an event log:

codex exec \
  --sandbox read-only \
  --json \
  --output-schema ./report.schema.json \
  -o ./report.json \
  'Review the tracked source code for likely correctness bugs. Do not edit files or run tests. Return a concise summary, findings with file paths and reasons, verification set to not_run, and limitations describing what you could not establish. Return an empty findings array if you find no supported issues.' \
  > ./events.jsonl \
  2> ./diagnostics.log
OptionWhat it controlsConsumer
--output-schema ./report.schema.jsonStructure of the final responseReport validation
-o ./report.jsonDestination for the final responseDownstream task logic
--jsonNewline-delimited JSON events on stdoutProgress and execution logging

The event stream includes types such as thread.started, turn.started, item.completed, turn.completed, turn.failed, and error. An agent-message event contains message text inside an event object; it is not the same file format as your report.

Parse events.jsonl one event per line. Parse report.json as the final JSON document. Do not treat the last event line as the report or merge stderr into the JSONL file. For lifecycle handling, see how to detect when a headless coding agent finishes.

Gate downstream work on a usable report

Check the process return code before parsing the saved report. In your wrapper, also reject a missing or empty report, a JSON parsing failure, or a mismatch against the supplied schema.

Use a fresh output path for each job so a failed invocation cannot leave your consumer reading an earlier report. Keep process success separate from task success: a successfully generated report may describe unresolved findings, failed verification, or limitations.

For concurrent jobs, give each invocation its own report and log paths; see running multiple Codex agents in parallel.

Where Parallel Code fits

Parallel Code is our free, open-source desktop app for macOS and Linux. It runs several AI coding agents in parallel, each isolated in its own git worktree, and provides a diff-first review surface. That helps when structured reports accompany concurrent coding tasks and you also need to review the resulting changes.