Skip to main content

AI-powered structured file editor with diff visualization

Project description

PatchAI โœจ

AI-powered structured file editor with beautiful diff visualization

PatchAI lets you edit JSON, YAML, and config files using natural language instructions. Perfect for fixing OCR errors, cleaning up data, or making bulk changes across structured documents.

โœจ Features

  • Natural Language Editing - Just describe what you want to change
  • Visual Diff - See exactly what changed with beautiful unified or side-by-side diffs
  • Image-Aware - Compare JSON/YAML with original document images to fix OCR errors
  • Undo/Reset - Full history tracking with easy undo
  • Rich CLI - Beautiful terminal interface with colors and panels
  • Simple API - 3 lines of code to get started

๐Ÿš€ Quick Start

Installation

# Basic installation (no LLM provider)
pip install patchai

# With specific provider
pip install patchai[gemini]     # Google Gemini
pip install patchai[openai]     # OpenAI GPT
pip install patchai[anthropic]  # Anthropic Claude

# With all providers
pip install patchai[all-providers]

# With Jupyter support
pip install patchai[jupyter,gemini]

Basic Usage

Python API:

from patchai import PatchAI

# Using Gemini (default)
editor = PatchAI("data.json", provider="gemini", api_key="your-key")
editor.edit("Fix all typos and remove extra newlines")
editor.save("fixed.json")

# Using OpenAI
editor = PatchAI("data.json", provider="openai", api_key="your-key", model="gpt-4o")
editor.edit("Fix all typos")
editor.save("fixed.json")

# Using Anthropic Claude
editor = PatchAI("data.json", provider="anthropic", api_key="your-key", model="claude-sonnet-4-20250514")
editor.edit("Fix all typos")
editor.save("fixed.json")

Command Line:

# Gemini (default)
export GEMINI_API_KEY="your-key"
patchai data.json -e "Fix all typos"

# OpenAI
export OPENAI_API_KEY="your-key"
patchai data.json --provider openai --model gpt-4o -e "Fix all typos"

# Anthropic
export ANTHROPIC_API_KEY="your-key"
patchai data.json --provider anthropic --model claude-sonnet-4-20250514 -e "Fix all typos"

๐Ÿ“– Examples

Fix OCR Errors with Image Reference

from patchai import PatchAI

# Load JSON with reference image
editor = PatchAI(
    data="extracted.json",
    image_path="original_document.jpg"
)

# AI compares JSON with image and fixes errors
editor.edit("Compare the JSON with the document image and correct any OCR errors")

# Show what changed
from patchai import print_diff
print_diff(editor.get_original(), editor.get_current())

# Save
editor.save("corrected.json")

Edit YAML Config

from patchai import PatchAI

editor = PatchAI("config.yaml", format="yaml")
editor.edit("Update database host to localhost and change port to 5432")
editor.save("config.yaml")

Interactive Editing Session

from patchai import PatchAI

editor = PatchAI("data.json")

# Make multiple edits
editor.edit("Remove all empty strings")
editor.edit("Add newlines after periods in the text field")
editor.edit("Fix capitalization in headers")

# Undo last change if needed
editor.undo()

# Check if anything changed
if editor.has_changes:
    editor.save("cleaned.json")

Batch Processing

from patchai import PatchAI
from pathlib import Path

# Process multiple files
files = Path("data/").glob("*.json")

for file in files:
    editor = PatchAI(file)
    editor.edit("Standardize date format to YYYY-MM-DD")
    editor.save(file)
    print(f"โœ“ Processed {file}")

๐ŸŽจ CLI Examples

Interactive Mode

patchai data.json

You'll see:

โ”Œโ”€ PatchAI Interactive Mode โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                    โ”‚
โ”‚ Commands:                                          โ”‚
โ”‚   โ€ข Type instruction to edit                       โ”‚
โ”‚   โ€ข 'undo' - undo last edit                       โ”‚
โ”‚   โ€ข 'reset' - reset to original                   โ”‚
โ”‚   โ€ข 'save' - save and exit                        โ”‚
โ”‚   โ€ข 'quit' - exit without saving                  โ”‚
โ”‚   โ€ข 'diff' - show current changes                 โ”‚
โ”‚                                                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Instruction: Fix all typos in the header

Single Edit Mode

# Basic edit
patchai data.json -e "Clean up formatting"

# With image reference
patchai extracted.json \
  --image document.jpg \
  -e "Compare with image and fix OCR errors"

# YAML file
patchai config.yaml \
  --format yaml \
  -e "Update production database credentials"

# Save to new file
patchai data.json \
  -e "Remove all null values" \
  -o cleaned.json

# Side-by-side diff
patchai data.json \
  -e "Fix all dates" \
  --diff side-by-side

๐Ÿ”ง Advanced Usage

Custom Model

from patchai import PatchAI

editor = PatchAI(
    "data.json",
    model="gemini-2.0-flash",  # or gemini-2.0-flash-thinking-exp
    api_key="your-api-key"     # or set GEMINI_API_KEY env var
)

Programmatic Diff Analysis

from patchai import PatchAI, print_diff, print_summary, generate_unified_diff

editor = PatchAI("data.json")
original = editor.get_original()

editor.edit("Your instruction here")
modified = editor.get_current()

# Print colored diff
print_diff(original, modified, style="unified")

# Print summary of changes
print_summary(original, modified)

# Get diff as string
diff_text = generate_unified_diff(original, modified)

History Tracking

editor = PatchAI("data.json")

# Make multiple edits
editor.edit("Fix typos")
editor.edit("Remove newlines")
editor.edit("Update dates")

