diff --git a/libs/langgraph/langgraph/graph/branch.py b/libs/langgraph/langgraph/graph/branch.py index 082fb8a5e..c2c6c4da1 100644 --- a/libs/langgraph/langgraph/graph/branch.py +++ b/libs/langgraph/langgraph/graph/branch.py @@ -34,7 +34,7 @@ from langgraph.utils.runnable import ( ) Writer = Callable[ - [Sequence[Union[str, Send]]], + [Sequence[Union[str, Send]], bool], Sequence[Union[ChannelWriteEntry, Send]], ] @@ -142,7 +142,7 @@ class Branch(NamedTuple): ), list( zip_longest( - writer([e for e in self.ends.values()]), + writer([e for e in self.ends.values()], True), [str(la) for la, e in self.ends.items()], ) ) @@ -211,12 +211,11 @@ class Branch(NamedTuple): ] else: destinations = cast(Sequence[Union[Send, str]], result) - destinations = [d for d in destinations if d != END] if any(dest is None or dest == START for dest in destinations): raise ValueError("Branch did not return a valid destination") if any(p.node == END for p in destinations if isinstance(p, Send)): raise InvalidUpdateError("Cannot send a packet to the END node") - entries = writer(destinations) + entries = writer(destinations, False) if not entries: return input else: diff --git a/libs/langgraph/langgraph/graph/graph.py b/libs/langgraph/langgraph/graph/graph.py index ec23b5351..cbc58e819 100644 --- a/libs/langgraph/langgraph/graph/graph.py +++ b/libs/langgraph/langgraph/graph/graph.py @@ -374,7 +374,7 @@ class CompiledGraph(Pregel): def attach_branch(self, start: str, name: str, branch: Branch) -> None: def get_writes( - packets: Sequence[Union[str, Send]], + packets: Sequence[Union[str, Send]], static: bool = False ) -> Sequence[Union[ChannelWriteEntry, Send]]: return [ ( diff --git a/libs/langgraph/langgraph/graph/state.py b/libs/langgraph/langgraph/graph/state.py index c6426efe0..e88b6db99 100644 --- a/libs/langgraph/langgraph/graph/state.py +++ b/libs/langgraph/langgraph/graph/state.py @@ -843,7 +843,7 @@ class CompiledStateGraph(CompiledGraph): self, start: str, name: str, branch: Branch, *, with_reader: bool = True ) -> None: def get_writes( - packets: Sequence[Union[str, Send]], + packets: Sequence[Union[str, Send]], static: bool = False ) -> Sequence[Union[ChannelWriteEntry, Send]]: writes = [ ( @@ -852,7 +852,10 @@ class CompiledStateGraph(CompiledGraph): else p ) for p in packets + if (True if static else p != END) ] + if not writes: + return [] if branch.then and branch.then != END: writes.append( ChannelWriteEntry(