[PR #2556] Add MongoDB Checkpoint Saver Implementation for LangGraph #2777

Closed
opened 2026-02-20 17:47:33 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langchain-ai/langgraph/pull/2556

State: closed
Merged: No


Pull Request Description:

This PR introduces a MongoDB-based checkpoint saver implementation for LangGraph. The implementation includes both synchronous and asynchronous versions, which can be used to save and retrieve checkpoints in a MongoDB database.

Key Changes:

  • AsyncMongoDBSaver: An asynchronous implementation of the checkpoint saver using MongoDB, designed for high performance and non-blocking operations.
  • MongoDBSaver: A synchronous implementation of the checkpoint saver using MongoDB, for simpler use cases where asynchronous operations are not necessary.
  • Example Usage: Added examples for using the MongoDB checkpoint savers with LangGraph agents, demonstrating how to use both the synchronous and asynchronous checkpointing methods.

Features:

  • Supports MongoDB version 4.x and later.
  • Utilizes the motor library for asynchronous MongoDB operations.
  • Provides a simple, configuration-driven API for connecting to a MongoDB database and saving/retrieving checkpoints.

Example Code:

  1. Synchronous Checkpoint Saver:
    from typing import Literal
    from langchain_core.runnables import ConfigurableField
    from langchain_core.tools import tool
    from langchain_openai import ChatOpenAI
    from langgraph.checkpoint.mongodb import MongoDBSaver
    from langgraph.checkpoint.mongodb.aio import AsyncMongoDBSaver
    from langgraph.prebuilt import create_react_agent

    @tool
    def get_weather(city: Literal["nyc", "sf"]):
        if city == "nyc":
            return "It might be cloudy in nyc"
        elif city == "sf":
            return "It's always sunny in sf"
        else:
            raise AssertionError("Unknown city")


    tools = [get_weather]  # List of tools to be used by the agent
    model = ChatOpenAI(model_name="gpt-4o-mini", temperature=0, api_key="your_api_key")

    # Create a MongoDBSaver instance and use it as a checkpointer
    with MongoDBSaver.from_conn_info(
        url="mongodb://localhost:27017", db_name="checkpoints"
    ) as checkpointer:

        # Create a React agent using the model and tools, along with the MongoDB checkpointer
        graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)

        # Example configuration for the agent's execution
        config = {"configurable": {"thread_id": "1"}}

        # Invoke the agent with a query
        res = graph.invoke({"messages": [("human", "what's the weather in sf")]}, config)

        # Retrieve the latest checkpoint
        latest_checkpoint = checkpointer.get(config)
        latest_checkpoint_tuple = checkpointer.get_tuple(config)

        # List all checkpoint tuples
        checkpoint_tuples = list(checkpointer.list(config))
  1. Asynchronous Checkpoint Saver:
     from typing import Literal
     from langchain_core.tools import tool
     from langchain_openai import ChatOpenAI
     from langgraph.checkpoint.mongodb.aio import AsyncMongoDBSaver
     from langgraph.prebuilt import create_react_agent
    
     @tool
     def get_weather(city: Literal["nyc", "sf"]):
         if city == "nyc":
             return "It might be cloudy in nyc"
         elif city == "sf":
             return "It's always sunny in sf"
         else:
             raise AssertionError("Unknown city")
    
    
     tools = [get_weather]  # List of tools to be used by the agent
     model = ChatOpenAI(model_name="gpt-4o-mini", temperature=0, api_key="your_api_key")
    
     # Create a AsyncMongoDBSaver instance and use it as a checkpointer
     async with AsyncMongoDBSaver.from_conn_info(
         url="mongodb://localhost:27017", db_name="checkpoints"
     ) as checkpointer:
    
         # Create a React agent using the model and tools, along with the MongoDB checkpointer
         graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)
    
         # Example configuration for the agent's execution
         config = {"configurable": {"thread_id": "1"}}
    
         # ainvoke the agent with a query
         res = await graph.ainvoke({"messages": [("human", "what's the weather in sf")]}, config)
    
         # Retrieve the latest checkpoint
         latest_checkpoint = checkpointer.get(config)
         latest_checkpoint_tuple = checkpointer.get_tuple(config)
    
         # List all checkpoint tuples
         checkpoint_tuples = list(checkpointer.list(config))
    

