How to Build a LlamaIndex Vector Store With TiDB A LlamaIndex tutorial details how to replace the framework's default in-memory vector store with a persistent TiDBVectorStore, requiring only a change to the StorageContext argument while leaving existing VectorStoreIndex code unchanged. The guide, referencing LlamaIndex and PingCAP TiDB documentation, notes that TiDB vector search runs on TiDB Cloud Starter or TiDB Self-Managed v8.4.0 and later, with v8.5.0 or later recommended, and that TiDBVectorStore supports only LlamaIndex's default query mode, so keyword and hybrid retrieval need a separate SQL or pytidb path. The setup requires Python 3.10 or later, an OpenAI API key for embeddings, and installation of the llama-index and llama-index-vector-stores-tidbvector packages. A LlamaIndex vector store is where your index keeps node embeddings, usually alongside the text chunks and metadata, so queries can find similar content. The default store lives in memory, so a restart or redeploy wipes the index and sends you back to your embedding provider to pay for the same corpus again. Production systems move to a persistent backend such as TiDB https://www.pingcap.com/what-is-tidb/ , which adds SQL, metadata filtering, and distributed scale. This tutorial walks the build path from document loading to a first query, using the LlamaIndex vector store documentation https://developers.llamaindex.ai/python/framework/module guides/storing/vector stores/ and the TiDB integration guide https://docs.pingcap.com/ai/vector-search-integrate-with-llamaindex/ as references. SQL matters here because retrieval rarely stays a pure similarity problem. You filter by tenant, source, or date, and you want those fields in the same system as the vectors. By the end you will have a working TiDBVectorStore, a metadata-filtered query, and a checklist for taking it to production. Key takeaways - The default LlamaIndex vector store lives in memory, so a restart wipes the index unless you persist it yourself. - Swapping in TiDB takes one StorageContext argument. The VectorStoreIndex code you already wrote stays the same. - Metadata filters narrow nearest-neighbor results by fields such as tenant or source, which keeps multi-user retrieval scoped correctly. - TiDBVectorStore supports only LlamaIndex’s default query mode, so keyword and hybrid retrieval need a separate SQL or pytidb path. - TiDB vector search https://docs.pingcap.com/ai/vector-search-overview/ runs on TiDB Cloud Starter https://www.pingcap.com/tidb-cloud-starter/ or TiDB Self-Managed https://www.pingcap.com/tidb/self-managed/ v8.4.0 and later v8.5.0 or later recommended . How to Build a LlamaIndex Vector Store You build a LlamaIndex vector store on TiDB in five steps: load documents, split them into nodes, create a TiDBVectorStore, wrap it in a StorageContext, and build a VectorStoreIndex from it. The index code matches the in-memory version. Only the storage context changes. The table below maps each piece of that flow to what LlamaIndex does by default and what a production setup replaces it with. | Component | Default behavior | Production alternative | TiDB role | |---|---|---|---| | Vector storage | In-memory store inside your Python process | Database-backed vector store | TiDBVectorStore writes embeddings to a TiDB table | | Persistence | Lost on exit unless you persist to local disk | Durable remote table | Data survives restarts and redeploys, and you can inspect it with SQL | | Metadata filters | Applied to in-process data | Applied by the backend during nearest-neighbor search | TiDBVectorStore accepts LlamaIndex MetadataFilters | | Multi-user access | One process owns the index | Shared store reached over the network | Many app instances connect to one cluster with a connection string | | Scale | Bounded by one machine’s RAM | Store that scales out | TiDB distributes rows across TiKV nodes, and vector indexes run on TiFlash replicas | You need three things before you start: Python 3.10 or later, an OpenAI API key for embeddings, and a TiDB cluster. The TiDB docs https://docs.pingcap.com/ai/vector-search-integrate-with-llamaindex/ recommend a TiDB Cloud Starter instance. TiDB Self-Managed also works on v8.4.0 or later, with v8.5.0 or later recommended. Install the packages: pip install llama-index llama-index-vector-stores-tidbvector Load Documents and Create Nodes LlamaIndex reads files into Document objects, then splits them into nodes, which are the chunks that get embedded and retrieved. Attach metadata at this stage, because every node inherits its parent document’s metadata. That is what makes tenant or document-type filters possible later. python import getpass import os from llama index.core import Settings, SimpleDirectoryReader from llama index.core.node parser import SentenceSplitter from llama index.embeddings.openai import OpenAIEmbedding Set the key before you create the embedding model. os.environ "OPENAI API KEY" = getpass.getpass "OpenAI API key: " Pin the embedding model so the vector dimension is known 1536 here . Settings.embed model = OpenAIEmbedding model="text-embedding-3-small" documents = SimpleDirectoryReader "./data/docs" .load data for document in documents: document.metadata "tenant" = "acme" document.metadata "doc type" = "runbook" splitter = SentenceSplitter chunk size=512, chunk overlap=50 nodes = splitter.get nodes from documents documents print f"{len documents } documents became {len nodes } nodes" Connect StorageContext to TiDBVectorStore Create the vector store with your connection string, then hand it to a StorageContext. The vector dimension value must match your embedding model. TiDB vector search accepts vectors of up to 16,383 dimensions https://docs.pingcap.com/ai/vector-search-limitations/ , so common embedding models fit. Copy the PyMySQL connection string from the Connect dialog of your TiDB Cloud Starter instance. python import getpass from llama index.core import StorageContext from llama index.vector stores.tidbvector import TiDBVectorStore Format: mysql+pymysql://