From 118016a21c8ee51e8c3cef92a94606addacf8fd4 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Mon, 31 Mar 2025 16:15:56 -0700 Subject: [PATCH] Add fast path to serialize None values --- libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py | 9 +++++++-- libs/langgraph/langgraph/graph/state.py | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py b/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py index 531edd185..49577b3aa 100644 --- a/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py +++ b/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py @@ -30,6 +30,7 @@ from langgraph.checkpoint.serde.types import SendProtocol from langgraph.store.base import Item LC_REVIVER = Reviver() +EMPTY_BYTES = b"" class JsonPlusSerializer(SerializerProtocol): @@ -194,7 +195,9 @@ class JsonPlusSerializer(SerializerProtocol): ) def dumps_typed(self, obj: Any) -> tuple[str, bytes]: - if isinstance(obj, bytes): + if obj is None: + return "null", EMPTY_BYTES + elif isinstance(obj, bytes): return "bytes", obj elif isinstance(obj, bytearray): return "bytearray", obj @@ -211,7 +214,9 @@ class JsonPlusSerializer(SerializerProtocol): def loads_typed(self, data: tuple[str, bytes]) -> Any: type_, data_ = data - if type_ == "bytes": + if type_ == "null": + return None + elif type_ == "bytes": return data_ elif type_ == "bytearray": return bytearray(data_) diff --git a/libs/langgraph/langgraph/graph/state.py b/libs/langgraph/langgraph/graph/state.py index 750ed30df..93fd65052 100644 --- a/libs/langgraph/langgraph/graph/state.py +++ b/libs/langgraph/langgraph/graph/state.py @@ -845,7 +845,7 @@ class CompiledStateGraph(CompiledGraph): if end != END: self.nodes[starts].writers.append( ChannelWrite( - (ChannelWriteEntry(CHANNEL_BRANCH_TO.format(end), starts),) + (ChannelWriteEntry(CHANNEL_BRANCH_TO.format(end), None),) ) ) elif end != END: @@ -871,7 +871,7 @@ class CompiledStateGraph(CompiledGraph): if filtered := [p for p in packets if p != END]: writes = [ ( - ChannelWriteEntry(CHANNEL_BRANCH_TO.format(p), start) + ChannelWriteEntry(CHANNEL_BRANCH_TO.format(p), None) if not isinstance(p, Send) else p )