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

langgraph[patch]: Fix unit test for Command(update) #2693

Merged
merged 2 commits into from
Dec 10, 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
24 changes: 12 additions & 12 deletions libs/langgraph/langgraph/pregel/loop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import asyncio

Check notice on line 1 in libs/langgraph/langgraph/pregel/loop.py

View workflow job for this annotation

GitHub Actions / benchmark

Benchmark results

......................................... fanout_to_subgraph_10x: Mean +- std dev: 62.7 ms +- 1.5 ms ......................................... fanout_to_subgraph_10x_sync: Mean +- std dev: 53.0 ms +- 1.0 ms ......................................... fanout_to_subgraph_10x_checkpoint: Mean +- std dev: 94.0 ms +- 7.7 ms ......................................... fanout_to_subgraph_10x_checkpoint_sync: Mean +- std dev: 96.6 ms +- 2.0 ms ......................................... fanout_to_subgraph_100x: Mean +- std dev: 648 ms +- 19 ms ......................................... fanout_to_subgraph_100x_sync: Mean +- std dev: 518 ms +- 8 ms ......................................... fanout_to_subgraph_100x_checkpoint: Mean +- std dev: 1.02 sec +- 0.04 sec ......................................... fanout_to_subgraph_100x_checkpoint_sync: Mean +- std dev: 982 ms +- 24 ms ......................................... react_agent_10x: Mean +- std dev: 31.3 ms +- 0.7 ms ......................................... react_agent_10x_sync: Mean +- std dev: 23.2 ms +- 0.3 ms ......................................... react_agent_10x_checkpoint: Mean +- std dev: 47.9 ms +- 0.9 ms ......................................... react_agent_10x_checkpoint_sync: Mean +- std dev: 37.4 ms +- 0.5 ms ......................................... react_agent_100x: Mean +- std dev: 349 ms +- 7 ms ......................................... react_agent_100x_sync: Mean +- std dev: 277 ms +- 3 ms ......................................... react_agent_100x_checkpoint: Mean +- std dev: 963 ms +- 17 ms ......................................... react_agent_100x_checkpoint_sync: Mean +- std dev: 859 ms +- 14 ms ......................................... wide_state_25x300: Mean +- std dev: 23.9 ms +- 0.5 ms ......................................... wide_state_25x300_sync: Mean +- std dev: 15.1 ms +- 0.2 ms ......................................... wide_state_25x300_checkpoint: Mean +- std dev: 293 ms +- 18 ms ......................................... wide_state_25x300_checkpoint_sync: Mean +- std dev: 281 ms +- 18 ms ......................................... wide_state_15x600: Mean +- std dev: 28.0 ms +- 0.5 ms ......................................... wide_state_15x600_sync: Mean +- std dev: 17.5 ms +- 0.2 ms ......................................... wide_state_15x600_checkpoint: Mean +- std dev: 491 ms +- 15 ms ......................................... wide_state_15x600_checkpoint_sync: Mean +- std dev: 476 ms +- 15 ms ......................................... wide_state_9x1200: Mean +- std dev: 27.8 ms +- 0.5 ms ......................................... wide_state_9x1200_sync: Mean +- std dev: 17.4 ms +- 0.2 ms ......................................... wide_state_9x1200_checkpoint: Mean +- std dev: 323 ms +- 15 ms ......................................... wide_state_9x1200_checkpoint_sync: Mean +- std dev: 310 ms +- 15 ms

Check notice on line 1 in libs/langgraph/langgraph/pregel/loop.py

View workflow job for this annotation

GitHub Actions / benchmark

Comparison against main

+-----------------------------------------+---------+------------------------+ | Benchmark | main | changes | +=========================================+=========+========================+ | react_agent_100x | 346 ms | 349 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_9x1200_sync | 17.3 ms | 17.4 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | react_agent_100x_sync | 275 ms | 277 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | fanout_to_subgraph_10x | 62.1 ms | 62.7 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_25x300 | 23.7 ms | 23.9 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | react_agent_10x | 31.0 ms | 31.3 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_9x1200_checkpoint | 319 ms | 323 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_15x600_checkpoint_sync | 471 ms | 476 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_9x1200_checkpoint_sync | 307 ms | 310 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_15x600_checkpoint | 485 ms | 491 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_9x1200 | 27.5 ms | 27.8 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_15x600_sync | 17.3 ms | 17.5 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | fanout_to_subgraph_100x_sync | 510 ms | 518 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_25x300_sync | 14.9 ms | 15.1 ms: 1.02x slower | +-----------------------------------------+---------+------------------------+ | fanout_to_subgraph_10x_sync | 52.1 ms | 53.0 ms: 1.02x slower | +-----------------------------------------+---------+------------------------+ | wide_state_15x600 | 27.5 ms | 28.0 ms: 1.02x slower | +-----------------------------------------+---------+------------------------+ | react_agent_10x_sync | 22.8 ms | 23.2 ms: 1.02x slower | +-----------------------------------------+---------+------------------------+ | react_agent_10x_checkpoint_sync | 36.6 ms | 37.4 ms: 1.02x slower | +-----------------------------------------+---------+------------------------+ | fanout_to_subgraph_10x_checkpoint_sync | 94.4 ms | 96.6 ms: 1.02x slower | +-----------------------------------------+---------+------------------------+ | wide_state_25x300_checkpoint | 286 ms | 293 ms: 1.03x slower | +-----------------------------------------+---------+------------------------+ | react_agent_10x_checkpoint | 46.6 ms | 47.9 ms: 1.03x slower | +-----------------------------------------+---------+------------------------+ | wide_state_25x300_checkpoint_sync | 273 ms | 281 ms: 1.03x slower | +-----------------------------------------+---------+------------------------+ | fanout_to_subgraph_100x | 628 ms | 648 ms: 1.03x slower | +-----------------------------------------+---------+------------------------+ | react_agent_100x_checkpoint_sync | 833 ms | 859 ms: 1.03x slower | +-----------------------------------------+---------+------------------------+ | react_agent_100x_checkpoint | 930 ms | 963 ms: 1.04x
import concurrent.futures
from collections import defaultdict, deque
from contextlib import AsyncExitStack, ExitStack
Expand Down Expand Up @@ -422,18 +422,6 @@
self.status = "out_of_steps"
return False

