LangChain blueprint · Accounting & Finance
Invoice data extraction
A Pydantic schema and structured output turn PDFs and scans into checked, typed invoice records.
- Steps
- 7
- Subjective questions
- 32
- Objective questions
- 32
The problem and the outcome
Problem: Clerks retype hundreds of invoices a day, and typos cause wrong or duplicate payments.
Outcome: Most invoices posted with no typing; people check only the exceptions.
Architecture
How it works, step by step
- Supplier invoices arrive by email as PDFs and phone scans.
- A document loader extracts the text; scans go through OCR first.
- The fields are defined once as a Pydantic schema: vendor, invoice number, dates, line items, tax and total.
- with_structured_output binds the schema to the model, so it returns typed data instead of free text.
- Code checks the result: line items add up to the total, the tax ID is valid and a purchase order matches.
- Valid invoices post straight to accounts payable in the ERP.
- Anything that fails a check goes to a person, with the fields and the source text side by side.
Tech stack
- LangChain with_structured_output
- Pydantic models
- PDF loaders + OCR (Tesseract, Textract, Azure Document Intelligence)
- Validation code
- ERP integration (SAP, Tally, Zoho Books)
Key design decisions
- A Pydantic schema defines every field and type once, and the model must return data in that shape.
- Arithmetic and business checks run in code, never trusted to the model.
- Low-confidence or failing invoices go to a person with the source highlighted.
Common mistakes
- Letting the model 'fix' totals so they add up.
- No duplicate-invoice check, causing double payments.
- Treating dates and currencies loosely (DD/MM vs. MM/DD, ₹ vs. $).
What to measure
- Field-level accuracy
- Straight-through rate (no human touch)
- Duplicate payments prevented
- Time per invoice
Most-asked interview questions
Subjective questions 32
1What does with_structured_output do?Asked almost every time
It binds a schema (Pydantic model, TypedDict or JSON Schema) to a chat model, using the provider's tool calling or JSON mode, so the model returns an object matching the schema instead of free text. You get typed, validated data, and a validation error when it does not fit.
2Design an invoice extraction pipeline with LangChain.Asked almost every time
Receive invoices by email or upload, convert PDFs and images to text with layout (OCR where needed) or send pages to a vision model, extract fields with with_structured_output into a Pydantic schema, validate totals, tax and supplier details with code, match against purchase orders and goods receipts, and route mismatches or low-confidence fields to an accounts payable reviewer before posting to the ERP.
3How do you design the invoice schema?Asked almost every time
Include header fields (supplier name and tax ID, invoice number, dates, currency, totals, tax amounts) and a list of line items (description, quantity, unit price, tax rate, amount), with types, formats and descriptions for each field, and optional fields marked explicitly rather than guessed.
4How do you do three-way matching?Asked almost every time
Compare the invoice with the purchase order (prices, quantities, supplier) and the goods receipt (quantities received); within tolerance it can be approved automatically, otherwise it is flagged with the specific differences for review.
5Why validate extracted data with code?Very often asked
Models can misread digits or invent values that look plausible. Code checks that line items sum to the total, tax rates are valid, the vendor exists, the purchase order matches and the invoice is not a duplicate. Failing checks route the invoice to a person.
6How do you handle different invoice layouts from many suppliers?Very often asked
Rely on a general extraction model with a strong schema rather than per-template rules, add supplier-specific hints for difficult formats, and learn from reviewer corrections per supplier.
7How do you get confidence for extracted fields?Very often asked
Combine validation results (totals add up, formats valid, supplier found), agreement between two extraction passes or models, and OCR confidence, and route fields below thresholds to review.
8How do you detect duplicate or fraudulent invoices?Very often asked
Check supplier plus invoice number plus amount against history, detect near-duplicates with fuzzy matching, verify bank details against the supplier master, and flag changes in bank accounts for verification by phone.
9How do you handle tax rules such as GST?Very often asked
Extract GSTIN, HSN or SAC codes and tax splits (CGST, SGST, IGST), validate the GSTIN format and state codes, recompute tax amounts in code, and flag inconsistencies.
10What does the reviewer screen look like?Very often asked
The original invoice with highlighted source regions next to extracted fields, validation errors and mismatch details, with quick edit and approve actions; corrections are saved for learning.
11How do you scale to thousands of invoices a day?Very often asked
Process asynchronously through a queue, batch model calls, cache supplier data, use cheaper models for easy invoices and stronger ones for hard ones, and monitor throughput and error rates.
12How do you post approved invoices to the ERP?Very often asked
Use the ERP's API to create the payable with the vendor, GL coding and PO references, idempotently keyed by supplier and invoice number, and record the ERP document ID back on the invoice.
13How do you validate extracted line items against the invoice totals?Very often asked
Recompute line amounts from quantity and unit price, sum lines plus tax, freight and discounts, and compare with the stated subtotal, tax and grand total within a small rounding tolerance. Any mismatch sends the invoice to review with the failing check highlighted.
14How do you route an invoice to the right approver?Very often asked
Look up approval rules by cost centre, amount band, supplier category and whether a PO exists, assign the approver from the organisation chart, escalate if they are absent or do not act within the deadline, and record every approval with time and comments.
15How do you measure extraction accuracy?Often asked
Label a sample of real invoices field by field, compare the extracted values per field (exact match for IDs, tolerance for amounts), and report accuracy per field and per supplier template. Track it over time as new templates appear.
16How do you assign general ledger codes?Often asked
Use rules from the PO and supplier defaults first, then a classifier trained on past coding for non-PO invoices, with low-confidence suggestions reviewed.
17How do you handle multi-page invoices and attachments?Often asked
Process all pages together, detect continuation of line item tables, ignore unrelated attachments like terms and conditions, and keep page references for each field.
18How do you handle handwritten or poor-quality scans?Often asked
Use OCR or vision models designed for handwriting, ask the supplier for a digital copy when quality is too low, and always route such invoices to review.
19How do you evaluate a new model for extraction?Often asked
Run it on a labelled set of invoices with field-level ground truth, compare field accuracy, straight-through processing rate and cost against the current model, then pilot on a subset.
20What is straight-through processing?Often asked
Invoices that pass extraction, validation and matching without any human touch and are posted automatically; increasing its share is the main efficiency goal.
21How do you secure invoice data?Often asked
Encrypt documents and extracted data, restrict access to finance roles, mask bank details in logs, and keep audit trails of every change and approval.
22How do e-invoicing standards change the pipeline?Often asked
Structured e-invoices (such as India's IRN-based e-invoicing, Peppol or UBL XML) can be parsed directly without AI extraction, so the pipeline should detect and prefer them.
23How do you learn from reviewer corrections?Often asked
Store corrected fields with the document, analyse frequent errors by supplier and field, add examples to prompts or fine-tuning sets, and re-evaluate regularly.
24How do you handle early-payment discounts?Often asked
Extract the payment terms, such as 2/10 net 30, compute the discount deadline from the invoice date, prioritise those invoices in the approval queue, and schedule payment before the deadline when cash policy allows.
25How do you extract data from invoices that arrive by email?Often asked
Monitor a dedicated mailbox, separate the invoice attachments from other files, handle invoices embedded in the email body, record the sender and message ID for traceability, and reject or quarantine files with suspicious types.
26How do you keep supplier master data clean while automating?Often asked
Match extracted supplier names, tax IDs and bank details against the vendor master, never create a new vendor automatically, and send unknown or changed details to the vendor management team for verification.
27How do you test the pipeline with sensitive financial documents?Often asked
Use a held-out set of real invoices in a secure environment with access controls, plus synthetic invoices generated from templates for edge cases, and compare field-level accuracy without exporting the documents.
28What should happen to invoices without a purchase order?Sometimes asked
Route them for approval by the budget owner according to spend limits, with suggested GL coding, before posting.
29How do you handle credit notes?Sometimes asked
Classify document type first; credit notes reduce payables and must be linked to the original invoice, with amounts validated as negative adjustments.
30What metrics matter for invoice automation?Sometimes asked
Field-level accuracy, straight-through processing rate, time from receipt to posting, exception rate by reason, duplicate catches and cost per invoice.
31How do you handle currencies and exchange rates?Sometimes asked
Extract the invoice currency, convert using the official rate for the posting date in code, and store both original and converted amounts.
32How do you handle withholding tax or TDS deductions on payments?Sometimes asked
Determine the applicable section and rate from the supplier's status and service type, calculate the deduction on the right base amount, record it separately in the ledger, and generate the certificates and filings required.
Objective questions 32
1What defines the fields the model must return?Asked almost every time
Why: The schema is passed to with_structured_output and enforced on the result.
2What is three-way matching?Asked almost every time
Why: Catches price and quantity errors.
3Which signal should trigger bank detail verification?Asked almost every time
Why: A common fraud pattern.
4How should missing fields be represented?Asked almost every time
Why: Guesses become errors.
5Who should check that line items add up to the total?Very often asked
Why: Deterministic arithmetic belongs in code.
6How should tax amounts be validated?Very often asked
Why: Tax needs exact arithmetic in code.
7What reduces errors across many supplier layouts?Very often asked
Why: Add supplier-specific hints where needed.
8How is ERP posting made safe to retry?Very often asked
Why: Avoid duplicate payables.
9Straight-through processing means…Very often asked
Why: The key efficiency goal.
10Which invoices should always be reviewed?Very often asked
Why: Low confidence needs humans.
11What should the reviewer see next to each field?Very often asked
Why: Highlights make verification fast.
12Extracted totals are validated by…Very often asked
Why: Arithmetic checks catch extraction errors.
13Approver routing depends on…Very often asked
Why: Approval rules follow finance policy.
14What prevents paying the same invoice twice?Often asked
Why: A uniqueness check catches resubmitted invoices.
15Which Indian identifiers appear on GST invoices?Often asked
Why: Validate their formats.
16Structured e-invoices should be…Often asked
Why: Parsing them is cheaper and exact.
17GL codes for non-PO invoices can come from…Often asked
Why: Review low-confidence suggestions.
18Credit notes must be…Often asked
Why: They reduce payables.
19How should a new extraction model be compared?Often asked
Why: Then pilot on a subset.
20What helps scale to thousands of invoices?Often asked
Why: Cheaper models for easy cases.
21Bank details in logs should be…Often asked
Why: Protect sensitive data.
22Invoices without a PO need…Often asked
Why: Approvals keep spending under control.
23Terms of 2/10 net 30 mean…Often asked
Why: Early payment earns the discount.
24Unknown supplier details should be…Often asked
Why: Automatic vendor creation invites fraud.
25Invoices arriving by email should be traced by…Often asked
Why: Traceability links the payment to its source.
26Sensitive test invoices should stay…Often asked
Why: Financial documents carry confidential data.
27Exchange rates should be applied…Sometimes asked
Why: Store both original and converted amounts.
28Reviewer corrections are useful for…Sometimes asked
Why: Learn per supplier and field.
29Which metric tracks exceptions?Sometimes asked
Why: Fix the biggest causes.
30Multi-page line item tables must be…Sometimes asked
Why: Tables continue across pages.
31Which standard is used for e-invoices in Europe?Sometimes asked
Why: Structured XML formats.
32TDS deductions are recorded…Sometimes asked
Why: Separate records support certificates and filings.
Track your progress through every blueprint in your dashboard.
Start learning