Manual invoice processing is slow, error-prone, and expensive. In 2026, AI-powered automation has matured, making it easier than ever to extract data, validate, and process invoices at scale. This step-by-step guide will show you how to automate invoice processing with AI in 2026 using state-of-the-art tools and open-source models. You'll learn to set up an end-to-end workflow that ingests invoices (PDFs or images), extracts key fields, validates data, and integrates with your accounting system.
For a broader perspective on how invoice automation fits into finance, see our Best AI Workflow Automation Tools for Financial Teams in 2026 overview.
Prerequisites
- Basic knowledge of Python (3.10+ recommended)
- Familiarity with REST APIs and JSON
- Docker (v25+) installed and running
- Access to a cloud AI OCR API (e.g., Google Document AI, Azure Form Recognizer, or open-source alternatives like
docTRorPaddleOCR) - Sample invoices in PDF or image format
- Optional: Access to an accounting system API (e.g., Xero, QuickBooks, SAP)
1. Set Up Your Project Environment
-
Create a project folder and initialize a virtual environment:
mkdir ai-invoice-automation cd ai-invoice-automation python3 -m venv venv source venv/bin/activate -
Install required Python packages:
pip install doctr[torch] pydantic fastapi uvicorn requests python-dotenvNote:
doctr[torch]is a fast, open-source OCR and document understanding library. You can swap this for a cloud OCR SDK if preferred. -
Set up a basic project structure:
tree . . ├── main.py ├── requirements.txt ├── .env ├── invoices/ └── processed/
2. Ingest Invoices and Run AI OCR
-
Place sample invoices in the
invoices/directory. -
Write Python code to perform OCR on each invoice.
Below is a script using
doctrto extract raw text from invoices:import os from doctr.io import DocumentFile from doctr.models import ocr_predictor INVOICE_DIR = 'invoices' OUTPUT_DIR = 'processed' os.makedirs(OUTPUT_DIR, exist_ok=True) model = ocr_predictor(pretrained=True) def extract_text(invoice_path): doc = DocumentFile.from_images(invoice_path) result = model(doc) return result.export() for filename in os.listdir(INVOICE_DIR): if filename.lower().endswith(('.pdf', '.png', '.jpg', '.jpeg')): path = os.path.join(INVOICE_DIR, filename) data = extract_text(path) with open(os.path.join(OUTPUT_DIR, filename + '.json'), 'w') as f: import json json.dump(data, f, indent=2) print("OCR extraction complete.")Screenshot description: Terminal output showing "OCR extraction complete." and a
processed/directory containing JSON files for each invoice. -
Test the OCR step:
python main.pyInspect the JSON files in
processed/to confirm text extraction.
3. Extract Key Invoice Fields Using AI
-
Define the fields you want to extract:
- Invoice Number
- Invoice Date
- Vendor Name
- Total Amount
- Due Date
-
Use a prompt-based LLM (e.g., OpenAI GPT-4, open-source LLM) to extract fields from OCR output.
Here's a function using the OpenAI API (replace
YOUR_OPENAI_KEYwith your key, or use an open-source LLM endpoint):import openai import os openai.api_key = os.getenv("OPENAI_API_KEY") def extract_fields_with_llm(ocr_text): prompt = f""" Extract the following fields from this invoice text: - Invoice Number - Invoice Date - Vendor Name - Total Amount - Due Date Text: {ocr_text} Respond in JSON with field names as keys. """ response = openai.ChatCompletion.create( model="gpt-4", messages=[{"role": "user", "content": prompt}], max_tokens=300, temperature=0 ) return response.choices[0].message['content']Tip: For open-source or self-hosted LLMs, adapt the API call as needed.
-
Combine OCR and LLM extraction:
import json for filename in os.listdir(OUTPUT_DIR): if filename.endswith('.json'): with open(os.path.join(OUTPUT_DIR, filename)) as f: ocr_data = json.load(f) text = "\n".join([block['value'] for page in ocr_data['pages'] for block in page['blocks']]) fields_json = extract_fields_with_llm(text) with open(os.path.join(OUTPUT_DIR, filename.replace('.json', '_fields.json')), 'w') as out_f: out_f.write(fields_json) print("Field extraction complete.")Screenshot description: Directory
processed/now contains_fields.jsonfiles with structured invoice data.
4. Validate and Clean Extracted Data
-
Define a Pydantic model to validate extracted fields:
from pydantic import BaseModel, ValidationError from datetime import date class InvoiceFields(BaseModel): invoice_number: str invoice_date: date vendor_name: str total_amount: float due_date: date -
Validate and clean the data:
for filename in os.listdir(OUTPUT_DIR): if filename.endswith('_fields.json'): with open(os.path.join(OUTPUT_DIR, filename)) as f: fields = json.load(f) try: invoice = InvoiceFields(**fields) print(f"Valid invoice: {invoice}") except ValidationError as e: print(f"Validation error in {filename}: {e}")Screenshot description: Terminal output showing "Valid invoice: ..." or validation error messages for problematic files.
5. Integrate with Your Accounting System
-
Set up API credentials for your accounting software (e.g., Xero, QuickBooks).
Store API keys in your
.envfile:ACCOUNTING_API_KEY=your_api_key_here -
Write a function to send validated invoice data to your accounting system:
import requests from dotenv import load_dotenv load_dotenv() ACCOUNTING_API_KEY = os.getenv("ACCOUNTING_API_KEY") def send_to_accounting(invoice: InvoiceFields): url = "https://api.youraccounting.com/v1/invoices" headers = {"Authorization": f"Bearer {ACCOUNTING_API_KEY}"} payload = invoice.dict() response = requests.post(url, headers=headers, json=payload) if response.status_code == 201: print(f"Invoice {invoice.invoice_number} uploaded successfully.") else: print(f"Failed to upload invoice: {response.text}") -
Automate the entire workflow:
for filename in os.listdir(OUTPUT_DIR): if filename.endswith('_fields.json'): with open(os.path.join(OUTPUT_DIR, filename)) as f: fields = json.load(f) try: invoice = InvoiceFields(**fields) send_to_accounting(invoice) except ValidationError as e: print(f"Validation error in {filename}: {e}")Screenshot description: Terminal showing successful uploads to the accounting system.
6. (Optional) Deploy as a FastAPI Microservice
-
Create a FastAPI app to expose invoice processing via REST:
from fastapi import FastAPI, File, UploadFile from fastapi.responses import JSONResponse app = FastAPI() @app.post("/process-invoice/") async def process_invoice(file: UploadFile = File(...)): # Save file, run OCR, extract fields, validate, and return JSON # (Reuse code from previous steps) return JSONResponse(content={"message": "Invoice processed!"}) -
Run the service locally:
uvicorn app:app --reload -
Test the API with
curlor Postman:curl -F "file=@invoices/sample_invoice.pdf" http://localhost:8000/process-invoice/Screenshot description: JSON response with extracted invoice fields.
Common Issues & Troubleshooting
-
OCR misses key fields or returns gibberish:
- Try a different OCR engine or increase image quality.
- Preprocess images (deskew, increase contrast) before OCR.
- See our AI invoice processing tutorial for more OCR tips.
-
LLM extraction returns incomplete or incorrect data:
- Refine your extraction prompt for clarity and explicitness.
- Try few-shot prompting with example outputs.
- Switch to a more advanced LLM or fine-tune an open-source model.
-
API integration fails:
- Check your API credentials and endpoint URLs.
- Validate payload structure against your accounting software's API docs.
- Review HTTP status codes and error messages for clues.
-
Validation errors in Pydantic:
- Ensure LLM extraction returns correct data types (dates, floats).
- Add error handling to coerce or sanitize fields before validation.
Next Steps
- Expand field extraction to include line items and tax details.
- Add human-in-the-loop review for low-confidence extractions.
- Integrate with RPA bots for end-to-end AP automation.
- For advanced compliance workflows, see our Playbook: Building Automated Compliance Workflows for Financial Services.
- Explore how this workflow can connect with broader enterprise automation in our 2026 Guide: Automating Email Triage Workflows with AI in Enterprise IT.
By following this guide, you now have a robust, testable workflow to automate invoice processing with AI in 2026. For a full comparison of tools and strategies, check out our Best AI Workflow Automation Tools for Financial Teams in 2026.