Unstructured input
Important fields are embedded in prose, labels, tables and attachments.
D2 publishes architecture, implementation evidence and evidence boundaries separately so the reader can distinguish demonstrated system design from unverified production outcomes.
Read D2 evidence methodologySelected project 04
Structured Extraction, Classification, Validation & Operational Routing
A document-operations architecture that converts incoming business files into structured, classified and auditable data before downstream systems are asked to act on them.
01 · Business problem
Invoices, forms, contracts, receipts, purchase orders and operational attachments often arrive as files or email content. Before they can trigger routing, database updates or reporting, their meaning must be converted into a controlled structured representation.
Important fields are embedded in prose, labels, tables and attachments.
Departments and owners cannot be assigned consistently from raw files alone.
Without logs and privacy handling, extraction creates another uncontrolled data path.
02 · Core pipeline
The source begins with Gmail-triggered document intake, extracts file content, asks Gemini for typed fields, then applies deterministic categorization, anomaly checks, PII redaction and operational logging.
03 · Structured extraction
The workflow uses n8n's file-content extraction before a Gemini-backed information extractor. The extraction contract names the fields the model is allowed to return instead of passing free-form prose downstream.
Incoming business document
Unstructured attachment / file content
Typed structured object
Fields · arrays · entities · tables
{
"document_type": "…",
"sender": "…",
"recipient": "…",
"date": "…",
"amount": 0,
"invoice_number": "…",
"line_items": [...],
"entities": [...],
"relationships": [...],
"tables": [...]
}The extraction prompt explicitly asks for fields, tables, entities and relationships, and allows unknown attribute values to be omitted rather than invented.
04 · Deterministic vs AI
The architecture separates probabilistic semantic extraction from deterministic operational rules. This keeps department routing, tagging, anomaly thresholds, logging and privacy actions inspectable in workflow code.
AI interpretation
Gemini is used to interpret document text and return named fields such as document type, sender, recipient, date, amount, invoice number, line items, entities, relationships and tables.
Deterministic operations
Code nodes classify departments and owners, compute anomaly signals, redact matching PII patterns, route anomaly branches and prepare extraction/GDPR audit records for Supabase.
05 · Classification
The source categorization code maps document characteristics and keyword signals into department, process owner, compliance tags and a rule-based confidence value. These are workflow rules, not learned classification metrics.
CFO
Invoices · receipts · payments
General Counsel
Contracts · agreements · NDA
HR Director
Resume · employment · offer
COO
PO · shipping · delivery
Document Controller
Uncategorized / review required
The workflow also emits rule-based tags such as ACCOUNTS_PAYABLE, LEGAL_REVIEW, PII_SENSITIVE, GDPR_RELEVANT, PROCUREMENT, LOGISTICS, CONFIDENTIAL and REVIEW_REQUIRED depending on matched conditions.
06 · Anomaly layer
The source adds a deterministic anomaly engine after categorization. If the anomaly branch fires, a second Gemini-backed extractor produces an explanation, root cause, severity, recommended action and confidence field before anomaly logging.
Flags amounts more than three standard deviations from available batch history when enough values exist.
Checks a configured list of expected fields and raises the anomaly score when fields are missing.
Validates date, email and phone patterns when those values are present.
Builds a composite document/date/amount/vendor key to find similar records inside the processing set.
Flags dates more than one year in the future or more than ten years in the past.
Flags negative, zero or unusually large values according to explicit prototype thresholds.
07 · PII & governance
The workflow includes a recursive regex-based PII redaction engine and a separate GDPR audit-log path. The implementation is useful as an architectural prototype, but it also exposes areas that require stronger production controls.
The code recursively scans nested strings and arrays for SSN, credit-card, email, phone, passport and driver-license patterns, replacing matches with explicit redaction tokens.
After extraction logging, the workflow prepares audit timestamp, action type, process owner, document type and a PII-redaction indicator for a Supabase GDPR audit table.
The prototype retains a serialized original-data copy inside PII metadata. A production design should move raw sensitive content into separately protected storage with explicit retention and access controls rather than treating that copy as already secure.
08 · Observability
Rather than treating the final structured object as the only output, the source maintains additional records intended to explain what was processed, what looked suspicious and how the system is behaving over time.
Supabase document_extractions receives timestamp, confidence, processing status and workflow execution context.
Anomaly explanations are written to a separate Supabase anomaly_logs path when the anomaly branch is taken.
A distinct Supabase gdpr_audit_logs path records processing/audit metadata.
Scheduled branches calculate dashboard telemetry and correction/model metrics; these are metric definitions, not demonstrated achieved results.
09 · Business applications
These are example applications enabled by the architecture, not claims about deployed customers or observed business outcomes.
10 · Engineering principles
Extract once, structure early
Do not send raw document ambiguity downstream
Keep AI output constrained
Separate AI interpretation from deterministic business rules
Log every important processing stage
Treat privacy as part of architecture
11 · Production roadmap
The items below are possible next steps. They are not presented as implemented capabilities in the source workflow.
Final takeaway
“The objective is to turn documents from passive files into structured events that business systems can act on.”
The workflow demonstrates the architecture of that transition: controlled extraction, deterministic classification, anomaly inspection, privacy handling and auditable operational outputs.
D2 Automation Systems
D2 maps the process, source of truth, deterministic rules, failure paths and evidence boundary before recommending the automation scope.