LangChain Redis Integration

raw JSON →
0.2.5 verified Mon Apr 27 auth: no python

LangChain Redis provides integrations for using Redis as a vector store, chat message history, cache, and semantic cache with LangChain. Version 0.2.5 supports LangChain v1 and v2, uses redis-py 6.x and redisvl 0.4+. Compatible with Python 3.10-3.13. Released under MIT license by LangChain.

pip install langchain-redis
error ModuleNotFoundError: No module named 'langchain_redis'
cause Package not installed or import name mismatch (old import path `langchain_redis` vs `langchain.redis`).
fix
Run pip install langchain-redis and use from langchain_redis import RedisVectorStore.
error redis.exceptions.ResponseError: Unknown index name
cause The specified Redis index does not exist or was not created before querying.
fix
Ensure the index is created by calling vector_store.create_index() or using add_texts first. Verify index name spelling.
error pydantic.errors.PydanticInvalidForJsonSchema
cause Using Pydantic v1 custom schema with langchain-redis v0.1.0+ which requires Pydantic v2.
fix
Migrate custom schemas to Pydantic v2 syntax. See Pydantic migration guide.
breaking Upgraded from Pydantic v1 to v2 in v0.1.0. Custom schemas using Pydantic v1 model validators may break.
fix Update custom schemas to Pydantic v2 syntax (e.g., model_validator instead of root_validator).
breaking Import paths restructured in v0.2.0. Top-level imports like `from langchain_redis import RedisVectorStore` now required; deep imports (e.g., `from langchain_redis.vectorstores import RedisVectorStore`) removed.
fix Use top-level imports from `langchain_redis`.
deprecated RedisConfig.index_schema initialization changed in v0.1.0. Setting index_schema directly is deprecated; use `add_texts` or `create_index` with schema instead.
fix Pass schema via `add_texts` method or use `RedisVectorStore.from_texts`.
gotcha RedisVectorStore.add_texts ignores the `ids` parameter if provided. This is a known bug fixed in v0.2.2.
fix Upgrade to v0.2.2 or later, or use `add_documents` with Document objects that have explicit IDs.
pip install langchain-redis[all]

Initializes a RedisVectorStore, adds documents, and performs similarity search. Requires a running Redis instance and OpenAI API key set as OPENAI_API_KEY.

import os
from langchain_redis import RedisVectorStore, RedisConfig
from langchain_core.documents import Document
from langchain_openai import OpenAIEmbeddings

redis_url = os.environ.get('REDIS_URL', 'redis://localhost:6379')
embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")

config = RedisConfig(redis_url=redis_url)
vector_store = RedisVectorStore(config, embeddings)

docs = [
    Document(page_content="LangChain is a framework for LLM apps", metadata={"category": "ai"}),
    Document(page_content="Redis is an in-memory data store", metadata={"category": "database"})
]
vector_store.add_documents(docs)
results = vector_store.similarity_search("LLM framework", k=1)
print(results[0].page_content)