Data Ingestion and File Parsing
The first link in the pipeline, "ingestion": normalize data from every source and format into one internal format.
Core concepts
| Source | Key handling points |
|---|---|
| User uploads | Size and type limits, virus scanning (depending on compliance needs), store the original file in object storage |
| Mailbox polling | Scheduled fetching, dedupe by Message-ID, split out attachments |
| Webhooks (pushed by customer systems) | Signature verification, return 2xx quickly, process asynchronously, dedupe by event ID |
| Pulling from third-party APIs | Pagination, rate limits, incremental cursors, retry on failure |
Unified output: Document { id, source, original_uri, mime, sha256, parsed_text, pages[], meta }.
How it works
- Store the original before processing: write the original file to object storage (S3 / MinIO / OSS) and record its sha256. Every later step can be replayed from the original, and you can dedupe by hash.
- Layered parsing:
- Native digital PDFs: extract the text layer directly (e.g. PyMuPDF, pdfplumber)
- Scans / images: OCR, or hand them straight to a multimodal model
- Office documents: python-docx, openpyxl, or convert to PDF first
- Webhook reliability: senders usually retry, so the receiver must be idempotent; verify the signature to prevent forgery (usually an HMAC over the request body).
In practice: receiving a webhook
python
import hmac, hashlib
from fastapi import Request, HTTPException
@router.post("/webhooks/erp")
async def erp_webhook(request: Request):
body = await request.body()
sig = request.headers.get("X-Signature", "")
expected = hmac.new(settings.ERP_SECRET.encode(), body, hashlib.sha256).hexdigest()
if not hmac.compare_digest(sig, expected): # constant-time comparison against timing attacks
raise HTTPException(401, "invalid signature")
event = ErpEvent.model_validate_json(body)
created = await create_task_if_absent(idempotency_key=f"erp:{event.id}", payload=event)
return {"accepted": True, "duplicate": not created} # return immediately; a worker does the processingLetting a multimodal model read the PDF directly
Both the Claude and OpenAI APIs accept PDFs as input (the model sees both the text and the page images). For tables and scans this is often simpler than a traditional OCR pipeline. Mind the page limits and token cost, and keep traditional parsing as a fallback.
Common pitfalls
- Doing slow work synchronously in a webhook: the sender times out and retries, causing duplicates. Return immediately and process asynchronously.
- Comparing signatures with
==: exposes you to timing attacks. Usehmac.compare_digest. - Not keeping the original: after the parsing logic improves, you can't re-run historical data.
- Encodings and tables: garbled Chinese PDFs, tables broken apart. Spot-check parsing results.
Common interview questions
- How would you design an endpoint that receives third-party webhooks?
- How does parsing a scan differ from parsing a digital PDF?
- Why store the original file first? How do you dedupe?
- When should a multimodal model read the document directly, and when should you use traditional OCR?
Sources
- Anthropic Docs: PDF support
- OpenAI Docs: File inputs
- Python Docs: hmac.compare_digest
- GitHub Docs: Validating webhook deliveries — an example of the industry-standard HMAC signature check
- PyMuPDF Docs
- FastAPI Docs: Request Files
