Importing User History Into a New Memory Layer

Importing User History Into a New Memory Layer

Importing User History Into a New Memory Layer

โ€ข

โ€ข

Updated on

โ€ข

Updated on


Most teams set up a memory layer for their app after they already have users. Those users have months or years of conversations behind them, and often a basic memory system too, like a table of user facts or an in-house vector search.


For example, a user Sam has been using a running coach app, Stride, and has had about 600 conversations over two years. Over that time, the coach would have learned Sam's goals, training preferences, injuries, etc. Now say Stride switches to a new memory system. If the history is not ingested correctly and Sam asks about a knee injury in a new session, the coach would have no record.


For long-running agents and consumer apps, the old details are a large part of why people keep using the app, and users notice a gap on their first message. So the first job with a new memory layer is to bring the history in without a gap.


This article covers the kinds of history an app usually has, the rules for every import, how to switch over, and how to set it up on Mem0. Stride and Sam are the running example, and the same steps apply to any app with existing users.

Three kinds of history

  • Raw conversation history. Chat logs, sessions, journal entries, voice transcripts. These need extraction, so the memory layer pulls out the facts as expected.

  • Already extracted facts. An in-house table of user facts/preferences, memories in another account, or an export from another memory system. Load them as written. Extracting them again rewrites a summary of a summary, which can drop some details.

  • Structured records. App's onboarding answers, profile fields, plan settings, a tutor's list of finished lessons. Store what the agent should bring up in conversation, and leave the rest in your database.


If you have both extracted facts and the raw chats they came from, pick one per user, or you get two copies of most facts in slightly different words.

Rules for every import

  • Keep the original time. In April 2025 Sam wrote "I hurt my knee on yesterday's long run". If the import reads that message against today's date, the memory says the injury happened yesterday. Pass the time each message was sent, or each old fact was written, with every record. With its April 14 send time attached, the message is stored as "User hurt their knee on April 13, 2025 during a long run". Date filters and "last month" questions depend on the same time.

  • Go oldest first, one user at a time. Later messages correct earlier ones. Sam lived in Lisbon in 2025 and moved to Berlin in 2026. In date order, the Berlin memory replaces the Lisbon one, and a search for current facts returns only Berlin. In reverse order, the two are not linked, and the same search returns "User lives in Lisbon" next to "User no longer lives in Lisbon". Import users in parallel if you like, but keep each user's sessions in order.

  • Split by session. Each call carries one timestamp, so send one session per call, or one day per call for long-running, single-threaded sessions.

  • Make it safe to run twice. Imports can fail halfway through. Tag every record with its source id so you can rerun only what is missing.

  • Decide what to leave behind. Two years of chat logs contain things you would not store today: a card number pasted into a refund request, a password, a message about a family member's health diagnosis. Import is a checkpoint where you can cut out such details using extraction rules. With such rules in place, a message that mentions both a knee injury and a card number would be stored as the injury alone.

  • Send both sides of the conversation to extraction. "ok" on its own means nothing. "ok" after "Let's move your long run to Saturday" is an agreement.

Switching without a gap

  1. Write to both systems. From day one, every new message goes to both. The old system keeps answering reads initially.

  2. Backfill in the background. Load history up to the day the double writes started, per user, oldest first.

  3. Compare answers on real questions. For 30 to 50 users, run the questions your agent asks every session ("What is Sam training for?", "Any injuries?") against both systems and read the answers side by side.

  4. Incremental rollout. Move reads for one group of users, then the rest. Keep the old system read-only for a few weeks so you can roll back.


Neither system misses a message at any point, so a user sees no gap.

Setting it up on Mem0

Mem0 has two write modes, and an import uses both:

  • An add with infer=False stores each message verbatim.

  • A default add runs extraction, applies your custom instructions, and links a new fact to an older one it replaces.


Both accept timestamp (Unix seconds) and metadata.


from mem0 import MemoryClient

client = MemoryClient()

# Facts that are already extracted: store as written, with the original time
for fact in legacy_facts(user_id):                 # sorted oldest first
    client.add(
        fact.text,
        user_id=user_id,
        infer=False,
        timestamp=int(fact.created_at.timestamp()),  # timezone-aware datetime
        metadata={"source": "legacy_store", "legacy_id": fact.id},
    )

# Raw chat history: one call per session, oldest first, with import rules
for session in chat_sessions(user_id):             # sorted oldest first
    client.add(
        session.messages,                          # user and coach turns
        user_id=user_id,
        timestamp=int(session.started_at.timestamp()),  # timezone-aware datetime
        custom_instructions=IMPORT_POLICY,
        metadata={"source": "chat_import", "session_id": session.id},
    )
from mem0 import MemoryClient

client = MemoryClient()

# Facts that are already extracted: store as written, with the original time
for fact in legacy_facts(user_id):                 # sorted oldest first
    client.add(
        fact.text,
        user_id=user_id,
        infer=False,
        timestamp=int(fact.created_at.timestamp()),  # timezone-aware datetime
        metadata={"source": "legacy_store", "legacy_id": fact.id},
    )

