Add fast path to serialize None values

This commit is contained in:
Nuno Campos
2025-03-31 16:15:56 -07:00
parent 067b99c789
commit 118016a21c
2 changed files with 9 additions and 4 deletions
@@ -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_)
+2 -2
View File
@@ -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
)