When to use this
You want to persist cached conversation sessions into the knowledge graph so the Q&A history becomes part of the searchable graph. In the current API, the user-facing way to do this iscognee.improve(dataset=..., session_ids=[...]).
Use the lower-level Memify pipeline in this guide when you need direct control over only the session persistence step.
Before you start:
- Complete Quickstart to understand basic operations
- Ensure you have LLM Providers configured
- Have an existing knowledge graph created with
remember() - Caching must be enabled and at least one session must exist (created by prior
cognee.recall()calls with asession_id)
Code in Action
What Just Happened
- Remember — builds a knowledge graph from your text.
- Recall with
session_id— runs two recalls that accumulate Q&A history in the session cache under"demo_session". get_default_user()— retrieves the authenticated user. This pipeline requires aUserobject with write access.persist_sessions_in_knowledge_graph_pipeline(user, session_ids, dataset)— reads the cached session data and writes it into the knowledge graph.
What Changed in Your Graph
- New nodes are created from the session Q&A history, grouped under the
user_sessions_from_cachenode set. - The session data is processed internally into graph memory, so entities and relationships from the session content become part of the graph.
- Persistence is incremental: each run only ingests the Q&A entries added since the last successful persist for that session. Re-running on an unchanged session adds nothing new, so you can safely call it repeatedly as a session grows without re-embedding or duplicating the earlier history.
Additional Information
- Runnable session persistence demo available on our GitHub
Parameters
Parameters
user(User, required) — authenticated user with write access. Obtain viaawait get_default_user().session_ids(Optional[List[str]]) — list of session IDs to persist. IfNone, no sessions are extracted.dataset(str, default:"main_dataset") — the dataset to write session data into.run_in_background(bool, default:False) — run asynchronously and return immediately.
Under the hood
Under the hood
Two tasks run in sequence, coordinated by a per-
(user, session) persist watermark:extract_user_sessions— reads Q&A data from theSessionManagerfor the specifiedsession_ids, then consults the persist watermark (the number of Q&A entries already persisted for that session) and yields only the entries added since the last successful run. A session with no new entries yields nothing, so re-running on an unchanged session does no ingestion work.cognify_session— callscognee.addandcognee.cognifyon the extracted entries, writing the results into the graph under theuser_sessions_from_cachenode set. Only after both succeed does it advance that session’s watermark. If cognify fails, the watermark is left untouched and the same entries are retried on the next run (add-level content-hash deduplication keeps the retry safe).
SessionManager — it reuses the existing session cache and introduces no new backend, configuration, or credentials.Troubleshooting
Troubleshooting
- No sessions found — caching must be enabled and
recall()with asession_idmust have been run first. See Sessions and Caching. - Error: no graph data found — run
cognee.remember(..., dataset_name=...)before calling this pipeline. - LLM errors — verify that your LLM provider is configured. See LLM Providers.
- Permission errors — the user must have write access to the target dataset. See Permissions.
Improve
Bridge sessions with the current improvement workflow
Sessions Guide
Learn how sessions and caching work in Cognee
Recall
Query the enriched graph with v1.0 retrieval