langchain-ai / langgraphjs

Build resilient language agents as graphs.
https://langchain-ai.github.io/langgraphjs/
MIT License
633 stars 95 forks source link

Allowing asynchronous serialization and deserialization within checkpoint savers #529

Open wpoynter opened 1 month ago

wpoynter commented 1 month ago

I am currently in the process of writing a package that uses AWS's Dynamno DB as the persistence store for checkpoint saving. I am keeping the implementation as close to the existing Sqlite and MongoDB checkpoint savers as possible.

In my use case for the library I am creating, I would like the serde to be able to encrypt and decrypt the data. My intention is to use an encryption library that performs asynchronously. There is no reason within my package I cannot support both serdes that work synchronously and asynchronously, but it does deviate from the existing classes.

Proposed Change

My idea is that the SerializationProtocol is updated to allow async methods, e.g.

export interface SerializerProtocol {
    dumpsTyped(data: any): Promise<[string, Uint8Array]> | [string, Uint8Array];
    loadsTyped(type: string, data: Uint8Array | string): Promise<any> | unknown;
}

Then changes are made to MemorySaver, MongoDBSaver, SqliteSaver, and tests to await the calls of dumpsTyped and loadsTyped. This should allow existing users to continue to use synchronous methods without an issue and will allow future use of asynchronous methods.

Possible Issue

The only possible issue that occurs to me is how Langgraph JS behaves if someone provides slow async methods for serialization for some reason?

Doing the work

I am happy to take a stab at the work if it seems like a reasonable idea.

wpoynter commented 1 month ago

PR: https://github.com/langchain-ai/langgraphjs/pull/543