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.
Documentation · PyPI · Changelog
Install
pip install beanis # the ODM
pip install "beanis[mcp]" # plus agent memory: CLI embeddings and MCP server (Python 3.10+)
You need a Redis server. Redis 5+ is enough for documents and indexes; vector search and memory need Redis 8.0+:
docker run -d -p 6379:6379 redis:8 # or: brew install redis
Quick start
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).
Memory for agents
Memory stores what an agent should keep between conversations and finds it again by meaning. Memories are scoped by namespace, user, session and kind; a new memory that is nearly identical to an old one replaces it instead of piling up.
from beanis import Memory
from beanis.memory import fastembed_embedder
memory = Memory(redis, embed=fastembed_embedder(), namespace="support-bot")
await memory.remember("Prefers answers in Italian", user_id="u42", kind="preference")
for record, score in await memory.recall("which language should I answer in?", user_id="u42"):
print(round(score, 3), record.text)
await memory.recent(user_id="u42")
await memory.forget(record.id)
fastembed_embedder() runs BAAI/bge-small-en-v1.5 locally on the CPU (no API key). Any embedding function works, sync or async; use the same one for every writer of a namespace.
From the command line
pip install "beanis[mcp]" installs a beanis command. Every command prints JSON, so shell scripts and coding agents can use it directly.
beanis doctor # checks Redis, vector sets, extras, stored memory
beanis memory remember "Deploys go out on Tuesdays" --kind decision
beanis memory recall "when do we deploy?" -k 3
beanis memory recent --limit 5
beanis memory forget <id>
Configuration is by environment: REDIS_URL (default redis://localhost:6379/0, or pass --url), BEANIS_MEMORY_NAMESPACE (default default), BEANIS_EMBED_MODEL (default BAAI/bge-small-en-v1.5).
As an MCP server
beanis-mcp (or beanis mcp) serves the same memory over MCP stdio with four tools: remember, recall, forget, recent.
Claude Code, as a plugin (MCP server plus a skill that teaches Claude the Beanis API):
claude plugin marketplace add andreim14/beanis
claude plugin install beanis@beanis
The plugin starts the server with uvx, so it needs uv and a Redis 8 at REDIS_URL. Without the plugin:
claude mcp add beanis-memory --env REDIS_URL=redis://localhost:6379/0 -- beanis-mcp
Claude Desktop, Cursor and other MCP clients:
{
"mcpServers": {
"beanis-memory": {
"command": "beanis-mcp",
"env": { "REDIS_URL": "redis://localhost:6379/0" }
}
}
}
More
await product.insert(ttl=3600) # expire after an hour
await product.set_ttl(7200); await product.get_ttl(); await product.persist()
price = await product.get_field("price") # read or write one field
await product.set_field("stock", 200)
await Product.insert_many(products) # pipelined batches
await Product.get_many(ids)
await Product.delete_many(ids)
Event hooks run around inserts, updates and deletes:
from beanis import Document, Insert, before_event
class Product(Document):
name: str
price: float
@before_event(Insert)
def check_price(self):
if self.price < 0:
raise ValueError("price cannot be negative")
Custom types are registered once with register_type(cls, encoder=..., decoder=...); NumPy arrays are supported when NumPy is installed. See the custom encoders tutorial.
When to use it
Use Beanis when you want typed models on Redis without running Redis modules: caches with structure, session and user state, job metadata, small catalogues, retrieval for RAG, and agent memory.
Use something else when you need complex queries across many fields, full-text search or aggregations: a document database, or Redis's query engine with Redis OM or RedisVL.
Requirements
- Python 3.9+ (3.10+ for the
mcpextra) - Redis 5.0+; Redis 8.0+ for
VectorField,Memory, the CLI memory commands and the MCP server - Pydantic 1.10+ or 2.x
Development
pip install -e ".[test,mcp]"
pytest # unit tests, on fakeredis
REDIS_PORT=6379 pytest tests/test_real_redis_regressions.py tests/test_memory_real_redis.py # needs Redis 8
See AGENTS.md for repository conventions.
Credits
Beanis started as a fork of Beanie, the MongoDB ODM by Roman Right and contributors, rewritten for Redis.
License
Apache License 2.0
Metadata
Release files for beanis 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| beanis-0.3.1.tar.gz | 48.4 kB | Without provenance | Details for beanis-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| beanis-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for beanis-0.3.1-py3-none-any.whl |
Total release size: 104.1 kB
Release files / beanis-0.3.1.tar.gz
| Download URL | beanis-0.3.1.tar.gz |
|---|---|
| Size | 48.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6598346239a270871e4bf7ce7ee5ddfff933c6798381274f7d9746d123e9cfc3
|
|
BLAKE2b-256 checksum How to use checksums |
0e2c05400675a8240ce809ca6506547d873ecab59ce997c5bfcb28056526e95f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.5
|
Release files / beanis-0.3.1-py3-none-any.whl
| Download URL | beanis-0.3.1-py3-none-any.whl |
|---|---|
| Size | 55.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ecb31852340854f0c94340e2e5858e57951281a9fae2cb0e985fb57f0904e4aa
|
|
BLAKE2b-256 checksum How to use checksums |
abcb05ae882555cf46ce1bc308f04d9e24f592e3c08dfa5608fb7db86229bbf4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.5
|
Security for beanis 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.