JsonPlusSerializer compatibility between LangGraph Python and LangGraph JS #2934
Replies: 1 comment
-
You raise an excellent point about the need for a consistent serialization protocol that bridges the gap between Python and JavaScript environments, particularly for tools like LangGraph and Vercel's AI SDK. Here's an approach to address this: Challenges Identified:
Solution Proposal: SerializerProtocol for CompatibilityA unified Step 1: Use JSON Serialization with MetadataDefine a serialization schema that adheres to JSON standards but includes metadata for extended features. Python ImplementationIn LangGraph, replace import json
class CustomJsonSerializer:
@staticmethod
def serialize(data):
"""Serialize Python objects to JSON."""
return json.dumps(data, ensure_ascii=False, default=str)
@staticmethod
def deserialize(data):
"""Deserialize JSON strings to Python objects."""
return json.loads(data) JavaScript ImplementationEnsure the same schema is used on the frontend. class CustomJsonSerializer {
static serialize(data) {
// Serialize JavaScript objects to JSON
return JSON.stringify(data);
}
static deserialize(data) {
// Deserialize JSON strings to JavaScript objects
return JSON.parse(data);
}
} Step 2: Modify PostgresSaver to Use Custom SerializerOverride the default from langgraph.persistence.postgres_saver import PostgresSaver
class CustomPostgresSaver(PostgresSaver):
def serialize(self, data):
return CustomJsonSerializer.serialize(data)
def deserialize(self, data):
return CustomJsonSerializer.deserialize(data) Step 3: Integration with Vercel’s AI SDKOn the frontend, adapt the retrieval logic to use the compatible serializer. fetch("/api/chat-history")
.then((response) => response.json())
.then((data) => {
const history = CustomJsonSerializer.deserialize(data);
console.log("Chat history:", history);
}); Step 4: Migration StrategyIf existing data in your Postgres database uses
Why This Approach Works:
Future Considerations:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Dear all, I hope you are doing well. I wanted to ask if there could be a possibility to have a SerializerProtocol for PostgresSaver that would be compatible for both Python and JavaScript.
I am currently implementing a complete Python back-end with LangGraph because it is very powerful and the Data Scientists are more familiar with Python. To save checkpoints, I use the PostgresSaver with a Supabase db. On the front-end, I am using Vercel's AI SDK and I implemented their stream protocol to be able to send streams from the back-end to the front-end. Everything is working great in this direction.
The problem arises when I want to retrieve chat history on the frontend. I wanted to rely on the channel_values available in the postgres checkpoints from the last "getTuple" result but I am having issues because of the use of msgpack since Python's JsonPlusSerializer uses that (or pickles for LangGraph api) whereas JS's JsonPlusSerializer only uses json serialization.
Having a SerializerProtocol that would bring compatibility could be absolutely amazing for startups using LangGraph Python and Vercel's AI SDK.
Of course, a workaround would be to independently save messages on the front end on each message received but that would just duplicate information across the database.
Thank you very much for all the great work!
Beta Was this translation helpful? Give feedback.
All reactions