Skip to main content
Memories are structured pieces of information extracted from your content. Mem[v] automatically processes text, files, videos, and other content to create searchable memories.

Add a memory

from memvai import Memv

client = Memv()

# Add a simple memory
response = client.memories.add(
    space_id="space_abc123",
    content="The user prefers dark mode and uses Python for development",
    metadata={
        "source": "preferences",
        "user_id": "user_123"
    }
)

print(f"Memory ID: {response.memory_id}")

Add conversation memories

# Add conversation memory
response = client.memories.add(
    space_id="space_abc123",
    content="User asked about implementing authentication in FastAPI. Prefers JWT tokens.",
    metadata={
        "type": "conversation",
        "topic": "authentication",
        "timestamp": "2026-02-14T10:30:00Z"
    }
)

Add structured data

# Add structured memory with entities
client.memories.add(
    space_id="space_abc123",
    content="Sarah Chen is the VP of Engineering at Acme Corp. She uses React and TypeScript.",
    metadata={
        "entities": ["Sarah Chen", "Acme Corp", "React", "TypeScript"],
        "type": "profile"
    }
)

Search memories

# Search for memories
results = client.memories.search(
    space_id="space_abc123",
    query="What programming languages does the user prefer?",
    limit=10
)

# Process results
for memory in results.memories:
    print(f"Score: {memory.score}")
    print(f"Content: {memory.content}")
    print(f"Metadata: {memory.metadata}")
    print("---")

Search with filters

# Search with metadata filters
results = client.memories.search(
    space_id="space_abc123",
    query="authentication",
    filters={
        "type": "conversation",
        "topic": "authentication"
    },
    limit=5
)
Mem[v] uses semantic search to find relevant memories even when exact keywords don’t match:
# Query: "How does the user like their UI?"
# Will match: "The user prefers dark mode"
results = client.memories.search(
    space_id="space_abc123",
    query="How does the user like their UI?"
)

for memory in results.memories:
    print(memory.content)
    # Output: "The user prefers dark mode and uses Python for development"

Common patterns

Store user preferences

def save_user_preference(user_id: str, preference: str):
    return client.memories.add(
        space_id=f"user_{user_id}_prefs",
        content=preference,
        metadata={
            "user_id": user_id,
            "type": "preference",
            "timestamp": datetime.now().isoformat()
        }
    )

# Usage
save_user_preference("user_123", "Prefers concise responses")

Build conversation history

def add_conversation_turn(space_id: str, role: str, message: str):
    return client.memories.add(
        space_id=space_id,
        content=f"{role}: {message}",
        metadata={
            "role": role,
            "timestamp": datetime.now().isoformat()
        }
    )

# Usage
add_conversation_turn("conv_123", "user", "How do I deploy a FastAPI app?")
add_conversation_turn("conv_123", "assistant", "You can deploy FastAPI using...")

Get relevant context for AI

def get_relevant_context(space_id: str, query: str, limit: int = 5) -> str:
    """Get relevant memories as context string."""
    results = client.memories.search(
        space_id=space_id,
        query=query,
        limit=limit
    )

    return "\n\n".join([m.content for m in results.memories])

# Usage in a chatbot
user_query = "What are the user's preferences?"
context = get_relevant_context("user_space", user_query)

# Pass context to your LLM
response = llm.generate(
    prompt=f"Context: {context}\n\nUser: {user_query}"
)

Best practices

Include relevant metadata to make memories more searchable:
client.memories.add(
    space_id="space_abc123",
    content="User reported bug in login flow",
    metadata={
        "type": "bug_report",
        "severity": "high",
        "feature": "authentication",
        "reported_by": "user_123",
        "timestamp": datetime.now().isoformat()
    }
)
Write memory content that is self-contained and easy to understand.Good: "User prefers email notifications for important updates only"Less good: "email - important only"
More specific queries return more relevant results.Specific: "What is the user's preferred framework for backend development?"Vague: "framework"

Error handling

import memvai

try:
    response = client.memories.add(
        space_id="space_abc123",
        content="Important user preference"
    )
    print(f"Created memory: {response.memory_id}")
except memvai.NotFoundError:
    print("Space not found")
except memvai.UnprocessableEntityError as e:
    print(f"Invalid content: {e.message}")
except memvai.APIError as e:
    print(f"API error: {e}")

Next steps

Files

Upload files to extract memories

Knowledge graphs

Build knowledge graphs from memories