This package contains the LangChain.js integrations for Redis through their SDK.
npm install @langchain/redis @langchain/core
To develop the Redis package, you'll need to follow these instructions:
pnpm install
pnpm build
Or from the repo root:
pnpm build --filter @langchain/redis
Test files should live within a tests/ file in the src/ folder. Unit tests should end in .test.ts and integration tests should
end in .int.test.ts:
$ pnpm test
$ pnpm test:int
Run the linter & formatter to ensure your code is up to standard:
pnpm lint && pnpm format
If you add a new file to be exported, either import & re-export from src/index.ts, or add it to the exports field in the package.json file and run pnpm build to generate the new entrypoint.
The FluentRedisVectorStore is the recommended approach for new projects. It provides a more powerful and type-safe filtering API with support for complex metadata queries. This guide helps you migrate from the legacy RedisVectorStore to FluentRedisVectorStore.
| Feature | RedisVectorStore | FluentRedisVectorStore |
|---|---|---|
| Metadata Schema Definition | Record<string, CustomSchemaField> |
MetadataFieldSchema[] |
| Inferred Metadata Schema | No, only custom schema supported | Yes, based on metadata when adding documents |
| Pre-filter - Definition | String arrays or raw query strings | Type-safe FilterExpression objects |
| Pre-filter - Nested conditions | All filters joined by single AND condition | AND, OR, nesting supported |
| Pre-filter - conditions types | Numeric, Tag and Text | Numeric, Tag, Text, Geo, Timestamp |
| Metadata Storage | JSON blob + optional indexed fields | Individual indexed fields (no JSON blob) |
Before (RedisVectorStore):
import { RedisVectorStore } from "@langchain/redis";
After (FluentRedisVectorStore):
import { FluentRedisVectorStore, Tag, Num, Text, Geo } from "@langchain/redis";
The schema format has changed from an object-based to an array-based structure.
Before (RedisVectorStore):
const customSchema = {
userId: { type: SchemaFieldTypes.TAG, required: true },
price: { type: SchemaFieldTypes.NUMERIC, SORTABLE: true },
description: { type: SchemaFieldTypes.TEXT },
location: { type: SchemaFieldTypes.GEO },
};
After (FluentRedisVectorStore):
const customSchema = [
{ name: "userId", type: "tag" },
{ name: "price", type: "numeric", options: { sortable: true } },
{ name: "description", type: "text" },
{ name: "location", type: "geo" },
];
Before:
const vectorStore = await RedisVectorStore.fromDocuments(
documents,
embeddings,
{
redisClient: client,
indexName: "products",
customSchema: {
category: { type: SchemaFieldTypes.TAG },
price: { type: SchemaFieldTypes.NUMERIC, SORTABLE: true },
},
}
);
After:
const vectorStore = await FluentRedisVectorStore.fromDocuments(
documents,
embeddings,
{
redisClient: client,
indexName: "products",
customSchema: [
{ name: "category", type: "tag" },
{ name: "price", type: "numeric", options: { sortable: true } },
],
}
);
The filtering API has changed significantly. Instead of passing metadata objects or string arrays, you now use fluent filter expressions.
Before (RedisVectorStore):
// Simple metadata filtering
const results = await vectorStore.similaritySearchVectorWithScoreAndMetadata(
queryVector,
5,
{ category: "electronics", price: { min: 100, max: 1000 } }
);
// Or with string-based filters
const results = await vectorStore.similaritySearchVectorWithScore(
queryVector,
5,
["electronics", "gadgets"]
);
After (FluentRedisVectorStore):
// Custom filter expression with the fluent API
const results = await vectorStore.similaritySearchVectorWithScore(
queryVector,
5,
Tag("category").eq("electronics").and(Num("price").between(100, 1000))
);
// Basic filter expression with the fluent API
const results = await vectorStore.similaritySearchVectorWithScore(
queryVector,
5,
Tag("metadata").eq("electronics", "gadgets")
);
The FluentRedisVectorStore only supports metadata stored in individual fields, alongside the vector data and content data.
It is not compatible with the implementation of the RedisVectorStore which stores metadata as a JSON blob in a single field.
The custom schema option of the RedisVectorStore could be migrated to the FluentRedisVectorStore following the instructions in step 2.
To avoid ambiguous results, it's recommended to create a new index with the updated schema and migrate data.
Replace all instances of RedisVectorStore with FluentRedisVectorStore and update filter usage:
Before:
async function searchProducts(query: string, category?: string) {
const results = await vectorStore.similaritySearchVectorWithScoreAndMetadata(
await embeddings.embedQuery(query),
5,
category ? { category } : undefined
);
return results;
}
After:
async function searchProducts(query: string, category?: string) {
const filter = category ? Tag("category").eq(category) : undefined;
const results = await vectorStore.similaritySearchVectorWithScore(
await embeddings.embedQuery(query),
5,
filter
);
return results;
}Logical AND filter for combining multiple filter conditions.
Combines two filter expressions with AND logic. In RediSearch, this is represented by space-separated conditions within parentheses.
Custom filter for providing raw RediSearch query syntax.
This filter allows you to provide a custom RediSearch query string that will be used as-is without any modification. This is useful when you n
Base class for all filter expressions.
All filter types extend this class and implement the toString() method
to generate the appropriate RediSearch query syntax.
Advanced Redis Vector Store with structured metadata filtering.
This class provides advanced filtering capabilities through FilterExpression and requires explicit MetadataFieldSchema definition. It s
Geographic filter for location-based searches.
Geo fields in Redis support radius-based geographic queries. They store coordinates as longitude,latitude pairs and allow filtering based on distance fr
Numeric filter for range and exact matching on numeric fields.
Numeric fields in Redis support range queries and exact matching on numerical values. They use interval notation where square brackets `
Logical OR filter for combining alternative filter conditions.
Combines two filter expressions with OR logic. In RediSearch, this is represented by pipe-separated conditions within parentheses.
Class for storing chat message history using Redis. Extends the
BaseListChatMessageHistory class.
Tag filter for exact matching on tag fields.
Tag fields in Redis are used for exact-match filtering on categorical data. They support efficient filtering on multiple values using OR logic within the
Text filter for full-text search on text fields.
Text fields in Redis support various types of text matching including exact phrases, wildcard patterns, and fuzzy matching. Text fields are tokenized
Timestamp filter for date/time-based searches.
Important: In Redis, there is no separate "timestamp" field type. Timestamps are stored as NUMERIC fields containing Unix epoch timestamps (seconds
Class representing a RedisVectorStore. It extends the VectorStore class and includes methods for adding documents and vectors, performing similarity searches, managing the index, and more.
Builds a RediSearch schema from metadata field definitions.
This function builds up a schema based on the metadata field schema definitions.
Checks if two metadata schemas have a mismatch.
This function compares two metadata schema arrays to determine if they contain the same fields with matching types. The comparison is order-independent
Converts legacy CustomSchemaField format to new MetadataFieldSchema format.
This function provides backward compatibility by converting the old Record-based schema format to the new array-based forma
Create a custom filter with raw RediSearch query syntax.
This is a convenience function for creating custom filters that use raw RediSearch query syntax. The provided query string will be used as-is
Deserializes metadata field values from Redis storage based on field type.
Converts Redis-stored values back to JavaScript types:
Create a geographic filter for location-based searches.
This is a convenience function that provides a fluent API for building geo filters. Geo filters support radius-based geographic queries using l
Infers metadata schema from a collection of documents by analyzing their metadata fields.
This function examines the metadata of all provided documents and attempts to infer the appropriate field typ
Create a numeric filter for range and exact matching on numeric fields.
This is a convenience function that provides a fluent API for building numeric filters. Numeric filters support range queries a
Serializes metadata field values for storage in Redis based on field type.
Converts JavaScript values to the appropriate format for Redis storage:
Create a tag filter for exact matching on tag fields.
This is a convenience function that provides a fluent API for building tag filters. Tag filters are used for exact-match categorical filtering.
Create a text filter for full-text search on text fields.
This is a convenience function that provides a fluent API for building text filters. Text filters support exact phrases, wildcard patterns, f
Create a timestamp filter for date/time-based searches.
This is a convenience function that provides a fluent API for building timestamp filters. Timestamp filters work with Date objects or Unix epoc