> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-docsdy-1782337909-2539eb6.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Redis integration

> Integrate with the Redis vector store using LangChain Python.

> [Redis](https://redis.io/docs/latest/) is an in-memory data platform with vector search, full-text search, and semantic caching capabilities, designed for real-time AI applications.

This notebook shows how to use functionality related to the `RedisVectorStore`.

## Setup

To use the `RedisVectorStore` you first need to install the partner package, as well as the other packages used throughout this notebook.

```bash theme={null}
pip install -qU langchain-redis langchain-openai
```

You'll need a running Redis instance with Redis Search features enabled. We recommend Redis 8.4+. You can start one locally with Docker:

```bash theme={null}
docker run -d --name redis -p 6379:6379 redis:8.4
```

Or use [Redis Cloud](https://redis.io/cloud/) for a managed deployment.

(Optional) Download [RedisInsight](https://redis.io/insight/) — a browser-based GUI for inspecting your data and indexes.

```bash theme={null}
docker run -d --name redisinsight -p 5540:5540 redis/redisinsight:latest
```

### Credentials

Configure your Redis connection by setting the following environment variable:

```bash theme={null}
export REDIS_URL="redis://localhost:6379"
```

For Redis with authentication:

```bash theme={null}
export REDIS_URL="redis://username:password@localhost:6379"
```

For Redis Cloud, use the connection string from your database dashboard:

```bash theme={null}
export REDIS_URL="redis://default:<password>@<host>:<port>"
```

This package also supports SSL/TLS (`rediss://`) and high-availability Redis Sentinel deployments (`redis+sentinel://`). See the [langchain-redis README](https://github.com/langchain-ai/langchain-redis/blob/main/libs/redis/README.md#redis-connection-options) for full connection options.

Set your OpenAI API key:

```bash theme={null}
export OPENAI_API_KEY="your-api-key-here"
```

<Tip>
  The [`langchain-redis`](https://github.com/langchain-ai/langchain-redis/blob/main/libs/redis/README.md) package also provides `RedisChatMessageHistory` for conversation memory and `RedisSemanticCache` for LLM response caching — all backed by the same Redis instance as your vector store.
</Tip>

## Initialization

```python theme={null}
import os
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
```

```python theme={null}
from langchain_redis import RedisConfig, RedisVectorStore

config = RedisConfig(
    index_name="my_vectors",
    redis_url=os.getenv("REDIS_URL", "redis://localhost:6379"),
    distance_metric="COSINE",  # Options: COSINE, L2, IP
    metadata_schema=[
        {"name": "baz", "type": "tag"},
        {"name": "foo", "type": "tag"},
    ]
)

vector_store = RedisVectorStore(embeddings=embeddings, config=config)
```

<Note>
  The `metadata_schema` parameter tells Redis which metadata fields to index. Fields not listed here cannot be used in filters. Use `"tag"` for categorical string values and `"numeric"` for numbers.
</Note>

## Manage vector store

### Add items to vector store

```python theme={null}
texts = ["foo", "bar"]
metadatas = [{"baz": "bar"}, {"foo": "baz"}]
vector_store.add_texts(texts, metadatas=metadatas)
```

You can also add documents with custom keys:

```python theme={null}
custom_keys = ["doc1", "doc2"]
vector_store.add_texts(texts, metadatas=metadatas, keys=custom_keys)
```

Or use `add_documents` with LangChain `Document` objects:

```python theme={null}
from langchain_core.documents import Document

document_1 = Document(page_content="foo", metadata={"baz": "bar"})
document_2 = Document(page_content="bar", metadata={"foo": "baz"})
document_3 = Document(page_content="to be deleted")

documents = [document_1, document_2, document_3]
ids = ["1", "2", "3"]
vector_store.add_documents(documents=documents, ids=ids)
```

### Delete items from vector store

```python theme={null}
vector_store.delete(ids=["3"])
```

## Query vector store

Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.

### Query directly

Performing a simple similarity search can be done as follows:

```python theme={null}
query = "foo"
docs = vector_store.similarity_search(query, k=2)
for doc in docs:
    print(f"* {doc.page_content} [{doc.metadata}]")
```

#### Similarity search with score

```python theme={null}
docs_and_scores = vector_store.similarity_search_with_score(query, k=2)
for doc, score in docs_and_scores:
    print(f"* [SIM={score:.3f}] {doc.page_content} [{doc.metadata}]")
```

#### Similarity search with filtering

```python theme={null}
from redisvl.query.filter import Tag

results = vector_store.similarity_search(
    query="foo",
    k=1,
    filter=Tag("baz") == "bar"
)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
```

#### Maximum marginal relevance search

```python theme={null}
docs = vector_store.max_marginal_relevance_search(query, k=2, fetch_k=10)
for doc in docs:
    print(f"* {doc.page_content} [{doc.metadata}]")
```

#### Other search methods

There are more search methods not listed in this notebook, to find all of them be sure to read the [`API reference`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore).

<Tip>
  Redis supports hybrid search combining vector similarity with full-text search. See the [`API reference`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore) for `similarity_search` parameters including `return_all` and `distance_threshold`.
</Tip>

### Query by turning into retriever

You can also transform the vector store into a retriever for easier usage in your chains.

```python theme={null}
retriever = vector_store.as_retriever(
    search_type="mmr",
    search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
)
retriever.invoke("your query here")
```

## Usage for retrieval-augmented generation

For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:

* [Retrieval docs](/oss/python/langchain/retrieval)
* [Build a RAG app with LangChain](/oss/python/langchain/rag)
* [Agentic RAG](/oss/python/langgraph/agentic-rag)

For a full RAG walkthrough using `langchain-redis`, see this [example notebook](https://github.com/redis-developer/redis-ai-resources/blob/main/python-recipes/RAG/02_langchain.ipynb).

***

## API reference

For detailed documentation of `RedisVectorStore` features and configurations head to the [`API reference`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore).

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/vectorstores/redis.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