# Raw chat history: one call per session, oldest first, with import rules
for session in chat_sessions(user_id):             # sorted oldest first
    client.add(
        session.messages,                          # user and coach turns
        user_id=user_id,
        timestamp=int(session.started_at.timestamp()),  # timezone-aware datetime
        custom_instructions=IMPORT_POLICY,
        metadata={"source": "chat_import", "session_id": session.id},
    )
from mem0 import MemoryClient

client = MemoryClient()

# Facts that are already extracted: store as written, with the original time
for fact in legacy_facts(user_id):                 # sorted oldest first
    client.add(
        fact.text,
        user_id=user_id,
        infer=False,
        timestamp=int(fact.created_at.timestamp()),  # timezone-aware datetime
        metadata={"source": "legacy_store", "legacy_id": fact.id},
    )

# Raw chat history: one call per session, oldest first, with import rules
for session in chat_sessions(user_id):             # sorted oldest first
    client.add(
        session.messages,                          # user and coach turns
        user_id=user_id,
        timestamp=int(session.started_at.timestamp()),  # timezone-aware datetime
        custom_instructions=IMPORT_POLICY,
        metadata={"source": "chat_import", "session_id": session.id},
    )


Details that matter during an import:

  • The original time and your metadata come back on every memory. A verbatim fact sent with an April 14, 2025 timestamp has that date as its created_at, and its legacy_id stays in metadata.

  • Writes with infer=False skip the check for differently worded duplicates. Sending the exact same sentence again returns the existing memory id and stores nothing new. But a different version, like "half-marathon" with a hyphen, becomes a second memory.

  • Writes with infer=False keep user and assistant messages and drop system messages. A coach turn sent this way becomes a memory on the user. Image and file content is skipped without an error, so compare counts after the run.

  • Per-call custom_instructions replace the project-level instructions for that call only, so any live traffic keeps its own rules during the import.

  • Extraction runs in the background. A default add returns an event_id with status PENDING, and a session finishes within seconds. Check that every event ends as SUCCEEDED and resend the ones that don't. A verbatim add returns its result straight away.

  • Mem0 keeps a replaced fact. When the Berlin message clearly says Sam no longer lives in Lisbon and arrives after the Lisbon one, both memories stay and Lisbon is marked as superseded. Pass latest_only=True on search when the agent needs only the current fact. A softer change, such as "I've started eating fish again" after "I'm vegetarian", may not get linked.


Moving from another Mem0 account or project. Export memories from the older project, sort by created_at, and write each one into the new project with infer=False, the same user_id, the old created_at as timestamp, and the old memory_id in metadata. The "superseded" fact-links do not carry over, so a current-facts search on the new project returns Lisbon and Berlin together. If the new project only needs current facts, read the old one with get_all(..., latest_only=True).


from datetime import datetime

memories, page = [], 1
while True:
    resp = old.get_all(filters={"user_id": user_id}, page=page, page_size=100)
    memories += resp["results"]
    if not resp.get("next"):
        break
    page += 1

for m in sorted(memories, key=lambda m: datetime.fromisoformat(m["created_at"])):
    new.add(
        m["memory"],
        user_id=user_id,
        infer=False,
        timestamp=int(datetime.fromisoformat(m["created_at"]).timestamp()),
        metadata={**(m.get("metadata") or {}), "old_memory_id": m["id"]},
    )
from datetime import datetime

memories, page = [], 1
while True:
    resp = old.get_all(filters={"user_id": user_id}, page=page, page_size=100)
    memories += resp["results"]
    if not resp.get("next"):
        break
    page += 1

for m in sorted(memories, key=lambda m: datetime.fromisoformat(m["created_at"])):
    new.add(
        m["memory"],
        user_id=user_id,
        infer=False,
        timestamp=int(datetime.fromisoformat(m["created_at"]).timestamp()),
        metadata={**(m.get("metadata") or {}), "old_memory_id": m["id"]},
    )
from datetime import datetime

memories, page = [], 1
while True:
    resp = old.get_all(filters={"user_id": user_id}, page=page, page_size=100)
    memories += resp["results"]
    if not resp.get("next"):
        break
    page += 1

for m in sorted(memories, key=lambda m: datetime.fromisoformat(m["created_at"])):
    new.add(
        m["memory"],
        user_id=user_id,
        infer=False,
        timestamp=int(datetime.fromisoformat(m["created_at"]).timestamp()),
        metadata={**(m.get("metadata") or {}), "old_memory_id": m["id"]},
    )


Moving from Mem0 open source. If your opensource setup uses hosted Qdrant, the migration guide has a one-line script that does the same thing, with an import key on each memory so a rerun skips what is already there. For other vector stores, read your memories out of the store and load them with the verbatim path above (infer=False with timestamp)..


If you are moving a consumer app to Mem0 and your users' history is stored somewhere else today, send us what your data looks like, and we will help you plan the import.

References


GET TLDR from:

Summarize

Website/Footer

Summarize

Website/Footer

Summarize

Website/Footer

Summarize

Website/Footer