PDF Audit Information Extraction System with LLM

I developed an intelligent document processing system that automatically extracts structured information from audit reports using GPT-4 vision capabilities, converting complex multi-page PDFs into validated, normalized JSON—replacing hours of manual data entry with a reliable automated pipeline.

GPT-4 VisionPydantic ValidationPDF ProcessingAutomated Extraction

The Problem

Audit reports in the telecom industry are dense, multi-page PDF documents filled with tables, annotations, compliance checkboxes, measurement data, and technical observations. At Sofrecom, dozens of these reports arrive every week from field auditors who inspect fiber installations, network equipment, and infrastructure compliance. Each report contains critical data points that need to be extracted and entered into tracking systems for project management, compliance reporting, and quality assurance.

The manual extraction process was a bottleneck that nobody loved. Junior team members would spend entire days reading through PDFs, identifying the relevant fields (installation dates, equipment serial numbers, compliance status, defect descriptions, measurement values), and typing them into spreadsheets or databases. The process was slow, error-prone, and mind-numbingly repetitive. Worse, inconsistencies in how different people interpreted the reports meant the data quality degraded over time—the same observation might be categorized differently depending on who was doing the data entry.

Traditional OCR-based approaches had been tried and abandoned. The problem wasn't text recognition—it was comprehension. The reports didn't follow a perfectly rigid template. Tables might appear on different pages across different reports. Annotations were sometimes handwritten. The same data point might be expressed in slightly different formats depending on the auditor. What was needed was a system that could understand the content of the report, not just read its text—and that's where LLM capabilities offered a genuine breakthrough.

The Approach

My approach was to treat each PDF page as an image and leverage GPT-4's vision capabilities to extract information with contextual understanding. The critical design decision was to treat this as a "reading comprehension" task rather than a traditional parsing task. Instead of defining rigid extraction rules, I provided the LLM with the document image and asked it to identify and extract specific information fields—allowing it to handle the natural variation in report formats.

Decision: Image-Based Extraction vs. Text-Based OCR + NLP

I chose to convert PDF pages to images and use GPT-4 Vision rather than extracting text via OCR and then applying NLP. The reason was layout comprehension: audit reports rely heavily on spatial relationships (tables, checkboxes, annotations near specific items) that are lost when you flatten to text. The vision model can see that a measurement value sits inside a specific table cell next to a specific label—information that OCR + positional heuristics would struggle to reconstruct reliably.

The architecture is deliberately modular. Each component (PDF loading, page-to-image conversion, base64 encoding, LLM querying, response parsing, validation, output generation) is a separate module that can be tested, replaced, or scaled independently. This modularity proved essential when we needed to adjust image resolution for different report types or add new extraction fields without touching the rest of the pipeline.

Decision: Pydantic for Validation Rather Than Post-Hoc Cleaning

Rather than extracting raw text and cleaning it afterward, I defined strict Pydantic models for each expected output structure. This means the system validates data at extraction time—if GPT-4 returns a date in the wrong format, a measurement without units, or a missing required field, the validation catches it immediately and can trigger a re-extraction or flag for human review. This "fail fast" approach dramatically improved data quality compared to catching errors downstream.

Implementation Details

The PDF-to-image conversion step is more nuanced than it sounds. I used pdf2image with carefully optimized DPI settings—too low and the LLM can't read small text or table entries; too high and you exceed token limits or incur unnecessary API costs. Through experimentation, I found that 200 DPI provides the optimal balance for most audit report formats, with a fallback to 300 DPI for pages that contain dense tables or small annotations.

The base64 encoding layer handles image compression and format optimization before sending to the API. I implemented intelligent page batching—rather than sending all pages independently, the system analyzes page relationships (continuation tables, multi-page sections) and groups related pages in a single prompt context where possible. This allows the LLM to maintain continuity when a table spans two pages or when a defect description starts on one page and continues on the next.

The Pydantic models are the backbone of data quality. I defined nested model hierarchies that mirror the structure of the audit reports: a top-level AuditReport model contains Installation details, ComplianceChecks, Measurements, and Defects—each with their own validated fields, type constraints, and custom validators. Date fields must parse to valid dates. Measurement values must be numeric with recognized units. Status fields must match a defined enumeration. When the LLM's response doesn't conform, the system logs the specific validation failure and either retries with a refined prompt or routes to human review.

The automated JSON output generation creates structured files that integrate directly with downstream systems. Each extracted report produces a validated JSON document that can be loaded into project management databases, compliance dashboards, or analytics pipelines without any manual transformation. I also built a simple reconciliation system that compares LLM extractions against any previously entered manual data, flagging discrepancies for review—this was crucial during the validation phase to build confidence in the system's accuracy before fully replacing manual entry.

Key Takeaway

