Conversations and stores#
Placeholders last for a conversation#
A run’s message history keeps the placeholders: the second run of a chat sees <<PERSON:1>> from the first. So the values behind placeholders are kept per conversation, not per run:
first = agent.run_sync("I'm John Smith.")
second = agent.run_sync("My manager is Jane Doe.", message_history=first.all_messages())
In the second run, John Smith is still <<PERSON:1>>, Jane Doe becomes <<PERSON:2>>, and enkrypt.restore can restore both.
The conversation is pydantic-ai’s conversation_id. It travels inside the message history, so it survives saving the history and loading it later, and it can be set explicitly with agent.run_sync(..., conversation_id="..."). A run with no conversation id is its own conversation.
Where placeholders are kept#
Enkrypt(store=...) takes a PlaceholderStore. The default, InMemoryStore, keeps the latest 1024 conversations in the process.
That is enough when every run of a conversation happens in one process. When the history is saved to a database and the next run may happen elsewhere or after a restart, keep the placeholders alongside the history, or restoring stops working for placeholders from earlier runs.
The values behind placeholders are personal data. Store them with the same care as the data itself.
Writing a store#
A store has three async methods. Each conversation is a mapping from placeholder to value:
from collections.abc import Mapping
class DatabaseStore:
async def load(self, conversation_id: str) -> Mapping[str, str]:
"""The conversation's placeholders, e.g. {"<<PERSON:1>>": "John Smith"}; empty if none."""
async def save(self, conversation_id: str, placeholders: Mapping[str, str]) -> None:
"""Replace the conversation's placeholders."""
async def forget(self, conversation_id: str) -> None:
"""Drop the conversation's placeholders."""
enkrypt = Enkrypt(store=DatabaseStore())
The store only ever holds this mapping; numbering is worked out from it.
Forgetting a conversation#
await enkrypt.forget(conversation_id)
drops a conversation’s placeholders and the values they stand for, for example on an erasure request. Placeholders still in the history then stay as placeholders.
Concurrent runs#
Two runs of the same conversation at the same time each load the mapping, add to it and save it, so one can overwrite the other’s new placeholders. A chat that runs one message at a time per conversation does not hit this. If yours can, serialize runs per conversation, or make save merge rather than replace.