DOC: Missing put_writes and aput_writes Method in "Create a Custom Checkpointer Using MongoDB" #160

Closed
opened 2026-02-20 17:29:59 -05:00 by yindo · 4 comments
Owner

Originally created by @samitaaissat on GitHub (Jul 28, 2024).

Issue with current documentation:

Issue identical to the one for Redis: #1134

There is no instructions nor mentions for the put_writes and put_writes method. If you follow the tutorial, you get this error: NotImplementedError: This method was added in langgraph 0.1.7. Please update your checkpoint saver to implement it.

Link to the doc page: https://langchain-ai.github.io/langgraph/how-tos/persistence_mongodb/

Idea or request for content:

Here are implementations of put_writes and async aput_writes:

    def put_writes(
        self,
        config: RunnableConfig,
        writes: Sequence[Tuple[str, Any]],
        task_id: str,
    ) -> None:
        """Save a list of writes to the database.

        Args:
            config (RunnableConfig): The MongoDB configuration.
            writes (Sequence[Tuple[str, Any]]): A sequence of tuples representing the writes to be saved.
            task_id (str): The task ID associated with the writes.

        Returns:
            None

        Raises:
            ValueError: If the MongoDBSaver is not initialized with a MongoClient.
        """
        if isinstance(self.client, AsyncIOMotorClient):
            raise ValueError(
                "MongoDBSaver is not initialized with a MongoClient"
            )
        self.client[self.db_name][self.collection_name].insert_many(
            [
                {
                    "thread_id": config["configurable"]["thread_id"],
                    "thread_ts": config["configurable"]["thread_ts"],
                    "task_id": task_id,
                    "idx": idx,
                    "channel": channel,
                    "value": self.serde.dumps(value),
                }
                for idx, (channel, value) in enumerate(writes)
            ]
        )
    async def aput_writes(
        self,
        config: RunnableConfig,
        writes: Sequence[Tuple[str, Any]],
        task_id: str,
    ) -> None:
        """Save a list of writes to the database, asynchronously.

        Args:
            config (RunnableConfig): The configuration for the MongoDB connection.
            writes (Sequence[Tuple[str, Any]]): The list of writes to be saved.
            task_id (str): The ID of the task.

        Raises:
            ValueError: If the MongoDBSaver is not initialized with an AsyncIOMotorClient.

        Returns:
            None
        """
        if isinstance(self.client, MongoClient):
            raise ValueError(
                "MongoDBSaver is not initialized with an AsyncIOMotorClient"
            )
        await self.client[self.db_name][self.collection_name].insert_many(
            [
                {
                    "thread_id": config["configurable"]["thread_id"],
                    "thread_ts": config["configurable"]["thread_ts"],
                    "task_id": task_id,
                    "idx": idx,
                    "channel": channel,
                    "value": self.serde.dumps(value),
                }
                for idx, (channel, value) in enumerate(writes)
            ]
        )

It was tested and is working properly

Originally created by @samitaaissat on GitHub (Jul 28, 2024). ### Issue with current documentation: Issue identical to the one for Redis: #1134 There is no instructions nor mentions for the `put_writes` and `put_writes` method. If you follow the tutorial, you get this error: NotImplementedError: This method was added in langgraph 0.1.7. Please update your checkpoint saver to implement it. Link to the doc page: https://langchain-ai.github.io/langgraph/how-tos/persistence_mongodb/ ### Idea or request for content: Here are implementations of `put_writes` and `async aput_writes`: ```python def put_writes( self, config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, ) -> None: """Save a list of writes to the database. Args: config (RunnableConfig): The MongoDB configuration. writes (Sequence[Tuple[str, Any]]): A sequence of tuples representing the writes to be saved. task_id (str): The task ID associated with the writes. Returns: None Raises: ValueError: If the MongoDBSaver is not initialized with a MongoClient. """ if isinstance(self.client, AsyncIOMotorClient): raise ValueError( "MongoDBSaver is not initialized with a MongoClient" ) self.client[self.db_name][self.collection_name].insert_many( [ { "thread_id": config["configurable"]["thread_id"], "thread_ts": config["configurable"]["thread_ts"], "task_id": task_id, "idx": idx, "channel": channel, "value": self.serde.dumps(value), } for idx, (channel, value) in enumerate(writes) ] ) ``` ```python async def aput_writes( self, config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, ) -> None: """Save a list of writes to the database, asynchronously. Args: config (RunnableConfig): The configuration for the MongoDB connection. writes (Sequence[Tuple[str, Any]]): The list of writes to be saved. task_id (str): The ID of the task. Raises: ValueError: If the MongoDBSaver is not initialized with an AsyncIOMotorClient. Returns: None """ if isinstance(self.client, MongoClient): raise ValueError( "MongoDBSaver is not initialized with an AsyncIOMotorClient" ) await self.client[self.db_name][self.collection_name].insert_many( [ { "thread_id": config["configurable"]["thread_id"], "thread_ts": config["configurable"]["thread_ts"], "task_id": task_id, "idx": idx, "channel": channel, "value": self.serde.dumps(value), } for idx, (channel, value) in enumerate(writes) ] ) ``` It was tested and is working properly
yindo closed this issue 2026-02-20 17:30:00 -05:00
Author
Owner

@DhavalThkkar commented on GitHub (Jul 29, 2024):

@samitaaissat Can you just create a pull request for Redis and MongoDB and add it over there?

I wrote the code for Redis and at that point in time there were no specific updates for put_writes and aput_writes

@DhavalThkkar commented on GitHub (Jul 29, 2024): @samitaaissat Can you just create a pull request for Redis and MongoDB and add it over there? I wrote the code for Redis and at that point in time there were no specific updates for `put_writes` and `aput_writes`
Author
Owner

@gustavo-zitta commented on GitHub (Aug 1, 2024):

It worked, the database structure wasn't that good, but it helps.
Thank you so much @samitaaissat

@gustavo-zitta commented on GitHub (Aug 1, 2024): It worked, the database structure wasn't that good, but it helps. Thank you so much @samitaaissat
Author
Owner

@samitaaissat commented on GitHub (Aug 2, 2024):

@samitaaissat Can you just create a pull request for Redis and MongoDB and add it over there?

I wrote the code for Redis and at that point in time there were no specific updates for put_writes and aput_writes

Alright, I'll create a PR tonight or tomorrow then :)

@samitaaissat commented on GitHub (Aug 2, 2024): > @samitaaissat Can you just create a pull request for Redis and MongoDB and add it over there? > > I wrote the code for Redis and at that point in time there were no specific updates for `put_writes` and `aput_writes` Alright, I'll create a PR tonight or tomorrow then :)
Author
Owner

@vbarda commented on GitHub (Aug 13, 2024):

This has been resolved in #1297

@vbarda commented on GitHub (Aug 13, 2024): This has been resolved in #1297
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#160