Ingestion and chunking

Getting documents in cleanly: parsing, metadata, structure-aware chunking, deduplication, versioning, updates and deletion.

L4 · Applied AI engineer≈ 5 min read
Key points · the two-minute version
  1. Pipeline: connect → layout-aware parse → handle PII → metadata → chunk → embed → index.
  2. Chunk structure-aware first (never split a table or a criterion), then tune size and overlap on recall@k.
  3. Prefix chunks with context (document › section) so they stand alone.
  4. Ingestion must be idempotent, keyed by source ID + checksum, with versions that supersede.
  5. Deleting a document removes its vectors, lexical entries and caches, then verifies they're gone.
You should be able to
  • Build an idempotent ingestion pipeline
  • Choose a chunking strategy and test it on retrieval metrics
  • Handle updates, deletions and embedding-model changes

Why this matters

Retrieval quality is capped by ingestion quality. A table split in half, a scanned page with garbled text, or a document that was deleted but still answers questions: these are ingestion bugs, and they look like "the AI is wrong".

The pipeline

  1. Connect: files, wikis, tickets, databases. Check type and size.
  2. Parse: layout-aware for PDFs and scans (tables, headers, columns); OCR where needed.
  3. Handle sensitive data: redact or classify personal data before anything is indexed.
  4. Normalise + metadata: title, section path, page, version, source ID, tenant, ACL labels, dates.
  5. Chunk (below).
  6. Embed in batches; record the embedding model.
  7. Index: vectors + lexical index + metadata, in one transaction where possible.

Q3. Chunk size and overlap?

No universal size. Structure-aware first (never split a lab table, dosage block or coverage criterion), baseline on a representative query set, then compare sizes and overlap on recall@k and answer quality. Small: precise, loses context. Large: context, diluted embeddings, more tokens.

Strategies

Strategy How Good for
Fixed size + overlap N tokens with M overlap Uniform prose; quick baseline
Structure-aware Split on headings, sections, list items, table boundaries Manuals, policies, contracts
Parent-child Retrieve small chunks, return their larger parent section Precise matching with enough context
Semantic Split where topic shifts Long unstructured text

Prepend context to each chunk ("Policy X › Section 4.2 › Exclusions") so it stands on its own.

Q9. Updates and deletions?

Version documents and chunks; idempotent ingestion keyed by source ID + checksum; supersede on update; on delete remove vectors, lexical entries and caches, then verify. Embedding model change = full re-embed with a dual-index cutover.

Common mistakes

  • Naive PDF text extraction that scrambles tables and columns.
  • No source ID or checksum, so re-ingestion creates duplicates.
  • Deleting the document row but not its vectors and cached answers.
  • Choosing chunk size by intuition rather than measurement.

Worked example: one policy document

source: handbook.pdf (v7, sha256 3f1…, tenant t_42, acl: [staff])
parse  → 42 pages, 3 tables kept intact, headings detected
chunks → 118 structure-aware chunks; 12 parent sections
         each prefixed: "Handbook › Leave › Parental leave (v7) — …"
embed  → 118 vectors, embed_model = "embed-v3"
index  → vectors + tsvector + metadata in one transaction
update → v8 arrives: new checksum → new version written, v7 chunks marked superseded
delete → vectors, lexical rows, caches removed; retrieval test confirms nothing returns

Interview follow-ups

  • "How do you handle tables in PDFs?" Layout-aware parsing that keeps tables whole; store them as Markdown or row-wise text with headers repeated, plus a summary chunk.
  • "How do you index scanned documents?" OCR with confidence scores; low-confidence pages flagged; keep page images for review.

Check yourself

How do you make ingestion idempotent?

Key every document by source ID and a content checksum. If the checksum is unchanged, skip; if changed, write a new version and mark old chunks superseded, in one transaction. Re-running the pipeline produces the same state.

What's the risk of an embedding model change, and how do you handle it?

Old and new vectors aren't comparable. Re-embed everything into a new index, run evals on both, switch reads atomically, then retire the old index.

Build it

Ingest a set of Markdown or PDF documents with structure-aware chunks, section-path prefixes, source IDs and checksums. Re-run it and prove no duplicates appear; delete one document and prove it can't be retrieved.

What comes next

8.3 Hybrid search and re-ranking.

Build it to remember

Two hands-on projects that exercise this lesson. Pick one, build it end to end, and keep it in your portfolio.

Project 1

Idempotent ingestion worker

Ingest PDFs and Markdown with structure-aware chunks, checksums and versions; re-runs create no duplicates and deletes are verified.

  • Python
  • Celery
  • unstructured or Docling
  • pgvector
  • S3/MinIO

Practises Ingestion correctness.

Project 2

Chunking experiment

Compare fixed, structure-aware and parent-child chunking on recall@5 and answer quality, and publish the results table.

  • Python
  • LlamaIndex or plain code
  • an eval set

Practises Choosing chunk strategy by measurement.

Can you answer every check-yourself question out loud without opening the answer?