Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: Support returning mixed list of commands and state updates #2667

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions libs/langgraph/langgraph/graph/state.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import inspect

Check notice on line 1 in libs/langgraph/langgraph/graph/state.py

View workflow job for this annotation

GitHub Actions / benchmark

Benchmark results

......................................... fanout_to_subgraph_10x: Mean +- std dev: 60.3 ms +- 1.5 ms ......................................... fanout_to_subgraph_10x_sync: Mean +- std dev: 52.8 ms +- 0.8 ms ......................................... WARNING: the benchmark result may be unstable * the standard deviation (11.3 ms) is 12% of the mean (95.9 ms) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. fanout_to_subgraph_10x_checkpoint: Mean +- std dev: 95.9 ms +- 11.3 ms ......................................... fanout_to_subgraph_10x_checkpoint_sync: Mean +- std dev: 95.5 ms +- 1.9 ms ......................................... fanout_to_subgraph_100x: Mean +- std dev: 642 ms +- 47 ms ......................................... fanout_to_subgraph_100x_sync: Mean +- std dev: 497 ms +- 9 ms ......................................... fanout_to_subgraph_100x_checkpoint: Mean +- std dev: 951 ms +- 42 ms ......................................... fanout_to_subgraph_100x_checkpoint_sync: Mean +- std dev: 919 ms +- 20 ms ......................................... react_agent_10x: Mean +- std dev: 30.9 ms +- 0.6 ms ......................................... react_agent_10x_sync: Mean +- std dev: 22.4 ms +- 0.3 ms ......................................... react_agent_10x_checkpoint: Mean +- std dev: 47.1 ms +- 1.2 ms ......................................... WARNING: the benchmark result may be unstable * the standard deviation (4.06 ms) is 10% of the mean (39.7 ms) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. react_agent_10x_checkpoint_sync: Mean +- std dev: 39.7 ms +- 4.1 ms ......................................... react_agent_100x: Mean +- std dev: 347 ms +- 7 ms ......................................... react_agent_100x_sync: Mean +- std dev: 273 ms +- 5 ms ......................................... react_agent_100x_checkpoint: Mean +- std dev: 953 ms +- 20 ms ......................................... react_agent_100x_checkpoint_sync: Mean +- std dev: 843 ms +- 17 ms ......................................... wide_state_25x300: Mean +- std dev: 23.3 ms +- 0.5 ms ......................................... WARNING: the benchmark result may be unstable * the standard deviation (1.73 ms) is 11% of the mean (15.5 ms) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. wide_state_25x300_sync: Mean +- std dev: 15.5 ms +- 1.7 ms ......................................... wide_state_25x300_checkpoint: Mean +- std dev: 287 ms +- 16 ms ......................................... wide_state_25x300_checkpoint_sync: Mean +- std dev: 274 ms +- 14 ms ......................................... wide_state_15x600: Mean +- std dev: 27.0 ms +- 0.5 ms ......................................... wide_state_15x600_sync: Mean +- std dev: 16.5 ms +- 0.1 ms ......................................... wide_state_15x600_checkpoint: Mean +- std dev: 487 ms +- 15 ms ......................................... wide_state_15x600_checkpoint_sync: Mean +- std dev: 472 ms +- 15 ms ......................................... wide_state_9x1200: Mean +- std dev: 27.0 ms +- 0.4 ms ......................................... wide_state_9x1200_sync: Mean +- std dev: 16.5 ms +- 0.2 ms ......................................... wide_state_9x1200_checkpoint: Mean +- std dev: 322 ms +- 16 ms ......................................... wide_state_9x1200_checkpoint_sync: Mean +- std dev: 306 ms +- 14 ms

Check notice on line 1 in libs/langgraph/langgraph/graph/state.py

View workflow job for this annotation

GitHub Actions / benchmark

Comparison against main

