Module 2 · Parsing & chunking
Metadata and automatic extractors
5 min read
A passage alone is often ambiguous. "The warranty covers the LED, the dimmer and the USB-C port": of which product? "The window is 30 days": to do what? Metadata provides this context, in three places: in the embedding (to retrieve better), in the prompt (to answer better) and in filters (to search only in the right place). LlamaIndex produces it with extractors, pipeline transformations that enrich each node.
Three uses of metadata
| Use | Example in Lumen | Mechanism |
|---|---|---|
| Enrich the embedding | The heading path "Lampe de bureau Halo / Garantie" embedded with the passage | Included by default, except excluded keys |
| Inform the model | The source shown with each excerpt, so it can cite it | Included in the context, except excluded keys |
| Filter retrieval | Search only in nodes about MUG-01 | Metadata filters (lesson 4.1) |
The same metadata can serve all three, or just one: that's the role of the excluded_embed_metadata_keys and excluded_llm_metadata_keys lists seen in lesson 1.3.
How metadata enters the text
When a node is embedded or sent to the model, LlamaIndex builds a text combining metadata and content, according to two templates configurable per node:
node.metadata_template = "{key}: {value}" # format of one metadata entry
node.text_template = "{metadata_str}\n\n{content}" # metadata + content assembly
print(node.get_content(metadata_mode=MetadataMode.EMBED)) # what the embedding model sees
print(node.get_content(metadata_mode=MetadataMode.LLM)) # what the LLM sees
Display these two views on a few nodes before launching a large ingestion: it's the surest way to detect an invasive metadata field (a long file path repeated in every embedding) or a missing one.
A custom extractor, without an LLM
The first reflex for extraction is to call an LLM. Often, a rule is enough — and it costs zero tokens, is never wrong and runs in microseconds. Lumen adds to each node the list of product references it mentions:
SKU = re.compile(r"\b(?:LAMP-3|CHAIR-7|DESK-2|MUG-01|CASTER-M)\b")
class SkuTagger(BaseExtractor):
"""Extracteur maison, sans LLM : ajoute aux nœuds la liste des produits qu'ils mentionnent."""
async def aextract(self, nodes: Sequence[BaseNode]) -> list[dict[str, Any]]:
results = []
for node in nodes:
skus = set(SKU.findall(node.get_content()))
if node.metadata.get("sku"): # un nœud de fiche produit hérite de la référence de sa fiche
skus.add(node.metadata["sku"])
results.append({"skus": sorted(skus)})
return results
An extractor inherits from BaseExtractor and implements aextract, which returns one metadata dictionary per node; LlamaIndex merges them into the existing metadata. Here, a node from a product sheet inherits its document's reference (sku), and a policy passage that cites "CASTER-M" is tagged too. The test_pipeline_splits_tags_and_embeds test checks that every node of the lamp's sheet has skus == ["LAMP-3"].
In production, the list of references would come from the catalog, not from a hand-written expression.
LLM-based extractors
When a rule isn't enough, LlamaIndex provides extractors that call the model:
| Extractor | Metadata produced | Usefulness |
|---|---|---|
TitleExtractor | Document title, inferred from the first nodes | Documents without an explicit title |
SummaryExtractor | Summary of the node (and its neighbors) | Context for ambiguous passages |
QuestionsAnsweredExtractor | Questions the passage answers | Bring the vocabulary of questions closer to that of documents |
KeywordExtractor | Keywords | Hybrid search, filters |
DocumentContextExtractor | The passage's place in the whole document | Passages meaningful only with the rest of the document |
PydanticProgramExtractor | Fields of a Pydantic schema | Custom structured extraction |
QuestionsAnsweredExtractor is particularly effective: users ask questions ("can I return a piece of furniture?"), whereas documents state rules ("for bulky items, a pickup is organized"). Adding to the passage the questions it answers brings the two vocabularies closer. Lumen enables it with the with_llm_extractors=True option, on the fast model:
QuestionsAnsweredExtractor(questions=2, llm=models.fast_llm())
The cost of LLM extractors
An LLM extractor makes one call per node (sometimes more). On 100,000 nodes, that's 100,000 calls per full ingestion. Four reflexes:
- Prefer a rule when it's enough (references, dates, categories from the file path).
- Use the fast model: extractions are simple tasks.
- Rely on the cache and docstore (next lesson): an unchanged node is never re-extracted.
- Measure the gain: an extractor that doesn't improve retrieval hit rate on your test questions isn't worth its cost.
Metadata and privacy
Metadata travels everywhere: in the vector database, in prompts, in traces. A customer name in a ticket's metadata will end up in your observability tool's logs. Apply the same rules to metadata as to text: minimization, and exclusion of sensitive keys from the prompt when they're only used for filtering.
Key takeaways
- Metadata serves to retrieve better (embedding), answer better (prompt) and filter better.
- Control what the embedding and the LLM see with excluded keys and
get_content(metadata_mode=...).- An extractor inherits from
BaseExtractorand returns one metadata dictionary per node.- Prefer a rule to an LLM when it's enough; otherwise, fast model, cache and measured gain.
- Metadata travels everywhere: minimize it like the text.
Practice
- Display, for a node of the lamp's sheet,
get_content(MetadataMode.EMBED)andget_content(MetadataMode.LLM): which metadata appears? - Write a
PolicyOwnerTaggerextractor that adds to policy nodes the responsible department (ownerin the header) and the update date. - With a real model, enable
QuestionsAnsweredExtractorand compare retrieval hit rate with and without it.