Module 3 · Index types
Property Graph Index: knowledge graphs
6 min read
Some questions aren't about a passage but about relationships: "Who makes the Axis chair?", "Which products are compatible with the Lift desk?", "Which products come from a German supplier?". In a vector index, this information is scattered across several documents, and similarity search links it poorly. A knowledge graph represents it explicitly: entities (products, suppliers) linked by typed relationships (supplied by, compatible with). LlamaIndex's PropertyGraphIndex builds and queries such graphs.
The property graph model
| Element | Example | In LlamaIndex |
|---|---|---|
| Entity, with a type (label) and properties | CHAIR-7 (PRODUCT, warranty: 5 years) | EntityNode(label="PRODUCT", name="CHAIR-7", properties={...}) |
| Typed, directed relationship | CHAIR-7 —SUPPLIED_BY→ Ergoform GmbH | Relation(label="SUPPLIED_BY", source_id=..., target_id=...) |
| Link to the source text | The entity comes from a given passage | Automatic relationship to the text node |
A triplet (entity, relationship, entity) is the unit of information. The graph also keeps the original text nodes, which makes it possible to answer with the relationships and the passages that justify them.
Building the graph: LLM extractors or code
The PropertyGraphIndex builds its graph with path extractors (kg extractors), applied to each node:
| Extractor | How it works | Use |
|---|---|---|
SimpleLLMPathExtractor | The LLM freely extracts triplets | Exploration; graph sometimes inconsistent |
SchemaLLMPathExtractor | The LLM extracts according to an imposed schema (allowed entity and relationship types) | Production: clean, queryable graph |
DynamicLLMPathExtractor | Starting schema that the LLM can extend | In between |
ImplicitPathExtractor | No LLM: reuses relationships already present between nodes (previous, next…) | Document structure |
For free text (manuals, contracts), SchemaLLMPathExtractor is the right choice: imposing entity types (PRODUCT, SUPPLIER, COUNTRY) and relationship types (SUPPLIED_BY, COMPATIBLE_WITH, LOCATED_IN) avoids a graph where "made by", "produced by" and "comes from" coexist unrelated.
Lumen's case: data that's already structured
Nimbus's catalog is a JSON file: products, suppliers, compatibilities. The relationships are already explicit. Having an LLM extract them would be costly and would introduce errors into exact data. So Lumen writes a deterministic extractor, which follows LlamaIndex's extractor contract:
class CatalogGraphExtractor(TransformComponent):
"""Extracteur de graphe DÉTERMINISTE : le catalogue est déjà structuré, pas besoin de LLM."""
suppliers: dict[str, dict]
def __call__(self, nodes, **kwargs):
for node in nodes:
product = node.metadata["product"]
supplier = self.suppliers[product["supplier"]]
item = EntityNode(label="PRODUCT", name=product["sku"], properties={
"title": product["name"], "warranty_years": product["warranty_years"]})
maker = EntityNode(label="SUPPLIER", name=supplier["name"], properties={
"country": supplier["country"], "certification": supplier["certification"]})
others = [EntityNode(label="PRODUCT", name=sku) for sku in product["compatible_with"]]
relations = [Relation(label="SUPPLIED_BY", source_id=item.id, target_id=maker.id)]
relations += [Relation(label="COMPATIBLE_WITH", source_id=item.id, target_id=other.id)
for other in others]
node.metadata[KG_NODES_KEY] = [item, maker, *others]
node.metadata[KG_RELATIONS_KEY] = relations
return nodes
The contract is simple: an extractor puts its entities and relationships in each node's KG_NODES_KEY and KG_RELATIONS_KEY metadata; the index inserts them into the graph. Each product becomes a short Document ("Chaise ergonomique Axis (CHAIR-7), fournisseur Ergoform GmbH…") that carries its raw data as metadata:
PropertyGraphIndex.from_documents(
documents,
kg_extractors=[CatalogGraphExtractor(suppliers=suppliers)],
property_graph_store=SimplePropertyGraphStore(), # in production: Neo4jPropertyGraphStore
embed_model=models.embed_model(),
embed_kg_nodes=True,
)
Why go through the index rather than insert the entities directly into the store? Because the index embeds the entities (embed_kg_nodes=True) and links them to their source text. The SimplePropertyGraphStore can't do vector search by itself: without this step, the similarity retriever would find no entity. It's a trap met while building Lumen: entities inserted "by hand" remained impossible to find by meaning.
Querying the graph
Several retrievers traverse the graph:
| Retriever | Principle |
|---|---|
LLMSynonymRetriever | The LLM generates keywords and synonyms of the question, searched among entity names |
VectorContextRetriever | The entities closest to the question (by embedding), then their neighbors |
TextToCypherRetriever | The LLM writes a Cypher query (Neo4j), executed on the database |
CypherTemplateRetriever | A Cypher query written by you, where the LLM only fills in the parameters |
By default, index.as_retriever() combines the first two. For the question "Qui fournit la chaise CHAIR-7 ?", the graph returns triplets such as:
CHAIR-7 -> SUPPLIED_BY -> Ergoform GmbH
CHAIR-7 -> COMPATIBLE_WITH -> DESK-2
CHAIR-7 -> COMPATIBLE_WITH -> CASTER-M
The path_depth parameter sets the number of hops traversed from the entities found: 1 for direct neighbors, 2 for "products compatible with those made by this supplier". Beyond that, the context quickly explodes.
The TextToCypherRetriever is the most powerful — aggregations, complex paths — and the riskiest: a query generated by an LLM can be wrong, slow, or, without precautions, modify the database. Use a read-only account, and prefer the CypherTemplateRetriever for recurring questions.
Storing the graph
The SimplePropertyGraphStore keeps the graph in memory: perfect for a catalog of a few thousand entities, reloaded at startup. For a real enterprise graph, Neo4j (llama-index-graph-stores-neo4j package) brings persistence, the Cypher language, native vector search on entities and visualization. Lumen's code changes by a single line: the property_graph_store.
When is a graph worth it?
A graph is valuable when questions are about relationships, paths or aggregations ("how many products per supplier?"), and when entities are well defined. It's less so for content questions, where vector search is enough. Building a graph with an LLM on a heterogeneous corpus is costly and often produces noise: start with data that's already structured, as Lumen does, before extracting from free text.
Key takeaways
- A property graph represents typed entities and relationships: ideal for questions about relationships and paths.
- LLM extractors (preferably with a schema) for free text; deterministic code for data that's already structured.
- An extractor puts its entities and relationships in
KG_NODES_KEY/KG_RELATIONS_KEY; the index embeds the entities.- Retrievers: synonyms, vector context, generated or templated Cypher;
path_depthsets the depth.- In memory for a small graph, Neo4j in production; generated Cypher in read-only mode.
Practice
- Run
examples/03_router.pyand look at the triplets returned for the supplier question. - Add a
MADE_INrelationship (supplier → country) to the catalog and ask "Quels produits sont fabriqués en Allemagne ?" withpath_depth=2. - Test
SchemaLLMPathExtractoron the product sheets (with a real model), imposing thePRODUCT,COMPONENTtypes and theHAS_COMPONENTrelationship: which components does it extract?