How to configure Codex with a Python type checker
Give Codex the same type-check command that developers and CI use. Put the command in AGENTS.md, package the procedure as a skill, return failures through a bounded Stop hook, and keep a Git hook as the final gate.
This guide uses Pyrefly, the handbook’s recommendation for new projects. Keep mypy, ty, or Pyright when the project already uses it.
Prerequisites
- Codex CLI and uv installed
- A Git repository with its type checker installed
- A passing type-check baseline, or a configured scope that excludes existing errors
Confirm the project command
Run the checker from the repository root before adding it to Codex:
uv run pyrefly checkuv run executes the command inside the project’s virtual environment, ensuring all dependencies are installed first. A passing project reports that Pyrefly found no errors. Configure gradual adoption before continuing if the command reports unrelated existing errors; otherwise every Codex turn will return the same failures.
Record the command in AGENTS.md
Add a focused rule to the repository’s AGENTS.md:
## Type checking
- Run `uv run pyrefly check` after changing Python code or signatures.
- Fix errors caused by the current change, then rerun the command.
- Report existing errors or blockers rather than suppressing them to pass.Codex reads AGENTS.md when a session starts. Restart the session after changing the file, then use the AGENTS.md verification procedure to confirm that Codex loaded it.
Give Codex a reusable skill
Create .agents/skills/type-check/SKILL.md:
---
name: type-check
description: Run the repository's Python type checker after changing Python code or signatures and before reporting completion.
---
From the repository root, run `uv run pyrefly check`.
Fix errors caused by the current change and run the command again. Report any
remaining errors or blockers instead of weakening the checker configuration or
adding suppressions only to make the command pass.Codex uses the name and description to decide when to load the skill. Invoke it directly with $type-check when the task is primarily about types. The AGENTS.md rule keeps the check visible for ordinary Python tasks, but neither file guarantees that the command runs.
Return failures through a bounded Stop hook
Create .codex/hooks/check_types.py:
from __future__ import annotations
import json
import subprocess
import sys
CHECK_COMMAND = ["uv", "run", "pyrefly", "check"]
def main() -> None:
event = json.load(sys.stdin)
if event.get("stop_hook_active"):
return
root = subprocess.run(
["git", "rev-parse", "--show-toplevel"],
cwd=event["cwd"],
check=True,
capture_output=True,
text=True,
).stdout.strip()
result = subprocess.run(
CHECK_COMMAND,
cwd=root,
capture_output=True,
text=True,
)
if result.returncode == 0:
return
output = "\n".join(
part.strip() for part in (result.stdout, result.stderr) if part.strip()
)
reason = (
"Type checking failed. Fix errors caused by the current task, rerun "
f"{' '.join(CHECK_COMMAND)}, and report any remaining blocker.\n\n{output}"
)
print(json.dumps({"decision": "block", "reason": reason}))
if __name__ == "__main__":
main()Inside the script, git rev-parse keeps the checker anchored to the repository root. On failure, the script returns the diagnostics as a new conversation turn. stop_hook_active lets Codex make one corrective attempt without creating an endless loop.
Register the script in .codex/hooks.json:
{
"description": "Run the Python type checker when a Codex turn stops.",
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "uv run --no-project python -c \"import runpy, subprocess; root = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'], text=True).strip(); runpy.run_path(root + '/.codex/hooks/check_types.py', run_name='__main__')\"",
"timeout": 60,
"statusMessage": "Checking Python types"
}
]
}
]
}
}The launcher asks Git for the repository root before loading the script, so the hook also works when Codex starts in a subdirectory. --no-project prevents project discovery and synchronization for this launcher. The script still runs Pyrefly through the project’s uv run command.
Start a new Codex session and open /hooks. Review the project hook, confirm that the command resolves to this repository, and trust it. Codex skips new or changed project hooks until they pass this review.
When Pyrefly fails at the end of a turn, Codex receives the diagnostics as a continuation prompt and gets one more pass. A passing check lets the turn finish without another prompt.
Keep prek as the commit-time gate
The Stop hook gives Codex feedback, but it does not prove that the final tree passes. Install prek and its Git hook:
uv add --dev prek
uv run prek installThe --dev flag marks prek as a development dependencyA package needed during development (testing, linting, formatting) that is not shipped to users of your project. Installed with the --dev flag.
, keeping it out of production installs. Expect pyproject.toml and uv.lock to include prek and Git to use the installed hook.
Add this local hook to .pre-commit-config.yaml:
repos:
- repo: local
hooks:
- id: pyrefly
name: Pyrefly
entry: uv run pyrefly check
language: system
pass_filenames: falseVerify the gate against the whole configured project:
uv run prek run pyrefly --all-filesThe Pyrefly row ends with Passed.
Keep the same command in CI. Codex’s hook supplies one corrective turn; prek blocks failing commits, and a required CI check can block merges.
Preserve the project’s existing checker
Replace the Pyrefly command in AGENTS.md, SKILL.md, CHECK_COMMAND, and .pre-commit-config.yaml when the repository already uses another checker:
| Checker | CHECK_COMMAND value |
Hook entry |
|---|---|---|
| mypy | ["uv", "run", "mypy", "."] |
uv run mypy . |
| ty | ["uv", "run", "ty", "check"] |
uv run ty check |
| Pyright | ["uv", "run", "pyright"] |
uv run pyright |
Do not migrate checkers as part of configuring Codex. A stable baseline matters more than which supported checker supplies the diagnostics.