Beanis is an async object-document mapper for Redis, built on Pydantic. You define a model, and Beanis stores each instance as a Redis hash, keeps secondary indexes up to date, and gives you typed objects back.
It also does vector search on the vector sets that ship with Redis 8, and it includes long-term memory for AI agents, usable from Python, from the beanis command line, or as an MCP server for Claude Code and other agents.
It works on plain Redis. No RedisJSON, RediSearch or Redis Stack.
import asyncio
from pydantic import BaseModel
from redis.asyncio import Redis
from beanis import Document, Indexed, init_beanis
class Category(BaseModel):
name: str
description: str
class Product(Document):
name: str
price: Indexed(float) # sorted-set index: range queries
category: Indexed(str) # set index: exact match
tags: list[str] = []
stock: int = 0
class Settings:
key_prefix = "Product"
async def main():
redis = Redis(decode_responses=True)
await init_beanis(database=redis, document_models=[Product])
bar = Product(name="Dark chocolate", price=5.95, category="chocolate", stock=100)
await bar.insert()
same = await Product.get(bar.id)
cheap = await Product.find(price__lt=10)
chocolate = await Product.find(category="chocolate", price__gte=5)
await bar.update(price=6.50)
await bar.increment_field("stock", -1)
await bar.delete_self()
asyncio.run(main())
decode_responses=True is required. Each document is the hash Product:<id>; nested models, lists, dicts, datetime, Decimal, UUID and enums are encoded for you and decoded on read.
Queries
find() takes keyword filters on indexed fields. Several filters are combined with AND.
await Product.find(category="chocolate") # exact match
await Product.find(price__gte=10, price__lte=50) # range: __gte, __lte, __gt, __lt
await Product.find(price=12.0) # exact match on a number
await Product.all(skip=0, limit=20) # every document, by insertion time
await Product.count()
Filtering on a field that is not indexed raises ValueError. Sorting by other fields and full-text search are not built in; sort the returned list in Python, or use Redis's query engine directly if you need them.
Declare indexes with Indexed(type) or Annotated[type, IndexedField()]. Numbers get a sorted set, strings and other scalars get a set, GeoPoint gets a geo index (see the geo tutorial).
Vector search
Embeddings live in a Redis 8 vector set (VADD / VSIM), next to the document's short fields so you can filter a search:
from typing import List
from typing_extensions import Annotated
from beanis import Document, IndexedField, VectorField
class Chunk(Document):
text: str
source: Annotated[str, IndexedField()]
embedding: Annotated[List[float], VectorField(dimensions=384)]
await Chunk(text="Refunds take 5 days", source="faq", embedding=embed("Refunds take 5 days")).insert()
for chunk, score in await Chunk.vector_search(embed("how long is a refund?"), k=3, filter='.source == "faq"'):
print(round(score, 3), chunk.text) # score: cosine similarity in [0, 1]
embed is any function from text to a list of floats. Vectors use HNSW with int8 quantization by default (VectorField(quantization="NOQUANT") keeps float32). Updating or deleting a document updates its vector. init_beanis raises at startup if a model has vector fields and the server has no vector sets (Redis 7, Valkey).