The success of LLM-based document extraction depends far more on your validation layer than on the extraction itself. GPT-4 is remarkably good at understanding document content, but it will occasionally hallucinate values, misread ambiguous handwriting, or return data in unexpected formats. Pydantic validation transforms this from a reliability problem into an engineering problem: you define exactly what valid output looks like, and the system either meets that bar or explicitly fails—there's no silent corruption. Build the validation first, then build the extraction around it.

Comments

FAQ

What is the key takeaway from "PDF Audit Information Extraction System with LLM"?

Automated extraction system for audit reports using GPT-4, with multi-page PDF processing, image conversion, and robust Pydantic-based validation for structured data output.

Who wrote this and what is it about?

This was written by Mahmoud Trigui, Senior Data Scientist. Automated extraction system for audit reports using GPT-4, with multi-page PDF processing, image conversion, and robust Pydantic-based validation for structured data output.

Machine Learning Feature Engineering MLForecast Time Series Decomposition Forecasting LightGBM XGBoost Catboost Clustering Segmentation NLP LLMs Web App R Markdown SQL Oracle DB SAS-Guide SAS E-Miner Dataiku BigQuery GCP Python R CRISP-DM Hypothesis Testing ANOVA Data Analytics Dimensionality Reduction Recommendation System Network Analysis Geospace Analysis Spatial Data Embedding Sampling Techniques Decision Rules Data Storytelling CVM Churn Fraud Detection Sentiment Analysis Topic Modeling IBM Watson PowerBI Looker Studio VBA Statistical Learning Ensemble Modeling Stacking Cross-Validation Profiling ABT Construction Plumber Tidyverse Shiny Prophet Deep Learning Scikit-Learn JSON SAS Programming Git VS Code CSS Styling Automated Reporting Outlier Detection Temporal Clustering Startup Survival Pre-Valuation Modeling K-Means Decision Trees Data Science Predictive Modeling SVM LDA Text Classification Weight Prediction Pattern Recognition Real-Time Detection Community Detection Pipeline Automation Data Quality Checks Data Reliability Specification Mapping Business Strategy Marketing Campaigns Try & Buy Frameworks KPI Dashboards Network Quality Sales Analytics Mentoring Statistics Lecturer Remote Work Hybrid Work Consulting Contract Full-Time Freelance Sofrecom Orange Group Tunisia Telecom Kiota Intelligence VC Analytics Series A Prediction Production ML Applied AI Prompt Engineering Business Forecasting Decision Systems Graph Analytics Household Detection Multi-SIM Detection FTTH Forecasting Audit Extraction Infrastructure Classification Pydantic GPT-4 OpenAI API Base64 Classification Zindi Codementor LAAS-CNRS ESSAI MIT xPRO Tunisia ML Competition Cell Tower Analysis Uber Logistics Uber Cape Town Necessary Condition Analysis Behavioral Signals Spike Smoothing Observation Unit Design Dendrogram Ward Clustering VIF Target Encoding Machine Learning Feature Engineering MLForecast Time Series Decomposition Forecasting LightGBM XGBoost Catboost Clustering Segmentation NLP LLMs Web App R Markdown SQL Oracle DB SAS-Guide SAS E-Miner Dataiku BigQuery GCP Python R CRISP-DM Hypothesis Testing ANOVA Data Analytics Dimensionality Reduction Recommendation System Network Analysis Geospace Analysis Spatial Data Embedding Sampling Techniques Decision Rules Data Storytelling CVM Churn Fraud Detection Sentiment Analysis Topic Modeling IBM Watson PowerBI Looker Studio VBA Statistical Learning Ensemble Modeling Stacking Cross-Validation Profiling ABT Construction Plumber Tidyverse Shiny Prophet Deep Learning Scikit-Learn JSON SAS Programming Git VS Code CSS Styling Automated Reporting Outlier Detection Temporal Clustering Startup Survival Pre-Valuation Modeling K-Means Decision Trees Data Science Predictive Modeling SVM LDA Text Classification Weight Prediction Pattern Recognition Real-Time Detection Community Detection Pipeline Automation Data Quality Checks Data Reliability Specification Mapping Business Strategy Marketing Campaigns Try & Buy Frameworks KPI Dashboards Network Quality Sales Analytics Mentoring Statistics Lecturer Remote Work Hybrid Work Consulting Contract Full-Time Freelance Sofrecom Orange Group Tunisia Telecom Kiota Intelligence VC Analytics Series A Prediction Production ML Applied AI Prompt Engineering Business Forecasting Decision Systems Graph Analytics Household Detection Multi-SIM Detection FTTH Forecasting Audit Extraction Infrastructure Classification Pydantic GPT-4 OpenAI API Base64 Classification Zindi Codementor LAAS-CNRS ESSAI MIT xPRO Tunisia ML Competition Cell Tower Analysis Uber Logistics Uber Cape Town Necessary Condition Analysis Behavioral Signals Spike Smoothing Observation Unit Design Dendrogram Ward Clustering VIF Target Encoding