codex - 💡(How to fix) Fix codex exec --output-schema does not apply only to final output [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openai/codex#19816Fetched 2026-04-28 06:36:37
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Author
Timeline (top)
labeled ×3commented ×1unlabeled ×1

Root Cause

When an agent run emits intermediate progress messages before tool calls, those intermediate agent_message items are also forced into the provided output schema. This can break workflows that consume the first schema-valid JSON message as the final result, because intermediate messages can look like valid final outputs.

Code Example

{
  "type": "object",
  "properties": {
    "final": {
      "type": "string"
    }
  },
  "required": [
    "final"
  ],
  "additionalProperties": false
}

---

codex exec --json --output-schema schema.json \
  "Strictly follow these three steps: First, say in one sentence that you will run pwd, then run pwd. Second, say in one sentence that you will run ls -1 | head -5, then run ls -1 | head -5. Third, say in one sentence that you will run git status --short, then run git status --short. Finally, summarize the results of all three commands. You must output the one-sentence explanation before running each command."

---

{"type":"item.completed","item":{"id":"item_0","type":"agent_message","text":"{\"final\": \"I will run `pwd` to confirm the current working directory.\"}"}}

---

{"type":"item.completed","item":{"id":"item_2","type":"agent_message","text":"{\"final\":\"I will run `ls -1 | head -5` to list the first five directory entries.\"}"}}

---

{"type":"item.completed","item":{"id":"item_4","type":"agent_message","text":"{\"final\":\"I will run `git status --short` to check the concise worktree status.\"}"}}

---

{"type":"item.completed","item":{"id":"item_6","type":"agent_message","text":"{\"final\":\"Results: `pwd` returned `/home/sunday/Projects/codex`. `ls -1 | head -5` returned `AGENTS.md`, `BUILD.bazel`, `CHANGELOG.md`, `LICENSE`, and `MODULE.bazel`. `git status --short` shows one untracked file: `schema.json`.\"}"}}
RAW_BUFFERClick to expand / collapse

What version of Codex CLI is running?

v0.125.0

What subscription do you have?

Pro

Which model were you using?

gpt-5.5

What platform is your computer?

Linux

What terminal emulator and version are you using (if applicable)?

No response

What issue are you seeing?

codex exec --output-schema does not appear to apply only to the final output.

When an agent run emits intermediate progress messages before tool calls, those intermediate agent_message items are also forced into the provided output schema. This can break workflows that consume the first schema-valid JSON message as the final result, because intermediate messages can look like valid final outputs.

In my case, this means automated workflows using --json --output-schema can intermittently fail or consume the wrong result depending on whether the model emits progress messages during the run.

What steps can reproduce the bug?

Create schema.json:

{
  "type": "object",
  "properties": {
    "final": {
      "type": "string"
    }
  },
  "required": [
    "final"
  ],
  "additionalProperties": false
}

Run:

codex exec --json --output-schema schema.json \
  "Strictly follow these three steps: First, say in one sentence that you will run pwd, then run pwd. Second, say in one sentence that you will run ls -1 | head -5, then run ls -1 | head -5. Third, say in one sentence that you will run git status --short, then run git status --short. Finally, summarize the results of all three commands. You must output the one-sentence explanation before running each command."

Actual output includes intermediate messages that already satisfy the final schema:

{"type":"item.completed","item":{"id":"item_0","type":"agent_message","text":"{\"final\": \"I will run `pwd` to confirm the current working directory.\"}"}}
{"type":"item.completed","item":{"id":"item_2","type":"agent_message","text":"{\"final\":\"I will run `ls -1 | head -5` to list the first five directory entries.\"}"}}
{"type":"item.completed","item":{"id":"item_4","type":"agent_message","text":"{\"final\":\"I will run `git status --short` to check the concise worktree status.\"}"}}

Only the last message is actually the final result:

{"type":"item.completed","item":{"id":"item_6","type":"agent_message","text":"{\"final\":\"Results: `pwd` returned `/home/sunday/Projects/codex`. `ls -1 | head -5` returned `AGENTS.md`, `BUILD.bazel`, `CHANGELOG.md`, `LICENSE`, and `MODULE.bazel`. `git status --short` shows one untracked file: `schema.json`.\"}"}}

What is the expected behavior?

--output-schema should constrain only the final output of codex exec, not intermediate progress messages in the agent loop.

Intermediate agent_message items should remain normal progress messages, and only the final returned result should be schema-compliant.

Additional information

From a quick source check, final_output_json_schema appears to be copied into each prompt built during the sampling loop, so every Responses API request in the turn may include text.format.schema.

Related issue: #15451 discusses schema/tool behavior, but this report is specifically about intermediate agent messages being shaped as final schema output.

extent analysis

TL;DR

The --output-schema flag should be modified to only apply to the final output of codex exec, rather than intermediate progress messages.

Guidance

  • Verify that the issue is indeed caused by the --output-schema flag being applied to intermediate messages by checking the final_output_json_schema variable in the source code.
  • Check the source code to see if the final_output_json_schema is being copied into each prompt built during the sampling loop, which could be causing the issue.
  • Consider modifying the codex exec command to only apply the --output-schema flag to the final output, possibly by adding a new flag or option to distinguish between intermediate and final outputs.
  • Test the modified command with the provided example to ensure that only the final output is schema-compliant.

Example

No code snippet is provided as the issue is more related to the behavior of the codex exec command and its flags.

Notes

The issue seems to be specific to the codex exec command and its interaction with the --output-schema flag. The provided example and source code check can help verify the issue and guide the modification of the command.

Recommendation

Apply a workaround by modifying the codex exec command to only apply the --output-schema flag to the final output, as the current behavior is causing issues with automated workflows.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

codex - 💡(How to fix) Fix codex exec --output-schema does not apply only to final output [1 comments, 2 participants]