Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Run llama Llama index VectorStoreIndex From Documents

From Leeroopedia

Overview

VectorStoreIndex is the primary vector-based index in LlamaIndex. It takes document nodes, generates embeddings using the configured embedding model, stores them in a vector store, and provides retrieval capabilities based on vector similarity search. The class supports construction from raw documents (with automatic chunking and embedding), from pre-processed nodes, or by wrapping an existing pre-populated vector store.

Vector Search RAG Pipeline Information Retrieval LlamaIndex Core

Source File

  • File: llama-index-core/llama_index/core/indices/vector_store/base.py, Lines 50-124
  • Class: VectorStoreIndex(BaseIndex)

Import

from llama_index.core import VectorStoreIndex

Class Signature

class VectorStoreIndex(BaseIndex):
    """Vector store-backed index.

    Builds an index by embedding document nodes and storing them
    in a vector store for similarity-based retrieval.
    """
    ...

Constructor Parameters

Parameter Type Default Description
nodes Optional[Sequence[BaseNode]] None Pre-processed nodes to build the index from. If provided, these nodes are embedded and stored directly (no chunking is applied).
use_async bool False Whether to use async operations for embedding and storage. Enables concurrent processing for better throughput.
store_nodes_override bool False Whether to store node data in the document store even when using an external vector store.
embed_model Optional[EmbedType] None The embedding model to use. If None, uses Settings.embed_model.
insert_batch_size int 2048 Number of nodes to embed and insert in each batch. Controls memory usage and API call frequency.
objects Optional[Sequence[IndexNode]] None Optional sequence of index node objects (nodes with references to other indices or tools).
index_struct Optional[IndexDict] None An existing index structure to use. If None, a new one is created.
storage_context Optional[StorageContext] None The storage context specifying the vector store, document store, and index store backends. If None, uses in-memory defaults.
show_progress bool False Whether to display a progress bar during embedding and indexing.

Class Methods

from_documents

The most common entry point for building an index from raw documents. Handles the complete pipeline: chunking documents into nodes, generating embeddings, and storing them.

@classmethod
def from_documents(
    cls,
    documents: Sequence[Document],
    storage_context: Optional[StorageContext] = None,
    show_progress: bool = False,
    transformations: Optional[List[TransformComponent]] = None,
    **kwargs,
) -> "VectorStoreIndex":
    ...
Parameter Type Default Description
documents Sequence[Document] required The documents to index. These are chunked into nodes, embedded, and stored.
storage_context Optional[StorageContext] None Storage backend configuration. If None, uses in-memory storage.
show_progress bool False Display progress bar during processing.
transformations Optional[List[TransformComponent]] None Custom transformation pipeline for document processing. If None, uses Settings.transformations.
**kwargs Additional keyword arguments passed to the constructor (e.g., embed_model, insert_batch_size).

from_vector_store

Wraps an existing, pre-populated vector store as a queryable index without re-embedding any data.

@classmethod
def from_vector_store(
    cls,
    vector_store: BasePydanticVectorStore,
    embed_model: Optional[EmbedType] = None,
    **kwargs,
) -> "VectorStoreIndex":
    ...
Parameter Type Default Description
vector_store BasePydanticVectorStore required An existing vector store instance that already contains embedded data.
embed_model Optional[EmbedType] None The embedding model to use for query-time embedding. Must match the model used to populate the vector store.
**kwargs Additional keyword arguments passed to the constructor.

Usage Examples

Building an Index from Documents

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.settings import Settings
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding

# Configure global settings
Settings.llm = OpenAI(model="gpt-4")
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")

# Load documents
documents = SimpleDirectoryReader("./data").load_data()

# Build the vector index (chunks, embeds, and stores)
index = VectorStoreIndex.from_documents(
    documents,
    show_progress=True,
)

Building with Custom Transformations

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.node_parser import SentenceSplitter

documents = SimpleDirectoryReader("./data").load_data()

# Use a custom chunking strategy
index = VectorStoreIndex.from_documents(
    documents,
    transformations=[SentenceSplitter(chunk_size=512, chunk_overlap=50)],
    show_progress=True,
)

Building from Pre-Processed Nodes

from llama_index.core import VectorStoreIndex
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core import SimpleDirectoryReader

# Load and manually chunk documents
documents = SimpleDirectoryReader("./data").load_data()
parser = SentenceSplitter(chunk_size=1024, chunk_overlap=200)
nodes = parser.get_nodes_from_documents(documents)

# Build index from pre-processed nodes
index = VectorStoreIndex(
    nodes=nodes,
    show_progress=True,
)

Using an External Vector Store (Chroma)

import chromadb
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext
from llama_index.vector_stores.chroma import ChromaVectorStore

# Set up Chroma
chroma_client = chromadb.PersistentClient(path="./chroma_db")
chroma_collection = chroma_client.get_or_create_collection("my_collection")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)

# Create storage context with the external vector store
storage_context = StorageContext.from_defaults(vector_store=vector_store)

# Load documents and build index
documents = SimpleDirectoryReader("./data").load_data()
index = VectorStoreIndex.from_documents(
    documents,
    storage_context=storage_context,
    show_progress=True,
)

Wrapping an Existing Vector Store

from llama_index.core import VectorStoreIndex
from llama_index.vector_stores.chroma import ChromaVectorStore
import chromadb

# Connect to an already-populated Chroma collection
chroma_client = chromadb.PersistentClient(path="./chroma_db")
chroma_collection = chroma_client.get_collection("my_collection")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)

# Wrap as a queryable index (no re-embedding)
index = VectorStoreIndex.from_vector_store(vector_store)

Inserting Additional Nodes into an Existing Index

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

# Build initial index
documents = SimpleDirectoryReader("./data/batch1").load_data()
index = VectorStoreIndex.from_documents(documents)

# Later, add more documents
new_documents = SimpleDirectoryReader("./data/batch2").load_data()
for doc in new_documents:
    index.insert(doc)

Querying the Index

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

# Build the index
documents = SimpleDirectoryReader("./data").load_data()
index = VectorStoreIndex.from_documents(documents)

# Create a query engine and query
query_engine = index.as_query_engine()
response = query_engine.query("What are the key findings in the report?")
print(response)

Construction Pipeline

When from_documents is called, the following steps occur internally:

  1. Apply transformations: Documents are passed through the transformation pipeline (defaults to Settings.transformations, typically [SentenceSplitter()]) to produce nodes.
  2. Generate embeddings: Each node's text is embedded using the configured embedding model (from embed_model parameter or Settings.embed_model), processed in batches of insert_batch_size.
  3. Store nodes: The embedded nodes are written to the vector store specified in the storage_context.
  4. Build index structure: An IndexDict is created mapping node IDs to their positions in the vector store.
  5. Return index: The fully constructed VectorStoreIndex instance is returned, ready for querying.

Inheritance Hierarchy

VectorStoreIndex inherits from BaseIndex, which provides:

  • The from_documents classmethod template
  • The as_query_engine() and as_retriever() methods
  • Storage and serialization infrastructure

Knowledge Sources

LlamaIndex Vector Store Index Guide LlamaIndex GitHub Repository

Principle

Principle:Run_llama_Llama_index_Vector_Index_Construction

Metadata

Environment:Run_llama_Llama_index_Python_LlamaIndex_Core Heuristic:Run_llama_Llama_index_Embedding_Batch_Size_Tuning 2026-02-11 00:00 GMT

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment