Skip to main content

Self-healing Python — catches runtime errors, fixes code & dependencies via LLM + MCP

Project description

🔧 pfix

PyPI version Python 3.10+ License: Apache-2.0

Self-healing Python — catches runtime errors and fixes source code + dependencies via LLM + MCP.

💡 New in 0.1.5: Zero-configuration mode! Just import pfix with PFIX_AUTO_APPLY=true in .env and any exception triggers automatic repair.

Features

  • Zero-config modeimport pfix + .env = auto-healing for entire project
  • @pfix decorator — wrap any function; errors trigger automatic repair
  • Fast dep fixModuleNotFoundError → instant pip/uv install (no LLM call)
  • pipreqs scanning — project-wide import analysis for missing dependencies
  • LLM code repair — sends error context to LLM (OpenRouter/LiteLLM) for intelligent fixes
  • pip + uv — auto-detects uv for faster installs, falls back to pip
  • MCP server@mcp.tool() via FastMCP for IDE integration (Claude Code, Cursor, VS Code)
  • Git auto-commit — optional auto-commit of fixes with configurable prefix
  • Auto-restartos.execv process restart after fix applied
  • Interactive diff — unified diff with confirmation before applying
  • Backup system — timestamped backups in .pfix_backups/ (can be disabled)
  • Async support@apfix for async functions

Installation

pip install pfix

# With MCP server support
pip install pfix[mcp]

# With git auto-commit
pip install pfix[git]

# Everything
pip install pfix[all]

Quick Start

# 1. Configure
cp .env.example .env
# Set OPENROUTER_API_KEY=sk-or-v1-...

# 2. Use in your code
from pfix import pfix

@pfix
def process_data(path):
    import pandas as pd                    # auto-installed if missing
    df = pd.read_csv(path)
    return df.groupby("category").sum()    # LLM fixes column errors

@pfix(retries=3, hint="Parses ISO dates from API", deps=["requests", "python-dateutil"])
def fetch_events(url: str):
    import requests
    from dateutil.parser import parse
    return [parse(e["ts"]) for e in requests.get(url).json()["events"]]

CLI

pfix run script.py              # Run with global exception hook
pfix run script.py --auto       # Auto-apply fixes
pfix run script.py --restart    # Restart process after fix
pfix check                      # Show config status
pfix deps scan                  # Scan for missing deps (pipreqs)
pfix deps install               # Install all missing deps
pfix deps generate              # Generate requirements.txt
pfix server                     # Start MCP server (stdio)
pfix server --http 3001         # Start MCP server (HTTP)

MCP Integration

pfix exposes tools via FastMCP for IDE integration:

Tool Description
pfix_analyze Analyze error → diagnosis + fix proposal
pfix_fix Analyze + apply fix (with backup)
pfix_deps_scan Scan for missing deps
pfix_deps_install Install a package
pfix_deps_generate Generate requirements.txt
pfix_edit_file Write file content

Claude Code / VS Code setup

Add to your MCP config (.claude/mcp.json or VS Code settings):

{
  "mcpServers": {
    "pfix": {
      "command": "python",
      "args": ["-m", "pfix.mcp_server"]
    }
  }
}

Configuration

.env

Variable Default Description
OPENROUTER_API_KEY Required
PFIX_MODEL openrouter/anthropic/claude-sonnet-4 LLM model
PFIX_AUTO_APPLY false Auto-apply fixes
PFIX_AUTO_INSTALL_DEPS true Auto pip/uv install
PFIX_AUTO_RESTART false Restart after fix
PFIX_PKG_MANAGER auto pip or uv
PFIX_MAX_RETRIES 3 Max attempts
PFIX_DRY_RUN false Show only
PFIX_GIT_COMMIT false Auto-commit fixes
PFIX_GIT_PREFIX pfix: Commit prefix

pyproject.toml

[tool.pfix]
model = "openrouter/anthropic/claude-sonnet-4"
auto_apply = false
max_retries = 3

Programmatic

from pfix import configure

configure(
    auto_apply=True,
    pkg_manager="uv",
    git_auto_commit=True,
)

How It Works

@pfix decorated function
        │
        ▼
   Exception caught
        │
        ├── ModuleNotFoundError? → pip/uv install → retry
        │
        ▼
   ErrorContext built (traceback, source, vars, imports, pipreqs scan)
        │
        ▼
   LLM analysis (LiteLLM → OpenRouter)
        │
        ▼
   FixProposal (diagnosis, code, deps, confidence)
        │
        ├── Show diff → confirm (or auto-apply)
        ├── Create .pfix_backups/ backup
        ├── Apply fix to source
        ├── Git commit (optional)
        ▼
   Reload module → retry (or os.execv restart)

Dependencies

Package Role
litellm LLM proxy — OpenRouter, OpenAI, Anthropic, Ollama
python-dotenv Load .env config
rich Terminal UI (diffs, panels, tables)
pipreqs Project import scanning
pathspec Gitignore-aware file filtering
mcp FastMCP server (optional)
gitpython Git auto-commit (optional)
watchdog File change watching (optional)

License

Licensed under Apache-2.0.

Licensed under Apache-2.0.

Licensed under Apache-2.0.

Licensed under Apache-2.0.

Licensed under Apache-2.0.

Licensed under Apache-2.0.

Licensed under Apache-2.0.

Apache 2.0 — Tom Sapletta / Softreck

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pfix-0.1.7.tar.gz (32.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pfix-0.1.7-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file pfix-0.1.7.tar.gz.

File metadata

  • Download URL: pfix-0.1.7.tar.gz
  • Upload date:
  • Size: 32.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pfix-0.1.7.tar.gz
Algorithm Hash digest
SHA256 8bc1c044ef4a7790df3a1e98fd29246d2ce271e62774b5263fff3ebe8423b8aa
MD5 006bd37ce7c395279fc25debe12d4f75
BLAKE2b-256 206b9308d2cee641fb1b7ec6f269306949634726168265cf2e8258e099a54b69

See more details on using hashes here.

File details

Details for the file pfix-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: pfix-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pfix-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 4873420e51d3688ac47a645be481ed9bb4174025824b9484ac184dab9749c88e
MD5 35ed771c55eb079fed14c343c8cfc987
BLAKE2b-256 d58ad92d33abb8f0d1e7f8186fd647d3d5e034f1a594bd8caa3e571acd6bd8b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page