langgraph: fix None handling for pydantic state updates

This commit is contained in:
vbarda
2025-02-12 17:53:12 -08:00
parent e005f0472b
commit 661476e88d
2 changed files with 15 additions and 2 deletions
+2 -2
View File
@@ -33,7 +33,7 @@ from langgraph.channels.dynamic_barrier_value import DynamicBarrierValue, WaitFo
from langgraph.channels.ephemeral_value import EphemeralValue
from langgraph.channels.last_value import LastValue
from langgraph.channels.named_barrier_value import NamedBarrierValue
from langgraph.constants import EMPTY_SEQ, NS_END, NS_SEP, SELF, TAG_HIDDEN
from langgraph.constants import EMPTY_SEQ, MISSING, NS_END, NS_SEP, SELF, TAG_HIDDEN
from langgraph.errors import (
ErrorCode,
InvalidUpdateError,
@@ -694,7 +694,7 @@ class CompiledStateGraph(CompiledGraph):
return [
(k, getattr(input, k))
for k in output_keys
if getattr(input, k, None) is not None
if getattr(input, k, MISSING) is not MISSING
]
else:
msg = create_error_message(
+13
View File
@@ -6482,3 +6482,16 @@ def test_node_destinations() -> None:
Edge(source="child", target="node_b", data="foo", conditional=True),
Edge(source="child", target="node_c", data="bar", conditional=True),
] == graph.edges
def test_pydantic_none_state_update() -> None:
from pydantic import BaseModel
class State(BaseModel):
foo: str | None
def node_a(state: State) -> State:
return State(foo=None)
graph = StateGraph(State).add_node(node_a).add_edge(START, "node_a").compile()
assert graph.invoke({"foo": ""}) == {"foo": None}