RedisProvider Class

Redis context provider with dynamic, filterable schema.

Stores context in Redis and retrieves scoped context. Uses full-text or optional hybrid vector search to ground model responses.

Create a Redis Context Provider.

Constructor

RedisProvider()

Parameters

Name Description
redis_url

The Redis server URL.

Default value: redis://localhost:6379
index_name

The name of the Redis index.

Default value: context
prefix

The prefix for all keys in the Redis database.

Default value: context
redis_vectorizer

The vectorizer to use for Redis.

Default value: None
vector_field_name

The name of the vector field in Redis.

Default value: None
vector_algorithm

The algorithm to use for vector search.

Default value: None
vector_distance_metric

The distance metric to use for vector search.

Default value: None
application_id

The application ID to scope the context.

Default value: None
agent_id

The agent ID to scope the context.

Default value: None
user_id

The user ID to scope the context.

Default value: None
thread_id

The thread ID to scope the context.

Default value: None
scope_to_per_operation_thread_id

Whether to scope to the per-operation thread ID.

Default value: False
context_prompt

The context prompt to use for the provider.

Default value: ## Memories Consider the following memories when answering user questions:
redis_index

The Redis index to use for the provider.

Default value: None
overwrite_index

Whether to overwrite the existing Redis index.

Default value: False

Methods

__init__

Create a Redis Context Provider.

__new__
invoked
invoking

Called before invoking the model to provide scoped context.

Concatenates recent messages into a query, fetches matching memories from Redis. Prepends them as instructions.

search_all

Returns all documents in the index.

Streams results via pagination to avoid excessive memory and response sizes.

thread_created

Called when a new thread is created.

Captures the per-operation thread id when scoping is enabled to enforce single-thread usage.

__init__

Create a Redis Context Provider.

__init__(redis_url: str = 'redis://localhost:6379', index_name: str = 'context', prefix: str = 'context', redis_vectorizer: BaseVectorizer | None = None, vector_field_name: str | None = None, vector_algorithm: Literal['flat', 'hnsw'] | None = None, vector_distance_metric: Literal['cosine', 'ip', 'l2'] | None = None, application_id: str | None = None, agent_id: str | None = None, user_id: str | None = None, thread_id: str | None = None, scope_to_per_operation_thread_id: bool = False, context_prompt: str = ContextProvider.DEFAULT_CONTEXT_PROMPT, redis_index: Any = None, overwrite_index: bool = False)

Parameters

Name Description
redis_url
str

The Redis server URL.

Default value: "redis://localhost:6379"
index_name
str

The name of the Redis index.

Default value: "context"
prefix
str

The prefix for all keys in the Redis database.

Default value: "context"
redis_vectorizer
<xref:redisvl.utils.vectorize.base.BaseVectorizer> | None

The vectorizer to use for Redis.

Default value: None
vector_field_name
str | None

The name of the vector field in Redis.

Default value: None
vector_algorithm
Literal['flat', 'hnsw'] | None

The algorithm to use for vector search.

Default value: None
vector_distance_metric
Literal['cosine', 'ip', 'l2'] | None

The distance metric to use for vector search.

Default value: None
application_id
str | None

The application ID to scope the context.

Default value: None
agent_id
str | None

The agent ID to scope the context.

Default value: None
user_id
str | None

The user ID to scope the context.

Default value: None
thread_id
str | None

The thread ID to scope the context.

Default value: None
scope_to_per_operation_thread_id

Whether to scope to the per-operation thread ID.

Default value: False
context_prompt
str

The context prompt to use for the provider.

Default value: ContextProvider.DEFAULT_CONTEXT_PROMPT
redis_index
Any

The Redis index to use for the provider.

Default value: None
overwrite_index

Whether to overwrite the existing Redis index.

Default value: False

__new__

__new__(**kwargs)

invoked

async invoked(request_messages: ChatMessage | Sequence[ChatMessage], response_messages: ChatMessage | Sequence[ChatMessage] | None = None, invoke_exception: Exception | None = None, **kwargs: Any) -> None

Parameters

Name Description
request_messages
Required
response_messages
Default value: None
invoke_exception
Default value: None
kwargs
Required
Any

Returns

Type Description

invoking

Called before invoking the model to provide scoped context.

Concatenates recent messages into a query, fetches matching memories from Redis. Prepends them as instructions.

async invoking(messages: ChatMessage | MutableSequence[ChatMessage], **kwargs: Any) -> Context

Parameters

Name Description
messages
Required

List of new messages in the thread.

kwargs
Required
Any

Keyword-Only Parameters

Name Description
**kwargs

not used at present at present.

Returns

Type Description

Context object containing instructions with memories.

search_all

Returns all documents in the index.

Streams results via pagination to avoid excessive memory and response sizes.

async search_all(page_size: int = 200) -> list[dict[str, Any]]

Parameters

Name Description
page_size
int

Page size used for pagination under the hood.

Default value: 200

Returns

Type Description

List of all documents.

thread_created

Called when a new thread is created.

Captures the per-operation thread id when scoping is enabled to enforce single-thread usage.

async thread_created(thread_id: str | None) -> None

Parameters

Name Description
thread_id
Required
str | None

The ID of the thread or None.

Returns

Type Description

Attributes

schema_dict

Get the Redis schema dictionary, computing and caching it on first access.

DEFAULT_CONTEXT_PROMPT

DEFAULT_CONTEXT_PROMPT: Final[str] = '## Memories\nConsider the following memories when answering user questions:'