Hardening Invoice Processing in n8n: Zero-Loss Currency Parsing & Multi-Factor Duplicate Detection (v0.9.1 Beta Update)

Overview

When building LLM-based OCR pipelines for financial document processing (e.g., using Gemini/LangChain nodes in n8n), relying solely on raw text extractions for duplicate validation creates silent edge cases.

In our v0.9.1 Beta pipeline, vendor-specific formatting variances in currency strings (⁠Rp 15.000.000⁠ vs ⁠15,000,000.00⁠ vs ⁠15000000,50⁠) led to false negatives during duplicate matching and rounding precision errors in downstream mathematical audit checks.

Here is how we solved this at the workflow layer, alongside our roadmap for database migration to PostgreSQL/Supabase.

The Architecture Vulnerability (v0.9.1 Beta)

1. Format Heterogeneity: OCR models output exact verbatim text per prompt instructions (⁠AMOUNT = "Rp 15.000.000,50"⁠). Comparing raw string outputs across execution runs caused identical amounts to be evaluated as non-matching.

2. Standard ⁠parseFloat()⁠ Pitfalls: Naive regex cleanup like ⁠.replace(/\./g, ‘’).replace(/,/g, ‘.’)⁠ breaks when receiving whole-number amounts with comma thousands separators (⁠15,000,000⁠ \rightarrow ⁠15.000.000⁠ \rightarrow truncated by ⁠parseFloat⁠ to ⁠15⁠).

3. Data Loss via Rounding: Floating-point arithmetic during math validation risks losing fractional minor units (sen/cents like ⁠,50⁠ or ⁠,91⁠).

Technical Solution: Deterministic Minor Unit Normalization

We implemented a zero-dependency JavaScript parsing algorithm inside custom Code Nodes (⁠MATCH DUPLICATE CODE⁠, ⁠MATH CHECK⁠, and ⁠RE VALIDATE MATH⁠).

1. Zero-Loss Currency Normalization Function

Instead of floating-point comparison, all currency values are converted into integer minor units (Cents/Sen)

2. Multi-Factor Duplicate Matching Logic

Rather than matching solely on ⁠VENDOR + INVOICE_NUMBER⁠, duplicate detection now enforces a deterministic 3-key tuple: ⁠VENDOR⁠ + ⁠INVOICE_NUMBER⁠ + ⁠AMOUNT_IN_CENTS⁠.

3. Math Audit Check with Rounding Tolerance

For invoice audit checks (\text{Subtotal} + \text{Tax} = \text{Amount}), we enforce strict minor-unit evaluation while allowing a 10,000 Sen (Rp 100) threshold to handle vendor tax rounding variations

Debugging & Troubleshooting Lessons

During execution testing, we encountered and resolved several critical edge cases:

Item Truncation from Sheet Nodes: Pushing array results directly from Google Sheets lookup nodes reduced execution batch size (N \rightarrow 1). Resolving this required mapping incoming items against the original ⁠$data⁠ execution context inside a unified Code Node using ⁠.all().map()⁠.

Logical Operator Syntax Dropping: Ensure syntax sanitization when passing JavaScript snippets across node setups; lost logical operators (⁠||⁠) will trigger silent ⁠SyntaxError⁠ crashes on runtime execution.

Preserving Raw Document Extraction: Do NOT attempt to alter the LLM prompt to output standardized numbers. Keep LLM extraction strict to raw document representation to avoid hallucination, and handle deterministic transformation exclusively in Code Nodes.

Next Roadmap: PostgreSQL / Supabase Database Migration

With data sanitization locked in at the workflow level, our current Google Sheets lookup bottleneck is being transitioned to PostgreSQL / Supabase.

This will enable:

1. Native indexing on composite keys ⁠(LOWER(vendor), LOWER(invoice_number), amount_cents)⁠.

2. ACID-compliant concurrent execution locks during high-throughput email batch processing.

3. Direct integration with n8n Postgres/Supabase nodes for sub-10ms duplicate evaluation times.

Hope this technical breakdown helps anyone building financial document automation in n8n! Feedback and alternative approaches are welcome.