+-----------------------------------------+---------+-----------------------+ | Benchmark | main | changes | +=========================================+=========+=======================+ | wide_state_9x1200_sync | 16.8 ms | 16.5 ms: 1.02x faster | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_100x_sync | 493 ms | 497 ms: 1.01x slower | +-----------------------------------------+---------+-----------------------+ | wide_state_15x600_sync | 16.4 ms | 16.5 ms: 1.01x slower | +-----------------------------------------+---------+-----------------------+ | react_agent_10x_sync | 22.3 ms | 22.4 ms: 1.01x slower | +-----------------------------------------+---------+-----------------------+ | wide_state_15x600_checkpoint_sync | 469 ms | 472 ms: 1.01x slower | +-----------------------------------------+---------+-----------------------+ | wide_state_15x600 | 26.8 ms | 27.0 ms: 1.01x slower | +-----------------------------------------+---------+-----------------------+ | react_agent_10x | 30.6 ms | 30.9 ms: 1.01x slower | +-----------------------------------------+---------+-----------------------+ | wide_state_25x300 | 23.0 ms | 23.3 ms: 1.01x slower | +-----------------------------------------+---------+-----------------------+ | react_agent_100x_sync | 270 ms | 273 ms: 1.01x slower | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_100x_checkpoint_sync | 907 ms | 919 ms: 1.01x slower | +-----------------------------------------+---------+-----------------------+ | wide_state_25x300_checkpoint | 283 ms | 287 ms: 1.02x slower | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_10x | 59.2 ms | 60.3 ms: 1.02x slower | +-----------------------------------------+---------+-----------------------+ | react_agent_10x_checkpoint | 46.2 ms | 47.1 ms: 1.02x slower | +-----------------------------------------+---------+-----------------------+ | react_agent_100x | 340 ms | 347 ms: 1.02x slower | +-----------------------------------------+---------+-----------------------+ | react_agent_100x_checkpoint_sync | 821 ms | 843 ms: 1.03x slower | +-----------------------------------------+---------+-----------------------+ | react_agent_100x_checkpoint | 927 ms | 953 ms: 1.03x slower | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_10x_checkpoint_sync | 92.5 ms | 95.5 ms: 1.03x slower | +-----------------------------------------+---------+-----------------------+ | react_agent_10x_checkpoint_sync | 38.1 ms | 39.7 ms: 1.04x slower | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_10x_sync | 50.4 ms | 52.8 ms: 1.05x slower | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_100x_checkpoint | 902 ms | 951 ms: 1.05x slower | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_10x_checkpoint | 90.1 ms | 95.9 ms: 1.06x slower | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_100x | 592 ms | 642 ms: 1.08x slower | +-----------------------------------------+---------+-----------------------+ | Geometric mean | (ref) | 1.02x slower | +-----------------------------------------+---------+-----------------------+ Benchmark hidden because not significant (6): wide_state_9x1200, wide_state_9x1200_checkpoint, wide_state_9x1200_checkpoint_sync, wide_state_15x600_checkpoint, wide_state_25x300_checkpoint_sync, wide_state_25x300_sync
import logging
import typing
import warnings
Expand Down Expand Up @@ -613,21 +613,24 @@
]

def _get_root(input: Any) -> Optional[Sequence[tuple[str, Any]]]:
if (
if isinstance(input, Command):
if input.graph == Command.PARENT:
return ()
return input._update_as_tuples()
elif (
isinstance(input, (list, tuple))
and input
and all(isinstance(i, Command) for i in input)
and any(isinstance(i, Command) for i in input)
):
updates: list[tuple[str, Any]] = []
for i in input:
if i.graph == Command.PARENT:
continue
updates.extend(i._update_as_tuples())
if isinstance(i, Command):
if i.graph == Command.PARENT:
continue
updates.extend(i._update_as_tuples())
else:
updates.append(("__root__", i))
return updates
elif isinstance(input, Command):
if input.graph == Command.PARENT:
return ()
return input._update_as_tuples()
elif input is not None:
return [("__root__", input)]

Expand All @@ -645,13 +648,16 @@
elif (
isinstance(input, (list, tuple))
and input
and all(isinstance(i, Command) for i in input)
and any(isinstance(i, Command) for i in input)
):
updates: list[tuple[str, Any]] = []
for i in input:
if i.graph == Command.PARENT:
continue
updates.extend(i._update_as_tuples())
if isinstance(i, Command):
if i.graph == Command.PARENT:
continue
updates.extend(i._update_as_tuples())
else:
updates.extend(_get_updates(i) or ())
return updates
elif get_type_hints(type(input)):
return [
Expand Down
28 changes: 28 additions & 0 deletions libs/langgraph/tests/test_pregel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14807,3 +14807,31 @@ def ask_age(s: State):
assert [event for event in graph.stream(Command(resume="19"), thread1)] == [
{"node": {"age": 19}},
]


def test_root_mixed_return() -> None:
def my_node(state: list[str]):
return [Command(update=["a"]), ["b"]]

graph = StateGraph(Annotated[list[str], operator.add])

graph.add_node(my_node)
graph.add_edge(START, "my_node")
graph = graph.compile()

assert graph.invoke([]) == ["a", "b"]


def test_dict_mixed_return() -> None:
class State(TypedDict):
foo: Annotated[str, operator.add]

def my_node(state: State):
return [Command(update={"foo": "a"}), {"foo": "b"}]

graph = StateGraph(State)
graph.add_node(my_node)
graph.add_edge(START, "my_node")
graph = graph.compile()

assert graph.invoke({"foo": ""}) == {"foo": "ab"}
6 changes: 3 additions & 3 deletions libs/langgraph/tests/test_pregel_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -9670,14 +9670,14 @@ async def outer_2(state: State):
),
(FloatBetween(0.2, 0.4), ((), {"outer_1": {"my_key": " and parallel"}})),
(
FloatBetween(0.5, 0.7),
FloatBetween(0.5, 0.8),
(
(AnyStr("inner:"),),
{"inner_2": {"my_key": " and there", "my_other_key": "got here"}},
),
),
(FloatBetween(0.5, 0.7), ((), {"inner": {"my_key": "got here and there"}})),
(FloatBetween(0.5, 0.7), ((), {"outer_2": {"my_key": " and back again"}})),
(FloatBetween(0.5, 0.8), ((), {"inner": {"my_key": "got here and there"}})),
(FloatBetween(0.5, 0.8), ((), {"outer_2": {"my_key": " and back again"}})),
]


Expand Down
Loading