LangGraph: GraphState serialization and deserialization

https://www.pexels.com/photo/landscape-photography-of-clouds-391522/
https://www.pexels.com/photo/landscape-photography-of-clouds-391522/

In this blog, we look at how to persist LangGraph state.  This is useful when we want to resume from a previous state. For demonstration purposes, we keep this code very simple.

The source code can be found here. The meat of the code is in the MemoryStore class. I store the data in the file system and this should be in Redis.


The put and restore functions are to store and restore the memory respectively. I stored the chat message history because this can be ended up in long term memory store.

I have main.py that just send request to a server. And, the server which is stateless will rebuild the langgraph from stored state accordingly.


Observe that we have a thread_id that can be used to locate the stored graph state.

The server is a simple langgraph solution and here is the system prompt.



So it generates random numbers and it can list the previously generated numbers.

The first demo, we do not store the graph state and we can see that langgraph cannot tell the previous numbers that were generated.

Please view the videos in full screen mode.

First demo (no audio)




The next video, we added code to store and restore state. Artifacts are stored in file system. Here we can see that langgraph's is restored and it is able to list the previously generated numbers.



This is just a simple demonstration of storing graph state by leveraging on the MemorySaver implementation from LangGraph.


UPDATE: We have created another git repo that demonstrate how to store memory in Azure Redis Cache. Here is the repo.







Comments