Automatically fix Python code using ChatGPT
Project description
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.pyinto a sandbox - execute inside the sandbox
- if the traceback points to
mylib.py, it will fixmylib.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-lineslimits unified-diff size per file--max-file-byteslimits the proposed new content size per file--max-total-byteslimits total proposed new content across all files
Dry run
auto-code-fixer path/to/main.py --project-root . --dry-run
Advanced options
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
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.
Environment variables
OPENAI_API_KEY(required unless you always pass--api-key)AUTO_CODE_FIXER_MODEL(default model)AUTO_CODE_FIXER_ASK=true|falseAUTO_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
--askand/or--dry-run.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file auto_code_fixer-0.3.7.tar.gz.
File metadata
- Download URL: auto_code_fixer-0.3.7.tar.gz
- Upload date:
- Size: 21.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c482c57dfbd4dac4d8d8230a10773090921718fe74ed8b5ef22168dd7476de13
|
|
| MD5 |
0d006212c886f5d1d876fd65c6ba89f5
|
|
| BLAKE2b-256 |
b6dd843c1e78dfda79196c92a7db97912a3ca727f778815c89a76d4596773990
|
File details
Details for the file auto_code_fixer-0.3.7-py3-none-any.whl.
File metadata
- Download URL: auto_code_fixer-0.3.7-py3-none-any.whl
- Upload date:
- Size: 22.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bebf2bcd457d0975266cec89da73babc39b06e6d36fa215648d9a80f63591511
|
|
| MD5 |
dea6863557958add689f94b4b58659cc
|
|
| BLAKE2b-256 |
0208fcf247492bd815b0457e252354707fe255b9a42c6462618fe91cc05d8b71
|