# Access edit history
for i, edit in enumerate(editor.edit_history):
    print(f"{i+1}. {edit['instruction']}")

# Undo multiple times
editor.undo()  # undo "Update dates"
editor.undo()  # undo "Remove newlines"

# Reset to original
editor.reset()

๐ŸŽฏ Use Cases

๐Ÿ“„ Document Processing

  • Fix OCR extraction errors by comparing JSON output with original document images
  • Clean up formatting in extracted data
  • Standardize field values across documents

โš™๏ธ Configuration Management

  • Update config files across multiple environments
  • Fix formatting issues in YAML/JSON configs
  • Bulk update values (URLs, API keys, ports)

๐Ÿ”ง Data Cleaning

  • Remove null/empty values
  • Standardize date formats
  • Fix typos across structured data
  • Normalize field values

๐Ÿ“Š Data Transformation

  • Restructure nested JSON
  • Move values between fields
  • Split/merge fields based on natural language instructions

๐Ÿงช Jupyter Notebook Support

from patchai import PatchAI

editor = PatchAI("data.json", image_path="doc.jpg")

# Edit
editor.edit("Fix all OCR errors")

# Beautiful diff in notebook
from patchai import print_diff
print_diff(editor.get_original(), editor.get_current(), style="side-by-side")

๐Ÿ’ฌ Interactive Jupyter Editor (Widget UI)

PatchAI also includes a fully interactive Jupyter/Colab editor โ€” a beautiful, two-panel interface that lets you edit JSON or YAML files using natural language instructions, with live diffs and undo support.

from patchai.jupyter import create_jupyter_editor

# Launch interactive editor
create_jupyter_editor("data.json", image_path="document.jpg")

๐Ÿ›ก๏ธ API Reference

PatchAI

PatchAI(
    data: Union[Dict, str, Path],
    format: str = "json",
    image_path: Optional[Union[str, Path]] = None,
    api_key: Optional[str] = None,
    model: str = "gemini-2.0-flash-exp"
)

Parameters:

  • data: Input data as dict, JSON/YAML string, or file path
  • format: File format ("json" or "yaml")
  • image_path: Optional reference image for visual comparison
  • api_key: Gemini API key (or set GEMINI_API_KEY env var)
  • model: Gemini model to use

Methods:

  • edit(instruction: str, include_image: bool = True) -> Dict - Apply edit instruction
  • undo() -> bool - Undo last edit
  • reset() - Reset to original data
  • save(path: Union[str, Path]) - Save to file
  • get_current() -> Dict - Get current data
  • get_original() -> Dict - Get original data

Properties:

  • has_changes: bool - Check if data has been modified
  • history: List[Dict] - Edit history
  • edit_history: List[Dict] - Instruction history

๐Ÿ” Environment Variables

# Set your Gemini API key
export GEMINI_API_KEY="your-api-key-here"

Get your free API key at: https://ai.google.dev

๐Ÿ“ฆ Project Structure

patchai/
โ”œโ”€โ”€ src/patchai/
โ”‚   โ”œโ”€โ”€ __init__.py      # Main exports
โ”‚   โ”œโ”€โ”€ editor.py        # Core PatchAI class
โ”‚   โ”œโ”€โ”€ diff.py          # Diff utilities
โ”‚   โ””โ”€โ”€ cli.py           # Command-line interface
โ”‚   โ””โ”€โ”€ jupyter.py        
โ”œโ”€โ”€ tests/               # Unit tests
โ”œโ”€โ”€ examples/            # Example scripts
โ”œโ”€โ”€ pyproject.toml       # Package configuration
โ””โ”€โ”€ README.md

๐Ÿค Contributing

Contributions welcome! Ideas for improvement:

  • Support for more formats (TOML, XML, CSV)
  • Web UI for non-technical users
  • Batch operation commands
  • Custom validation rules
  • PDF form field editing
  • Excel/DOCX simple edits

๐Ÿ“„ License

MIT License - see LICENSE file

๐Ÿ™ Acknowledgments

Built with:

๐Ÿ’ก Tips

  1. Be specific: "Fix typos in the 'description' field" works better than "fix everything"
  2. Use image reference: When fixing OCR errors, always include the original image
  3. Check diffs: Always review changes before saving
  4. Start small: Test on a copy before editing important files

๐Ÿ“š More Examples

Check out the examples/ directory for:

  • OCR error correction workflow
  • Batch config file updates
  • Data cleaning pipelines
  • YAML configuration management

๐Ÿ”— Links


Made with โค๏ธ by developers who hate manual data cleaning

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

patchai-0.1.5.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

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

patchai-0.1.5-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file patchai-0.1.5.tar.gz.

File metadata

  • Download URL: patchai-0.1.5.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for patchai-0.1.5.tar.gz
Algorithm Hash digest
SHA256 8b304032af68fc86e9175519dcccd8c3160574fafee8e63af06fd07e1ed5bdd6
MD5 f38ec657bc17182dc61c5dbd4b1af27a
BLAKE2b-256 36906ea216e6aa17a9f0e1bc97b227f8fdb43de7eb85921273596216f7c31d89

See more details on using hashes here.

File details

Details for the file patchai-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: patchai-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for patchai-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3e766466726a2e8a79931df7a236a3cec13bfbdcad900ed1398798c0036c9acb
MD5 30ec0cd19fd3003addff3f1aaa8886b8
BLAKE2b-256 0535aeb1a42db8f9f8641f78e6dbe3d20e3d860efd787fcd8eebd3d179623f89

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