Skip to content
mentoor

Topic 3 of 5 · 4 min read

Attaching metadata to every chunk

Goal: Design a chunk metadata schema that supports filtering, citations and permission checks for a described corpus.

Store enough metadata with every chunk that the system can filter it, cite it and decide who may see it without going back to the source file. Text alone is not enough, because a chunk that says "the limit is 50" means nothing without knowing which product, which version and which page.

Check yourself · 1 of 3

Why should access labels be stored as metadata on each chunk?

Choose one answer

Why it matters

Metadata does three jobs:

  • Filtering. Restrict a search to one product, language, version or date range before similarity ranking. This removes whole classes of wrong answers.
  • Citations. The answer can name the document, section and page, so a person can check it.
  • Access control. Each chunk carries who may read it, and the retriever enforces that before anything reaches the model.

What to store

Field Used for
doc_id, source_url citation, re-indexing, deletion
title, section_path citation, display, context
page citation for PDFs and slides
version, updated_at prefer current content, drop stale
language filter by user language
doc_type filter (policy, manual, ticket)
acl (groups or roles) permission filter
chunk_index rebuild order, fetch neighbours

Keep values simple and typed: strings, numbers, dates, short lists. The vector store filters on these fields, so they must be indexed and consistent. "v2", "2.0" and "version 2" in the same field breaks filters.

How to do it

  1. Decide which questions need filters: which product, which year, which team. Add those fields.
  2. Fill metadata at parse time, from the file path, document properties and detected headings. Do not rely on the model to guess it later.
  3. Put a short context prefix, such as the title and section path, into the text that is embedded when it helps retrieval, and keep the full values in metadata.
  4. Enforce acl in the retrieval query, not in the prompt.
chunk = {
  "text": "Refunds are issued within 14 days of return approval.",
  "metadata": {
    "doc_id": "policy-returns", "title": "Returns policy",
    "section_path": "Refunds > Timing", "page": 4,
    "version": "2026-03", "language": "en",
    "acl": ["support", "finance"], "chunk_index": 17,
  },
}

What goes wrong

  • Missing version. An old policy and a new one both match, and the answer mixes them.
  • Permission checks in the prompt. "Only answer if the user is allowed" does not stop a restricted chunk from being retrieved and leaking.
  • Inconsistent values. Filters silently match nothing because of spelling differences.
  • Metadata added late. Back-filling millions of chunks later means re-indexing.

Examples

A version filter removes the wrong answer

Two chunks answer 'What is the refund window?': one from the 2024 policy says 30 days, one from the 2026 policy says 14. Without a version field, the older chunk ranks first by wording.

With version stored, the query adds version = latest and only the 2026 chunk is searched.

Access labels in the query

results = index.search(
    query_vector,
    k=5,
    filter={"acl": {"$in": user.groups}, "language": "en"},
)

A user in the support group never sees chunks labelled only for finance, because those chunks are never returned.

Key points

  • Metadata lets the system filter, cite and enforce permissions without reopening the source file.
  • Store source, title, section path, page, version, date, language, document type and access labels with each chunk.
  • Fill metadata at parse time from paths, properties and headings, not by asking the model later.
  • Keep values typed and consistent, because filters on inconsistent strings quietly match nothing.
  • Enforce access labels in the retrieval query, since a prompt instruction cannot stop a restricted chunk from being retrieved.
  • A version or date field lets retrieval prefer current content over superseded documents.

Common mistakes

  • Storing only text and a vector, then trying to cite sources later.
  • Leaving out version or date, so superseded documents compete with current ones.
  • Relying on a prompt rule to keep restricted content from users instead of filtering at retrieval.
  • Writing the same value in different forms, such as 'v2' and '2.0', so filters miss chunks.

Check yourself · 2 of 3

An answer cites a policy that was replaced last year. Which missing field most directly allows the fix?

Choose one answer

Check yourself · 3 of 3

A filter on version = '2.0' returns no results, although many chunks come from version two. What is the most likely cause?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic