LlamaIndex Documents, Nodes and Node Parsers
LlamaIndex architecture and installation, and how documents become nodes with parsers and metadata.
LlamaIndex is a framework focused on connecting LLMs to your data. Its data model has two central objects: Documents, which represent source files, and Nodes, which are the chunks that get embedded and retrieved. This lesson explains LlamaIndex's architecture, installs it, and shows how documents become nodes through node parsers, with metadata and relationships intact.
What you will learn
- LlamaIndex's architecture: data loaders, nodes, indexes, retrievers and query engines
- How to install LlamaIndex and its integration packages
- How to load documents with
SimpleDirectoryReader - How node parsers create nodes, and how nodes keep metadata and relationships
Prerequisites
- RAG with LlamaIndex (overview) is recommended
Architecture
| Stage | LlamaIndex component |
|---|---|
| Load | Readers (SimpleDirectoryReader, hundreds of connectors on LlamaHub) produce Documents |
| Transform | Node parsers split documents into Nodes; extractors add metadata |
| Index | Indexes (VectorStoreIndex and others) store nodes, usually with embeddings |
| Query | Retrievers, node postprocessors, response synthesizers, query engines and chat engines |
Installation
pip install llama-index-core llama-index-readers-file llama-index-embeddings-huggingface llama-index-llms-anthropic
The llama-index meta-package installs a default bundle (including OpenAI integrations); installing llama-index-core plus the integrations you need keeps dependencies small. Integrations are separate packages named llama-index-<type>-<provider>.
Loading documents
from llama_index.core import SimpleDirectoryReader
reader = SimpleDirectoryReader(input_dir="docs", recursive=True, required_exts=[".txt", ".md", ".pdf"])
documents = reader.load_data()
print(len(documents))
print(documents[0].metadata) # file_name, file_path, file_type, dates, ...
3
{'file_path': '.../docs/expenses.txt', 'file_name': 'expenses.txt', 'file_type': 'text/plain', ...}
PDFs are loaded page by page, with the page label in metadata. Add your own metadata with file_metadata= (a function from file path to dictionary), or create documents directly:
from llama_index.core import Document
doc = Document(text="Employees get 18 days of paid leave.",
metadata={"department": "hr", "year": 2026},
excluded_embed_metadata_keys=["year"]) # keep 'year' out of the embedded text
LlamaIndex adds metadata to the text that is embedded and sent to the LLM by default; excluded_embed_metadata_keys and excluded_llm_metadata_keys control this per key.
Node parsers: documents to nodes
from llama_index.core.node_parser import MarkdownNodeParser, SentenceSplitter
splitter = SentenceSplitter(chunk_size=512, chunk_overlap=64) # sizes in tokens
nodes = splitter.get_nodes_from_documents(documents)
node = nodes[0]
print(len(nodes), node.node_id)
print(node.metadata["file_name"], node.get_content()[:80])
print(node.relationships.keys()) # SOURCE, PREVIOUS, NEXT ...
Each node knows its source document and its neighbours, which LlamaIndex uses for features such as returning surrounding context.
Node parser (from llama_index.core.node_parser) | Splits by |
|---|---|
SentenceSplitter | Token size, respecting sentence boundaries (default choice) |
TokenTextSplitter | Token size |
MarkdownNodeParser | Markdown headings |
SemanticSplitterNodeParser | Embedding similarity between sentences |
SentenceWindowNodeParser | Single sentences, storing a window of neighbours in metadata |
HierarchicalNodeParser | Several chunk sizes, linked as parents and children |
CodeSplitter | Source code structure |
An ingestion pipeline
Transformations can be chained so that splitting and embedding run together, with caching of unchanged inputs:
from llama_index.core import VectorStoreIndex
from llama_index.core.ingestion import IngestionPipeline
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
pipeline = IngestionPipeline(transformations=[SentenceSplitter(chunk_size=512, chunk_overlap=64), embed_model])
nodes = pipeline.run(documents=documents) # nodes now carry embeddings
index = VectorStoreIndex(nodes, embed_model=embed_model)
Import paths such as llama_index.core.ingestion have been stable for several releases, but check the documentation for your installed version if an import fails.
Common errors and troubleshooting
- OpenAI key errors although you use other models: LlamaIndex defaults to OpenAI for embeddings and LLM; set your own models globally or pass them explicitly (
embed_model=,llm=). - Import errors for integrations: install the specific
llama-index-*package. - Chunks longer than expected: metadata is counted into the text; exclude bulky metadata keys.
Key takeaways
- LlamaIndex flows from Documents to Nodes to Indexes to query and chat engines.
SimpleDirectoryReaderloads files with useful metadata; you can add your own.- Node parsers chunk documents and keep source and neighbour relationships.
- Set your embedding model and LLM explicitly to avoid default-provider surprises.
Frequently asked questions
What is LlamaHub?
LlamaIndex's registry of integrations: readers, vector stores, LLMs and tools, each as a separate package.
Can I use my own chunks?
Yes: create TextNode objects (from llama_index.core.schema) yourself and build an index from them.
Practice exercises
- Load a folder and print, per file, the number of nodes produced by
SentenceSplitterand byMarkdownNodeParser. - Add a
departmentmetadata field from the folder name withfile_metadata=.