A useful AI review should answer a specific question: does this staged patch introduce a concrete defect? It should also produce a report you can inspect, validate, and associate with the exact code that was reviewed.
A paragraph in a terminal is difficult to use reliably in an automated workflow. A structured report gives your application explicit fields for review status, file paths, severity, and explanations.
This tutorial uses Codex CLI with a configured GPT model route to review a snapshot of staged Git changes. It requests JSON output, validates the result locally, and checks whether the repository state changed while the review was running.
The result is an advisory report. It does not commit code, apply patches, or replace your tests.
1. Define exactly what is being reviewed
Git distinguishes between your working files, the staging area, and the last commit.
That distinction matters when using AI review.
Suppose you stage a bug fix and then continue experimenting in the same file. A review that reads the working file may inspect changes that are not part of your intended commit.
A review of the staged snapshot should use the content in Git’s index.
The Git diff documentation explains these comparison modes. In this tutorial, the script records the current commit and creates a tree object representing the index. It then compares those two snapshots.
This gives the review a stable input even if you later edit a working file.
Run the example in a repository with an existing commit. Avoid changing the index while the initial snapshot is being captured.
2. Prepare an existing Codex connection
The tutorial assumes that:
- Codex CLI is already installed and authenticated.
- Your configured provider supports the selected GPT model.
- The route supports the features required by your Codex version.
- Git and Python are available.
- You have a small text patch staged for review.
For installation and provider configuration, use the existing Codex CLI and API setup guide.
For an API relay, verify the actual Codex-compatible route. A gateway that accepts a basic chat request may still differ in Responses API behavior or structured output support.
The script reuses your configured Codex provider. Setting the model environment variable below does not change that provider or its authentication configuration.
Start with a non-sensitive example patch when verifying a new route. Once the connection and output contract work, move to an appropriate project.
3. Give the report an explicit scope
The report contains three top-level fields:
| Field | Meaning |
|---|---|
status | Whether the supplied patch could be reviewed with the available context |
summary | A brief description of the outcome and limits |
findings | Candidate defects for a developer to inspect |
Each finding includes a file path, an optional line number, a severity, and an explanation.
The status can be reviewed or needs_context.
This distinction prevents an important failure: treating insufficient information as a clean review.
A patch may change a function call without including the called function’s contract. The model should describe the missing context instead of inventing that contract.
An empty findings list is not proof that the patch contains no defects. It means no findings were returned for this particular review.
4. Install the local validator
The workflow uses jsonschema to check the returned report:
Set the requested model identifier:
Use the exact identifier supported by your configured route.
The environment variable is separate from the API key. Credentials should remain in the provider configuration or credential mechanism you already use.
Before sending real source code through a gateway, confirm that the selected service and account settings are appropriate for that repository.
5. Create the review script
Save the following as review_staged.py.
The example accepts a bounded UTF-8 text patch. Its 200,000-byte input limit is a tutorial policy, not a published model limit.
Run it from your repository:
The script creates review artifacts in a separate directory. It does not stage additional files or modify your working source.
git write-tree records the index as a Git tree object; it does not create a commit.
6. Understand the Codex output options
The example uses codex exec for a non-interactive task.
The --output-schema option requests a final response with the specified structure. The -o option writes that final response to a file.
These options are documented in the official Codex non-interactive mode guide.
This differs from requesting a JSON event stream. An event stream can contain progress, tool activity, and lifecycle events. It is not automatically the final review object your application expects.
Keep the report file separate from diagnostic logs. Parsing a mixture of progress text and JSON is fragile and can cause downstream failures.
The temporary output directory also prevents a report from a previous run from being mistaken for a new result.
7. Know what local validation proves
The validator checks that the report has the expected fields and types.
The additional Python checks confirm that finding paths belong to the patch and that supplied line numbers are positive.
They do not establish that a reported bug is real.
A model can produce valid JSON with an incorrect explanation. It can cite a valid file path but misunderstand the effect of a changed line.
For each candidate finding, check:
- The behavior before and after the change.
- The input or state required to trigger the problem.
- Whether the explanation matches the code.
- Whether surrounding code already handles the condition.
- What test or reproduction supports the claim.
The jsonschema documentation describes structural validation. Semantic review remains a separate responsibility.
Do not automatically turn a valid schema into a merge approval.
8. Reject stale reviews
A review is useful only for the code it examined.
The script records the base commit and staged tree. After Codex finishes, it compares them with the current repository state.
If you staged additional changes or moved to another commit during the review, the result is marked stale.
That does not make earlier findings worthless. They may still help explain a defect. It means the report cannot be treated as a review of the current staged change.
A production integration should preserve this relationship when displaying findings. Show the reviewed revision or snapshot identifier.
If another process will act later, recheck the identity at that point. A check performed when the model finishes cannot prevent changes made afterward.
9. Handle incomplete and failed reviews separately
There are several distinct outcomes:
| Outcome | Recommended handling |
|---|---|
| Codex process fails | Preserve diagnostics and investigate |
| Output is not valid JSON | Treat the run as invalid |
| JSON violates the schema | Reject it from downstream automation |
| Report needs more context | Provide targeted context and review again |
| Repository state changed | Review the new snapshot |
| Valid report with findings | Inspect each candidate defect |
| Valid report without findings | Continue normal tests and human review |
Do not replace failed states with an empty findings list.
Doing so makes service failures look identical to successful reviews that returned no concerns.
For rate-limit failures, use the existing Codex 429 troubleshooting guide. Keep retry diagnosis separate from review interpretation.
A useful reporting dashboard should count invalid or incomplete runs separately from completed reviews.
10. Keep the initial review narrowly scoped
The example runs from a temporary directory and asks Codex to use only the supplied patch.
Its read-only sandbox policy is useful, but it should not be described as complete process isolation. A prompt also does not replace control over the environment in which an agent runs.
For unfamiliar repositories, use an appropriately isolated review environment and avoid exposing unrelated credentials or files.
Start with advisory reports that a developer reads. Once you have evaluated the workflow on representative patches, decide whether particular conditions should require another reviewer.
Avoid automatically blocking every commit based on an unverified model severity label.
A measured rollout gives you time to identify false positives, recurring context gaps, and failures in the reporting logic.
11. Evaluate review quality with known examples
Build a small evaluation set from changes your team understands.
Include patches with known defects, clean changes, incomplete context, and cases where a suspicious line is actually protected elsewhere.
For each review, record whether the model identified the relevant issue and whether the explanation was actionable.
A finding such as “this might fail” gives a developer less value than a specific description of the triggering input and incorrect behavior.
Track false positives as well as detected defects. Repeated incorrect warnings can reduce trust and make developers ignore useful findings.
Keep the patch, prompt, schema, model identifier, and route configuration stable when comparing runs.
12. Connect the workflow through an API gateway
An API gateway can provide the configured model access layer when it supports the required Codex protocol and model features.
For 4SAPI, verify the current Codex integration instructions, available GPT routes, and credential settings before running the review.
Keep the prompt, schema, and patch constant when evaluating a route change. That makes differences easier to interpret.
Record invalid reports, missed known defects, and false positives. A faster response is not necessarily more useful if it creates additional review work.
The relevant outcome is a report that helps a developer decide what to do with a specific patch.
Frequently asked questions
Does this review unstaged changes?
No. The supplied patch comes from a recorded index snapshot compared with the recorded base commit.
Does the script run tests?
No. It requests patch analysis and tells the model not to claim that tests were executed.
Does valid JSON mean the review is correct?
No. Schema validation establishes structure. Developers still need to verify the findings.
Why stop when the staging area changes?
The report belongs to the earlier snapshot. A different staged change needs its own review identity.
Create your first structured review
Open the 4SAPI console, register or sign in, and confirm a GPT route compatible with your Codex setup.
Start with one small staged patch whose intended behavior you understand. Generate the report, inspect its findings, and compare them with your tests and manual review.
Keep the patch, metadata, and JSON report together. This gives you a repeatable workflow to evaluate before expanding it across a team.