Checklist:

  • Code implemented and tested
  • Documentation updated (README.md, usage examples)
  • Tests added
  • Code linting and formatting checks
**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/2556 **State:** closed **Merged:** No --- ### Pull Request Description: This PR introduces a MongoDB-based checkpoint saver implementation for LangGraph. The implementation includes both synchronous and asynchronous versions, which can be used to save and retrieve checkpoints in a MongoDB database. #### Key Changes: - **`AsyncMongoDBSaver`**: An asynchronous implementation of the checkpoint saver using MongoDB, designed for high performance and non-blocking operations. - **`MongoDBSaver`**: A synchronous implementation of the checkpoint saver using MongoDB, for simpler use cases where asynchronous operations are not necessary. - **Example Usage**: Added examples for using the MongoDB checkpoint savers with LangGraph agents, demonstrating how to use both the synchronous and asynchronous checkpointing methods. #### Features: - Supports MongoDB version 4.x and later. - Utilizes the `motor` library for asynchronous MongoDB operations. - Provides a simple, configuration-driven API for connecting to a MongoDB database and saving/retrieving checkpoints. #### Example Code: 1. **Synchronous Checkpoint Saver**: ```python from typing import Literal from langchain_core.runnables import ConfigurableField from langchain_core.tools import tool from langchain_openai import ChatOpenAI from langgraph.checkpoint.mongodb import MongoDBSaver from langgraph.checkpoint.mongodb.aio import AsyncMongoDBSaver from langgraph.prebuilt import create_react_agent @tool def get_weather(city: Literal["nyc", "sf"]): if city == "nyc": return "It might be cloudy in nyc" elif city == "sf": return "It's always sunny in sf" else: raise AssertionError("Unknown city") tools = [get_weather] # List of tools to be used by the agent model = ChatOpenAI(model_name="gpt-4o-mini", temperature=0, api_key="your_api_key") # Create a MongoDBSaver instance and use it as a checkpointer with MongoDBSaver.from_conn_info( url="mongodb://localhost:27017", db_name="checkpoints" ) as checkpointer: # Create a React agent using the model and tools, along with the MongoDB checkpointer graph = create_react_agent(model, tools=tools, checkpointer=checkpointer) # Example configuration for the agent's execution config = {"configurable": {"thread_id": "1"}} # Invoke the agent with a query res = graph.invoke({"messages": [("human", "what's the weather in sf")]}, config) # Retrieve the latest checkpoint latest_checkpoint = checkpointer.get(config) latest_checkpoint_tuple = checkpointer.get_tuple(config) # List all checkpoint tuples checkpoint_tuples = list(checkpointer.list(config)) ``` 2. **Asynchronous Checkpoint Saver**: ```python from typing import Literal from langchain_core.tools import tool from langchain_openai import ChatOpenAI from langgraph.checkpoint.mongodb.aio import AsyncMongoDBSaver from langgraph.prebuilt import create_react_agent @tool def get_weather(city: Literal["nyc", "sf"]): if city == "nyc": return "It might be cloudy in nyc" elif city == "sf": return "It's always sunny in sf" else: raise AssertionError("Unknown city") tools = [get_weather] # List of tools to be used by the agent model = ChatOpenAI(model_name="gpt-4o-mini", temperature=0, api_key="your_api_key") # Create a AsyncMongoDBSaver instance and use it as a checkpointer async with AsyncMongoDBSaver.from_conn_info( url="mongodb://localhost:27017", db_name="checkpoints" ) as checkpointer: # Create a React agent using the model and tools, along with the MongoDB checkpointer graph = create_react_agent(model, tools=tools, checkpointer=checkpointer) # Example configuration for the agent's execution config = {"configurable": {"thread_id": "1"}} # ainvoke the agent with a query res = await graph.ainvoke({"messages": [("human", "what's the weather in sf")]}, config) # Retrieve the latest checkpoint latest_checkpoint = checkpointer.get(config) latest_checkpoint_tuple = checkpointer.get_tuple(config) # List all checkpoint tuples checkpoint_tuples = list(checkpointer.list(config)) ``` #### Checklist: - [x] Code implemented and tested - [x] Documentation updated (README.md, usage examples) - [x] Tests added - [x] Code linting and formatting checks
yindo added the pull-request label 2026-02-20 17:47:33 -05:00
yindo closed this issue 2026-02-20 17:47:33 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#2777