# apply NULL writes
if null_writes := [
w[1:] for w in self.checkpoint_pending_writes if w[0] == NULL_TASK_ID
]:
mv_writes = apply_writes(
self.checkpoint,
self.channels,
[PregelTaskWrites((), INPUT, null_writes, [])],
self.checkpointer_get_next_version,
)
for key, values in mv_writes.items():
self._update_mv(key, values)
# prepare next tasks
self.tasks = prepare_next_tasks(
self.checkpoint,
Expand Down Expand Up @@ -552,6 +540,18 @@
# save writes
for tid, ws in writes.items():
self.put_writes(tid, ws)
# apply NULL writes
if null_writes := [
w[1:] for w in self.checkpoint_pending_writes if w[0] == NULL_TASK_ID
]:
mv_writes = apply_writes(
self.checkpoint,
self.channels,
[PregelTaskWrites((), INPUT, null_writes, [])],
self.checkpointer_get_next_version,
)
for key, values in mv_writes.items():
self._update_mv(key, values)
# proceed past previous checkpoint
if is_resuming:
self.checkpoint["versions_seen"].setdefault(INTERRUPT, {})
Expand Down
20 changes: 9 additions & 11 deletions libs/langgraph/tests/test_pregel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14906,9 +14906,14 @@ def my_node(state: State):
assert graph.invoke({"foo": ""}) == {"foo": "ab"}


def test_command_with_static_breakpoints() -> None:
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_SYNC)
def test_command_with_static_breakpoints(
request: pytest.FixtureRequest, checkpointer_name: str
) -> None:
"""Test that we can use Command to resume and update with static breakpoints."""

checkpointer = request.getfixturevalue(f"checkpointer_{checkpointer_name}")

class State(TypedDict):
"""The graph state."""

Expand All @@ -14930,20 +14935,13 @@ def node2(state: State):
builder.add_edge(START, "node1")
builder.add_edge("node1", "node2")

# A checkpointer must be enabled for interrupts to work!
checkpointer = MemorySaver()
graph = builder.compile(checkpointer=checkpointer, interrupt_before=["node1"])

config = {
"configurable": {
"thread_id": uuid.uuid4(),
}
}
config = {"configurable": {"thread_id": str(uuid.uuid4())}}

# Start the graph and interrupt at the first node
graph.invoke({"foo": "abc"}, config)
result = graph.invoke(Command(resume="node1"), config)
assert result == {"foo": "abc|node-1|node-2"}
result = graph.invoke(Command(update={"foo": "def"}), config)
assert result == {"foo": "def|node-1|node-2"}


@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_SYNC)
Expand Down
35 changes: 35 additions & 0 deletions libs/langgraph/tests/test_pregel_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -13201,6 +13201,41 @@ async def ask_age(s: State):
]


@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_ASYNC)
async def test_command_with_static_breakpoints(checkpointer_name: str) -> None:
"""Test that we can use Command to resume and update with static breakpoints."""

class State(TypedDict):
"""The graph state."""

foo: str

def node1(state: State):
return {
"foo": state["foo"] + "|node-1",
}

def node2(state: State):
return {
"foo": state["foo"] + "|node-2",
}

builder = StateGraph(State)
builder.add_node("node1", node1)
builder.add_node("node2", node2)
builder.add_edge(START, "node1")
builder.add_edge("node1", "node2")

async with awith_checkpointer(checkpointer_name) as checkpointer:
graph = builder.compile(checkpointer=checkpointer, interrupt_before=["node1"])
config = {"configurable": {"thread_id": str(uuid.uuid4())}}

# Start the graph and interrupt at the first node
await graph.ainvoke({"foo": "abc"}, config)
result = await graph.ainvoke(Command(update={"foo": "def"}), config)
assert result == {"foo": "def|node-1|node-2"}


@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_ASYNC)
async def test_multistep_plan(checkpointer_name: str):
from langchain_core.messages import AnyMessage
Expand Down
Loading