Skip to main content

Auto Code Fixer

Auto Code Fixer is a CLI tool that detects runtime failures and automatically fixes Python code using OpenAI.

It is designed for real projects where an entry script imports multiple local modules. The tool runs your code in an isolated sandbox + venv, installs missing external dependencies, and applies fixes only after the code executes successfully.


Installation

pip install auto-code-fixer

Quick start

auto-code-fixer path/to/main.py --project-root . --ask

If you want it to overwrite without asking:

auto-code-fixer path/to/main.py --project-root . --no-ask

What it fixes (core behavior)

✅ Local/internal imports are treated as project code

If your entry file imports something like:

from mylib import add

…and mylib.py exists inside the project, Auto Code Fixer will:

  • copy main.py + mylib.py into a sandbox
  • execute inside the sandbox
  • if the traceback points to mylib.py, it will fix mylib.py
  • then apply the fix back to your repo (with backups)

✅ External imports are auto-installed

If execution fails with:

ModuleNotFoundError: No module named 'requests'

…it will run:

pip install requests

…but only inside the sandbox venv (so your system env isn’t polluted).


Safety

Backups

Before overwriting any file, it creates a backup:

  • file.py.bak (or .bak1, .bak2, ...)

Approval mode (diff review)

auto-code-fixer path/to/main.py --project-root . --approve

In patch-protocol mode, approvals are file-by-file (apply/skip).

Diff / size guards

To prevent huge edits from being applied accidentally:

  • --max-diff-lines limits unified-diff size per file
  • --max-file-bytes limits the proposed new content size per file
  • --max-total-bytes limits total proposed new content across all files

Dry run

auto-code-fixer path/to/main.py --project-root . --dry-run

Advanced options

Fixpack mode (folder/package runner)

Fixpack runs a command (default: pytest -q) against a full-project sandbox, then iteratively fixes the failing file(s) until green or you hit --max-retries.

auto-code-fixer path/to/package --project-root . --fixpack --no-ask
# equivalent explicit command:
auto-code-fixer path/to/package --project-root . --fixpack --run "pytest -q" --no-ask

Run a custom command (pytest, etc.)

Instead of python main.py, run tests:

auto-code-fixer . --project-root . --run "pytest -q" --no-ask

When you use --run, the tool (by default) also performs a post-apply check: after copying fixes back to your project, it re-runs the same command against the real project files (using the sandbox venv for dependencies).

You can disable that extra check with:

auto-code-fixer . --project-root . --run "pytest -q" --no-post-apply-check

Model selection

export AUTO_CODE_FIXER_MODEL=gpt-4.1-mini
# or
auto-code-fixer main.py --model gpt-4.1-mini

Max retries / timeout

auto-code-fixer main.py --max-retries 8 --timeout 30

Optional AI planning (which file to edit)

auto-code-fixer main.py --ai-plan

This enables a helper that can suggest which local file to edit. It is best-effort.

Structured patch protocol (JSON + sha256) (default)

By default, Auto Code Fixer uses a structured patch protocol where the model returns strict JSON:

{ "files": [ {"path": "...", "new_content": "...", "sha256": "..."}, ... ] }

The tool verifies the SHA-256 hash of new_content before applying edits.

To disable this and use legacy full-text mode only:

auto-code-fixer main.py --legacy-mode

Ruff-first mode (best-effort)

Before calling the LLM, you can try Ruff auto-fixes (including import sorting) and Ruff formatting:

auto-code-fixer path/to/package --project-root . --fixpack --ruff-first

Optional formatting / linting (best-effort)

auto-code-fixer main.py --format black
auto-code-fixer main.py --lint ruff --fix

These run inside the sandbox venv and are skipped if the tools are not installed.

JSON report (explain)

Write a machine-readable report with attempts, files touched, and unified diffs:

auto-code-fixer main.py --explain
# or
auto-code-fixer main.py --report /tmp/report.json

Environment variables

  • OPENAI_API_KEY (required unless you always pass --api-key)
  • AUTO_CODE_FIXER_MODEL (default model)
  • AUTO_CODE_FIXER_ASK=true|false
  • AUTO_CODE_FIXER_VERBOSE=true|false

Notes

  • This tool edits code. Use it on a git repo so you can review diffs.
  • For maximum safety, run with --ask and/or --dry-run.

Release files for auto-code-fixer 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for auto-code-fixer 0.4.0
File Size Uploaded
auto_code_fixer-0.4.0.tar.gz 25.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for auto-code-fixer 0.4.0
File Interpreter ABI Platform
auto_code_fixer-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 52.8 kB

Release files / auto_code_fixer-0.4.0.tar.gz

Download URL auto_code_fixer-0.4.0.tar.gz
Size 25.9 kB
Tags Source
SHA-256 checksum
How to use checksums
87cd68dfda81e2b94ed75d5f61b6f64afc6845c4d7ce823b3c0386f6965ab24a
BLAKE2b-256 checksum
How to use checksums
e90b4160ebdaac90171c16a7fd2e06710b4c82f73a580ab5a181b56159d804ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / auto_code_fixer-0.4.0-py3-none-any.whl

Download URL auto_code_fixer-0.4.0-py3-none-any.whl
Size 26.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c352ac0e5ffe7d8318e2d7da785e32a51c11169d1cb81c23928a8cbe599a5406
BLAKE2b-256 checksum
How to use checksums
8ca30ec7bd3a8c102a8eb18037da60a07fe72950e9cab64f581095a5c531daca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page