Skip to main content

Overview

The Memori class is the main entry point for the Memori SDK. It provides methods for managing memory attribution, sessions, recall, and LLM integrations.

Constructor

conn
Callable[[], Any] | Any | None
default:"None"
Database connection or connection factory. Can be:
  • A callable that returns a database connection
  • A database connection object directly
  • None to use cloud-based storage (requires MEMORI_API_KEY)
Supported databases: PostgreSQL, CockroachDB, SQLite, MongoDB, OceanBase
debug_truncate
bool
default:"True"
Whether to truncate long content in debug logs for readability

Initialization

from memori import Memori
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# Using PostgreSQL
engine = create_engine("postgresql://user:pass@localhost/db")
Session = sessionmaker(bind=engine)
mem = Memori(conn=Session)

# Using cloud storage
mem = Memori()  # Requires MEMORI_API_KEY environment variable

Methods

attribution()

Set entity and process identifiers for memory attribution.
entity_id
str | None
default:"None"
Unique identifier for the entity (e.g., user ID, customer ID). Maximum 100 characters.
process_id
str | None
default:"None"
Identifier for the process or application context. Maximum 100 characters.
return
Memori
Returns self for method chaining
mem.attribution(entity_id="user-123", process_id="my-app")

new_session()

Create a new session with a fresh UUID. This resets the session context and cache.
return
Memori
Returns self for method chaining
# Start a new conversation session
mem.new_session()

set_session()

Set an existing session ID to continue a previous session.
id
str | UUID
The session ID to use
return
Memori
Returns self for method chaining
from uuid import UUID

session_id = UUID("123e4567-e89b-12d3-a456-426614174000")
mem.set_session(session_id)

recall()

Retrieve relevant memories based on a query string.
query
str
required
The search query to find relevant memories
limit
int | None
default:"None"
Maximum number of facts to return. Defaults to config.recall_facts_limit (5)
return
list[FactSearchResult | Mapping[str, object] | str]
List of relevant memory facts matching the query
# Retrieve memories about user preferences
facts = mem.recall("What is the user's favorite color?", limit=10)
for fact in facts:
    print(fact)

embed_texts()

Generate embeddings for text inputs using the configured embedding model.
texts
str | list[str]
required
Single text string or list of text strings to embed
async_
bool
default:"False"
If True, returns an awaitable that runs embedding in a threadpool
return
list[list[float]] | Awaitable[list[list[float]]]
List of embedding vectors, one per input text. Returns awaitable if async_=True
# Synchronous embedding
embeddings = mem.embed_texts(["Hello world", "Goodbye world"])
print(len(embeddings))  # 2
print(len(embeddings[0]))  # Embedding dimension (e.g., 384)

# Asynchronous embedding
import asyncio

async def main():
    embeddings = await mem.embed_texts("Hello world", async_=True)
    print(embeddings)

asyncio.run(main())

close()

Close the underlying storage connection or session. Important for long-running processes like web servers to release database connections.
mem.close()

# Or use context manager
with Memori(conn=Session) as mem:
    mem.attribution(entity_id="user-123")
    # ... use mem ...
# Automatically closed when exiting context

LLM Integration

The Memori class provides built-in integration with multiple LLM providers:

llm.register()

Register an LLM client for automatic memory augmentation.
client
Any
default:"None"
Generic LLM client (auto-detected provider)
openai_chat
OpenAIChat
default:"None"
OpenAI chat completion client
claude
Anthropic
default:"None"
Anthropic Claude client
gemini
GoogleGenerativeModel
default:"None"
Google Gemini client
xai
XAI
default:"None"
xAI client
chatbedrock
ChatBedrock
default:"None"
LangChain ChatBedrock
chatgooglegenai
ChatGoogleGenerativeAI
default:"None"
LangChain ChatGoogleGenerativeAI
chatopenai
ChatOpenAI
default:"None"
LangChain ChatOpenAI
chatvertexai
ChatVertexAI
default:"None"
LangChain ChatVertexAI
from openai import OpenAI

client = OpenAI(api_key="sk-...")
mem = Memori(conn=Session).llm.register(client)

# Now use client as normal - Memori automatically augments it
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}]
)

Provider-Specific Integrations

Memori also provides direct access to provider-specific integrations:
  • mem.openai - OpenAI integration
  • mem.anthropic - Anthropic Claude integration
  • mem.google - Google Gemini integration
  • mem.xai - xAI integration
  • mem.langchain - LangChain integration
  • mem.agno - Agno integration
  • mem.pydantic_ai - Pydantic AI integration

Configuration

Access and modify configuration through mem.config:
# Set recall limits
mem.config.recall_facts_limit = 10
mem.config.recall_embeddings_limit = 2000

# Set embedding model
mem.config.embeddings.model = "all-MiniLM-L6-v2"

# Access session ID
print(mem.config.session_id)

# Build database schema
mem.config.storage.build()

Context Manager Support

The Memori class supports Python’s context manager protocol:
with Memori(conn=Session) as mem:
    mem.attribution(entity_id="user-123", process_id="app-1")
    facts = mem.recall("user preferences")
    # Connection automatically closed on exit

Complete Example

import os
from openai import OpenAI
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from memori import Memori

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

engine = create_engine(os.getenv("DATABASE_CONNECTION_STRING"))
Session = sessionmaker(bind=engine)

mem = Memori(conn=Session).llm.register(client)
mem.attribution(entity_id="user-123", process_id="my-app")
mem.config.storage.build()

# First conversation
response1 = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{
        "role": "user",
        "content": "My favorite color is blue and I live in Paris"
    }]
)
print(response1.choices[0].message.content)

# Later conversation - Memori remembers!
response2 = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{
        "role": "user",
        "content": "What's my favorite color?"
    }]
)
print(response2.choices[0].message.content)

# Wait for async memory processing to complete
mem.augmentation.wait()
mem.close()

See Also

Build docs developers (and LLMs) love