Scope memories per user, agent and conversation
Attach a scope when you add memories, then search with the same scope to read back only the memories you want.
To keep memories apart, give each one a scope when you add it. Then pass the same scope when you search. Search returns only memories that match every scope field you send.
Before you start
- You have an app ID and an API key. See Create an app and API key.
- You have read Memory scope.
Scope memories to a user
- Build a scope with
actor_idandactor_type. Always send the two together. - Pass the scope to
add. - Pass the same scope to
search.
from smritea import MemoryScope
alice = MemoryScope(actor_id="alice", actor_type="user")
client.add("Alice is vegetarian", scope=alice)
bob = MemoryScope(actor_id="bob", actor_type="user")
client.add("Bob eats fish", scope=bob)
# Returns only Alice's memories.
results = client.search("What does Alice eat?", scope=alice)The Go blocks run inside a function that returns error.
Scope memories to an agent
Use the same fields with actor_type set to agent. Give each agent its own actor_id.
from smritea import MemoryScope
bot = MemoryScope(actor_id="support-bot", actor_type="agent")
client.add("I escalate billing issues to a human", scope=bot)Scope memories to a conversation
Add a conversation_id to group memories by conversation. You can combine it with an actor.
from smritea import MemoryScope
chat = MemoryScope(
actor_id="alice",
actor_type="user",
conversation_id="chat-1042",
source_type="conversation",
)
client.add("Alice wants to book a trip to Lisbon", scope=chat)
# Returns memories from this conversation only.
only_chat = MemoryScope(conversation_id="chat-1042")
results = client.search("Where does Alice want to go?", scope=only_chat)Find conversations by who took part
Use participant_ids on search to find memories from conversations that more than one actor shared. The conversation must be one where all the listed actors took part.
from smritea import MemoryScope
shared = MemoryScope(participant_ids=["alice", "bob"])
results = client.search("What did we plan?", scope=shared)Handle the scope errors
Search rejects a scope that mixes participant_ids with conversation_id. The SDK raises its bad request error, which maps to HTTP 400 with the code VALIDATION_FAILED. Send one or the other.
from smritea import MemoryScope, SmriteaBadRequestError
mixed = MemoryScope(
conversation_id="chat-1042",
participant_ids=["alice", "bob"],
)
try:
client.search("trip plans", scope=mixed)
except SmriteaBadRequestError as e:
print(e.code) # VALIDATION_FAILEDRelated pages
Last updated on



