{
  "schema_version": 2,
  "id": "integrate/llamaindex",
  "title": "Redis with LlamaIndex",
  "url": "https://redis.io/docs/latest/integrate/llamaindex/",
  "summary": "Use Redis as a LlamaIndex vector store, chat store, document store, index store, and ingestion cache.",
  "tags": [
    "docs",
    "integrate",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-11-23T13:52:33Z",
  "children": [],
  "page_type": "content",
  "content_hash": "c8e1e2e179e14c2a0a0a9e0b228fe9dc44009303fb48a431918d42d65a7be73a",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "[LlamaIndex](https://developers.llamaindex.ai/) is a Python framework for building retrieval-augmented generation (RAG) applications over your own data. Separate Redis packages plug into it:\n\n| Package | Use it to |\n|---------|-----------|\n| `llama-index-vector-stores-redis` | Store embeddings and run vector searches with metadata filters, using [Redis Search](https://redis.io/docs/latest/develop/ai/search-and-query). |\n| `llama-index-storage-chat-store-redis` | Keep chat history for each user or session. |\n| `llama-index-storage-docstore-redis` and `llama-index-storage-index-store-redis` | Persist the document and index metadata that LlamaIndex otherwise keeps in memory. |\n| `llama-index-storage-kvstore-redis` | Cache the results of ingestion pipeline steps. |\n\nSee the [semantic cache](https://redis.io/docs/latest/develop/use-cases/semantic-cache) and\n[memory layer](https://redis.io/docs/latest/develop/use-cases/memory-layer) use cases for more\ninformation about when to use these patterns."
    },
    {
      "id": "requirements",
      "title": "Requirements",
      "role": "content",
      "text": "- Python 3.11 to 3.13\n- Redis with [Redis Search](https://redis.io/docs/latest/develop/ai/search-and-query) for the vector store\n(Redis 8 and later versions include it but earlier versions need Redis Stack). The chat store, document store, index store, and cache work on any Redis server.\n\nInstall the packages you need:\n\n[code example]\n\nThe examples on this page were run with LlamaIndex Core 0.14.26 and Redis 8.10. They use LlamaIndex's default OpenAI models, so set `OPENAI_API_KEY` or configure [`Settings.embed_model`](https://developers.llamaindex.ai/python/framework/module_guides/models/embeddings/) and `Settings.llm` to your own providers."
    },
    {
      "id": "search-embeddings-with-the-vector-store",
      "title": "Search embeddings with the vector store",
      "role": "content",
      "text": "Create a `RedisVectorStore`, wrap it in a `StorageContext`, and build an index:\n\n[code example]\n\nLlamaIndex embeds each document, stores it in a Redis hash, and indexes the hashes with a Redis Search index. `overwrite=True` drops and recreates the index if it already exists.\n\nTo reuse the index in a later run without embedding the documents again, connect to the same index and build from the vector store:\n\n[code example]"
    },
    {
      "id": "define-a-custom-schema",
      "title": "Define a custom schema",
      "role": "content",
      "text": "The default schema has an index named `llama_index`, a key prefix of `llama_index/vector`, and a vector field of **1536** dimensions, the size of OpenAI `text-embedding-ada-002` and `text-embedding-3-small` vectors. An embedding model with a different size fails when you add documents:\n\n[code example]\n\nPass your own schema to set the vector size, the index name and prefix, the vector algorithm, and the metadata fields that you want to filter on:\n\n[code example]\n\nThe schema must contain the `id`, `doc_id`, `text`, and `vector` fields. `RedisVectorStore` uses the [RedisVL](https://redis.io/docs/latest/develop/ai/redisvl) `IndexSchema` class, which is installed with the package. See [Get started with RedisVL](https://redis.io/docs/latest/develop/ai/redisvl/user_guide/getting_started) for the field types and attributes."
    },
    {
      "id": "filter-on-metadata",
      "title": "Filter on metadata",
      "role": "content",
      "text": "Add a field to the schema for each metadata key that you want to filter on (such as `topic` in the\nexample above). Then pass a `MetadataFilters` object to the retriever:\n\n[code example]\n\nIf a filter names a key that is not in the schema, the query returns no results. LlamaIndex logs `One or more requested filter fields are not present in the Redis index schema` but doesn't raise an error."
    },
    {
      "id": "keep-chat-history",
      "title": "Keep chat history",
      "role": "content",
      "text": "`RedisChatStore` keeps each conversation as a Redis list, keyed by a name you choose. Pass it to a memory buffer to give a chat engine or agent a history that survives a restart:\n\n[code example]\n\nThe \"time to live\" (`ttl`) is in seconds. This is the time (since the last message) after which Redis\nexpires the whole conversation. This is useful for automatically cleaning up idle sessions.\n\n\nThe `RedisChatStore.get_keys()` method runs [`KEYS *`](https://redis.io/docs/latest/commands/keys), which returns\nevery key in the database, not only conversation \nkeys. It also blocks the server while it scans. To avoid performance problems, use a dedicated database\nfor the chat store, and keep your own list of conversation keys instead of calling\n`RedisChatStore.get_keys()`."
    },
    {
      "id": "persist-documents-and-indexes",
      "title": "Persist documents and indexes",
      "role": "content",
      "text": "By default, LlamaIndex keeps the document store and index store in memory and writes them to disk. To keep them in Redis instead, pass Redis-backed stores to the `StorageContext`:\n\n[code example]\n\nBoth stores prefix their keys with `namespace`, so several applications can share a database. A new `StorageContext` with the same namespace reads the stored documents and index metadata back."
    },
    {
      "id": "cache-ingestion-work",
      "title": "Cache ingestion work",
      "role": "content",
      "text": "An `IngestionPipeline` runs transformations such as splitting and embedding. If you supply\nit with an `IngestionCache` backed by `RedisKVStore`, as shown in the example below,\nit will skip any step whose input hasn't changed, even in a later process:\n\n[code example]\n\nReplace `embed_model` with your embedding model. The cache is a Redis hash named after `collection`. On a second run with the same documents, the pipeline reads the embeddings from Redis and makes no embedding calls."
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "- [LlamaIndex Redis vector store guide](https://developers.llamaindex.ai/python/examples/vector_stores/redisindexdemo/)\n- [RedisVL](https://redis.io/docs/latest/develop/ai/redisvl), the Redis library that `RedisVectorStore` is built on\n- [Vector search with Redis Search](https://redis.io/docs/latest/develop/ai/search-and-query/vectors)"
    }
  ],
  "examples": [
    {
      "id": "requirements-ex0",
      "language": "bash",
      "code": "pip install llama-index-vector-stores-redis \\\n    llama-index-storage-chat-store-redis \\\n    llama-index-storage-docstore-redis \\\n    llama-index-storage-index-store-redis \\\n    llama-index-storage-kvstore-redis",
      "section_id": "requirements"
    },
    {
      "id": "search-embeddings-with-the-vector-store-ex0",
      "language": "python",
      "code": "from llama_index.core import Document, StorageContext, VectorStoreIndex\nfrom llama_index.vector_stores.redis import RedisVectorStore\n\nvector_store = RedisVectorStore(redis_url=\"redis://localhost:6379\", overwrite=True)\nstorage_context = StorageContext.from_defaults(vector_store=vector_store)\n\ndocuments = [\n    Document(text=\"Redis is an in-memory data store.\"),\n    Document(text=\"Llamas live in the Andes.\"),\n]\nindex = VectorStoreIndex.from_documents(documents, storage_context=storage_context)\n\nretriever = index.as_retriever(similarity_top_k=1)\nfor result in retriever.retrieve(\"What is Redis?\"):\n    print(result.score, result.text)",
      "section_id": "search-embeddings-with-the-vector-store"
    },
    {
      "id": "search-embeddings-with-the-vector-store-ex1",
      "language": "python",
      "code": "vector_store = RedisVectorStore(redis_url=\"redis://localhost:6379\")\nindex = VectorStoreIndex.from_vector_store(vector_store)",
      "section_id": "search-embeddings-with-the-vector-store"
    },
    {
      "id": "define-a-custom-schema-ex0",
      "language": "text",
      "code": "ValueError: Attempting to index embeddings of dim 8 which doesn't match the index schema expectation of 1536.",
      "section_id": "define-a-custom-schema"
    },
    {
      "id": "define-a-custom-schema-ex1",
      "language": "python",
      "code": "from redisvl.schema import IndexSchema\n\nschema = IndexSchema.from_dict({\n    \"index\": {\"name\": \"docs_idx\", \"prefix\": \"doc\", \"key_separator\": \":\"},\n    \"fields\": [\n        {\"name\": \"id\", \"type\": \"tag\"},\n        {\"name\": \"doc_id\", \"type\": \"tag\"},\n        {\"name\": \"text\", \"type\": \"text\"},\n        {\"name\": \"topic\", \"type\": \"tag\"},\n        {\n            \"name\": \"vector\",\n            \"type\": \"vector\",\n            \"attrs\": {\"dims\": 1536, \"algorithm\": \"hnsw\", \"distance_metric\": \"cosine\"},\n        },\n    ],\n})\n\nvector_store = RedisVectorStore(schema=schema, redis_url=\"redis://localhost:6379\")",
      "section_id": "define-a-custom-schema"
    },
    {
      "id": "filter-on-metadata-ex0",
      "language": "python",
      "code": "from llama_index.core.vector_stores.types import MetadataFilter, MetadataFilters\n\nindex = VectorStoreIndex.from_documents(\n    [\n        Document(text=\"Redis is an in-memory data store.\", metadata={\"topic\": \"redis\"}),\n        Document(text=\"Llamas live in the Andes.\", metadata={\"topic\": \"animals\"}),\n    ],\n    storage_context=StorageContext.from_defaults(vector_store=vector_store),\n)\n\nfilters = MetadataFilters(filters=[MetadataFilter(key=\"topic\", value=\"redis\")])\nretriever = index.as_retriever(filters=filters)",
      "section_id": "filter-on-metadata"
    },
    {
      "id": "keep-chat-history-ex0",
      "language": "python",
      "code": "from llama_index.core.llms import ChatMessage\nfrom llama_index.core.memory import ChatMemoryBuffer\nfrom llama_index.storage.chat_store.redis import RedisChatStore\n\nchat_store = RedisChatStore(redis_url=\"redis://localhost:6379\", ttl=3600)\n\nmemory = ChatMemoryBuffer.from_defaults(\n    token_limit=3000,\n    chat_store=chat_store,\n    chat_store_key=\"user-1\",\n)\nmemory.put(ChatMessage(role=\"user\", content=\"Hello\"))\nmemory.put(ChatMessage(role=\"assistant\", content=\"Hi there\"))\nprint([m.content for m in memory.get()])",
      "section_id": "keep-chat-history"
    },
    {
      "id": "persist-documents-and-indexes-ex0",
      "language": "python",
      "code": "from llama_index.storage.docstore.redis import RedisDocumentStore\nfrom llama_index.storage.index_store.redis import RedisIndexStore\n\nstorage_context = StorageContext.from_defaults(\n    docstore=RedisDocumentStore.from_host_and_port(\"localhost\", 6379, namespace=\"demo\"),\n    index_store=RedisIndexStore.from_host_and_port(\"localhost\", 6379, namespace=\"demo\"),\n)",
      "section_id": "persist-documents-and-indexes"
    },
    {
      "id": "cache-ingestion-work-ex0",
      "language": "python",
      "code": "from llama_index.core.ingestion import IngestionCache, IngestionPipeline\nfrom llama_index.core.node_parser import SentenceSplitter\nfrom llama_index.storage.kvstore.redis import RedisKVStore\n\ncache = IngestionCache(\n    cache=RedisKVStore.from_host_and_port(\"localhost\", 6379),\n    collection=\"ingestion_cache\",\n)\n\npipeline = IngestionPipeline(\n    transformations=[SentenceSplitter(chunk_size=512), embed_model],\n    cache=cache,\n)\nnodes = pipeline.run(documents=documents)",
      "section_id": "cache-ingestion-work"
    }
  ]
}
