Turning documents into nodes in LlamaIndex

MediumRetrieval-augmented generation (RAG)RAG

Goal: Understanding the first step of LlamaIndex indexing before embedding.

Python — example.py
from llama_index.core import Document
from llama_index.core.node_parser import SentenceSplitter

docs = [Document(text="PyTorch إطار تعلم عميق. " * 30, metadata={"source": "pytorch.md"}),
        Document(text="MONAI للصور الطبية. " * 30, metadata={"source": "monai.md"})]
nodes = SentenceSplitter(chunk_size=64, chunk_overlap=8).get_nodes_from_documents(docs)
print("Number of nodes:", len(nodes))
print("Source of the first node:", nodes[0].metadata["source"])
print("The first node has a next node:", nodes[0].next_node is not None)

Code explanation

SentenceSplitter turns Document objects into nodes while keeping metadata and the relationships between adjacent nodes. The nodes are then embedded in a VectorStoreIndex, a step that needs an embedding model (local or through an API), so we did not run it here.

Requirements

Shell
pip install llama-index-core

How to run

Save the code in a file example.py then run it:

Shell
python example.py

Output recorded during testing

Number of nodes: 18
Source of the first node: pytorch.md
The first node has a next node: True

This example was actually run and tested on 21 November 2026 on a CPU — llama-index-core 0.14.26, python 3.13.18

Libraries used

Related examples