An in-memory checkpoint saver.
This checkpoint saver stores checkpoints in memory using a defaultdict.
InMemorySaver(
self,
*,
serde: SerializerProtocol | None = None,
factory: type[defaultdict] = defaultdict
)Note:
Only use InMemorySaver for debugging or testing purposes.
For production use cases we recommend installing langgraph-checkpoint-postgres and using PostgresSaver / AsyncPostgresSaver.
If you are using LangSmith Deployment, no checkpointer needs to be specified. The correct managed checkpointer will be used automatically.
Example:
import asyncio
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import StateGraph
builder = StateGraph(int)
builder.add_node("add_one", lambda x: x + 1)
builder.set_entry_point("add_one")
builder.set_finish_point("add_one")
memory = InMemorySaver()
graph = builder.compile(checkpointer=memory)
coro = graph.ainvoke(1, {"configurable": {"thread_id": "thread-1"}})
asyncio.run(coro) # Output: 2| Name | Type | Description |
|---|---|---|
serde | SerializerProtocol | None | Default: NoneThe serializer to use for serializing and deserializing checkpoints. |
| Name | Type |
|---|---|
| serde | SerializerProtocol | None |
| factory | type[defaultdict] |
Get a checkpoint tuple from the in-memory storage.
This method retrieves a checkpoint tuple from the in-memory storage based on the
provided config. If the config contains a checkpoint_id key, the checkpoint with
the matching thread ID and timestamp is retrieved. Otherwise, the latest checkpoint
for the given thread ID is retrieved.
List checkpoints from the in-memory storage.
This method retrieves a list of checkpoint tuples from the in-memory storage based on the provided criteria.
Save a checkpoint to the in-memory storage.
This method saves a checkpoint to the in-memory storage. The checkpoint is associated with the provided config.
Save a list of writes to the in-memory storage.
This method saves a list of writes to the in-memory storage. The writes are associated with the provided config.
Delete all checkpoints and writes associated with a thread ID.
Asynchronous version of get_tuple.
This method is an asynchronous wrapper around get_tuple that runs the synchronous
method in a separate thread using asyncio.
Asynchronous version of list.
This method is an asynchronous wrapper around list that runs the synchronous
method in a separate thread using asyncio.
Asynchronous version of put.
Asynchronous version of put_writes.
This method is an asynchronous wrapper around put_writes that runs the synchronous
method in a separate thread using asyncio.
Delete all checkpoints and writes associated with a thread ID.
Fetch a checkpoint using the given configuration.
Delete all checkpoints and writes associated with the given run IDs.
Copy all checkpoints and writes from one thread to another.
Prune checkpoints for the given threads.
Asynchronously fetch a checkpoint using the given configuration.
Asynchronously delete all checkpoints and writes for the given run IDs.
Asynchronously copy all checkpoints and writes from one thread to another.
Asynchronously prune checkpoints for the given threads.
Return a shallow clone with a derived msgpack allowlist.