# Codex CLI: Stdout or JSONL? Build a Contract Gate

> Source: <https://dev.to/bestbee/codex-cli-stdout-or-jsonl-build-a-contract-gate-1g4c>
> Published: 2026-12-05 10:39:36+00:00

**Recommendation:** When wrapping an agent CLI in CI, capture stdout and stderr separately, fail on nonzero exit or zero-byte stdout, and treat any unparseable JSONL line as a failed job.
More in the playbook "Running AI in production": <https://wpnews.pro/playbooks/production-ops>

A job that exits green with an empty payload is the cheapest failure to ship. If your CI or internal automation shells out to an agent CLI, the interesting question is not which model it calls. It is which stream you decided to trust—and whether that decision survives a redirect, a timeout, and a version bump.

This is a decision guide for that choice using one tool: OpenAI's Codex CLI in non-interactive mode. Two documented output contracts exist. A team can pick one by accident, often by merging streams when debugging.

*Illustrative scenario, not a real deployment:* a nightly job uses the CLI to draft release notes, then a second job posts the file. If the wrapper merges progress and final output, the poster may ingest progress text as if it were the notes. The cited vendor page supports the split; it does not report this scenario.

Per the vendor's [non-interactive mode documentation](https://learn.chatgpt.com/docs/non-interactive-mode), in default output mode `codex exec` writes progress to stderr and sends the final agent message to stdout. The same page says that adding `--json` changes stdout to a JSON Lines event stream, where each line is a JSON object.

Be precise about the scope. The documentation states the stream split and the JSONL shape. It does **not** enumerate event types, field names, exit codes, terminal event rules, or what partial output looks like after a timeout or kill. Those are version-specific observations, not contractual guarantees. Also, the cited page says `--json` changes **stdout**; it does not leave the JSONL destination as an open question.

JSON Lines is not the same thing as a resumable event stream. MDN's [Server-Sent Events documentation](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) notes that an event ID does not by itself retain or replay events. Nothing in the Codex page promises replay, so do not build a consumer that assumes it can resume a JSONL stream from the last ID.

`--json` stdout is the event channel. In default mode stderr is diagnostics and should not reach the consumer.
If the answers are 'no human,' 'yes consumer,' and 'yes pinned,' either contract can work. Choose by how much structure the consumer needs, not by which looks more modern.

Treat stdout as the payload, stderr as diagnostics, and the process exit status as the first gate. The second gate is payload presence, because an empty payload is not a result.

The wrapper should preserve stdout and stderr as separate channels, capture the exit status, and fail the job when the status is nonzero or stdout contains zero bytes. Why the double gate? A consumer that checks only the exit status can forward a zero-byte file downstream. A consumer that checks only for a non-empty file can forward a partial one after a kill. The documentation does not specify partial-output behavior, so treat truncation as a risk to test, not a documented outcome.

**Fit:** human-readable deliverables, diffable artifacts, and anything a person will read before it matters. **Misfit:** consumers that need one JSON object per line, or pipelines where two people will argue about what counts as 'the final message.'

With `--json`, the cited page says stdout becomes a sequence of JSON Lines records: each line is a JSON object. The contract work moves from 'is the file non-empty' to 'did the stream arrive in a form my parser can accept.'

Before writing a parser, run one sample against your installed version and inspect stdout and stderr separately. That is a proposed check, not a result reported here. The parser should ignore records it does not recognize and should fail the job when a line is not valid JSON. A truncated final line is invalid: if a timeout or kill leaves half an object, a JSON parser should error, and the wrapper must convert that error into a failed job rather than an empty artifact. Zero lines should also fail if your workflow expects at least one event; verify whether zero lines can ever be a legitimate result for your prompt and version.

Do not assume there is a documented terminal event. The page does not specify one. If your consumer needs a terminal marker, verify it against the installed version and pin that version.

**Fit:** you need structured records, multiple consumers will read the stream, or you want to build parsing once rather than scrape prose. **Misfit:** you cannot pin the version and cannot absorb changes in record shape.

These rows are design comparisons, not vendor guarantees.

|  | Contract A (default split) | Contract B ( `--json` ) | 
|---|---|---|
| Payload | Final agent message on stdout | JSON object per line on stdout | 
| Success check | Exit status plus non-empty stdout | Exit status plus every emitted line parses as a JSON object | 
| Progress visibility | Progress on stderr by default | Event stream on stdout; stderr content not specified by the cited page | 
| Version sensitivity | Fewer documented fields to depend on | Depends on record semantics not specified by the cited page | 
| Best for | One human-facing artifact | Multiple programmatic consumers | 

These are proposed checks for a human to run against the installed CLI. They were not executed for this draft, and no results are reported here.

If a person runs the command in a terminal and reads merged output, there is no contract to defend. Stop there. The same is true for a throwaway local script whose output nobody reads. The gate matters once output crosses a boundary: into a file another job reads, into a queue, or into something a customer sees.

Write four lines and revisit them when the version changes:

Whichever way you decide, the question that should reverse it is narrow: would a truncated or empty payload currently reach the next stage without failing the job? If yes, the contract is not the problem—the gate is.

*Disclosure: This article was created with AI assistance. The Codex CLI behavior described is limited to the linked vendor documentation; general shell semantics and engineering recommendations are labeled as such. No commands were executed for this draft, and no CLI results were observed. All checks above are proposed observation-based tests for a human to run against the installed version. Event types, exit codes, partial-output behavior, and failure-path output were not verified here; verify them against your installed CLI and the current vendor documentation before relying on a parser.*
