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: add names for tasks #3202

Merged
merged 5 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion libs/langgraph/langgraph/func/__init__.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/func/__init__.py

View workflow job for this annotation

GitHub Actions / benchmark

Benchmark results

......................................... fanout_to_subgraph_10x: Mean +- std dev: 63.9 ms +- 1.3 ms ......................................... fanout_to_subgraph_10x_sync: Mean +- std dev: 56.2 ms +- 0.9 ms ......................................... fanout_to_subgraph_10x_checkpoint: Mean +- std dev: 80.7 ms +- 1.9 ms ......................................... fanout_to_subgraph_10x_checkpoint_sync: Mean +- std dev: 99.6 ms +- 2.1 ms ......................................... fanout_to_subgraph_100x: Mean +- std dev: 646 ms +- 20 ms ......................................... fanout_to_subgraph_100x_sync: Mean +- std dev: 552 ms +- 18 ms ......................................... fanout_to_subgraph_100x_checkpoint: Mean +- std dev: 891 ms +- 72 ms ......................................... fanout_to_subgraph_100x_checkpoint_sync: Mean +- std dev: 1.01 sec +- 0.02 sec ......................................... react_agent_10x: Mean +- std dev: 31.0 ms +- 0.9 ms ......................................... react_agent_10x_sync: Mean +- std dev: 23.3 ms +- 0.3 ms ......................................... react_agent_10x_checkpoint: Mean +- std dev: 39.4 ms +- 0.9 ms ......................................... react_agent_10x_checkpoint_sync: Mean +- std dev: 37.0 ms +- 1.2 ms ......................................... react_agent_100x: Mean +- std dev: 343 ms +- 7 ms ......................................... react_agent_100x_sync: Mean +- std dev: 275 ms +- 5 ms ......................................... react_agent_100x_checkpoint: Mean +- std dev: 648 ms +- 7 ms ......................................... react_agent_100x_checkpoint_sync: Mean +- std dev: 625 ms +- 11 ms ......................................... wide_state_25x300: Mean +- std dev: 23.9 ms +- 0.5 ms ......................................... wide_state_25x300_sync: Mean +- std dev: 16.0 ms +- 0.2 ms ......................................... wide_state_25x300_checkpoint: Mean +- std dev: 250 ms +- 13 ms ......................................... wide_state_25x300_checkpoint_sync: Mean +- std dev: 249 ms +- 15 ms ......................................... wide_state_15x600: Mean +- std dev: 28.0 ms +- 0.6 ms ......................................... wide_state_15x600_sync: Mean +- std dev: 18.5 ms +- 0.5 ms ......................................... wide_state_15x600_checkpoint: Mean +- std dev: 430 ms +- 14 ms ......................................... wide_state_15x600_checkpoint_sync: Mean +- std dev: 430 ms +- 17 ms ......................................... wide_state_9x1200: Mean +- std dev: 28.2 ms +- 0.9 ms ......................................... wide_state_9x1200_sync: Mean +- std dev: 18.5 ms +- 0.3 ms ......................................... wide_state_9x1200_checkpoint: Mean +- std dev: 282 ms +- 14 ms ......................................... wide_state_9x1200_checkpoint_sync: Mean +- std dev: 276 ms +- 14 ms

Check notice on line 1 in libs/langgraph/langgraph/func/__init__.py

View workflow job for this annotation

GitHub Actions / benchmark

Comparison against main

+-----------------------------------------+----------+------------------------+ | Benchmark | main | changes | +=========================================+==========+========================+ | fanout_to_subgraph_100x | 678 ms | 646 ms: 1.05x faster | +-----------------------------------------+----------+------------------------+ | react_agent_100x_checkpoint | 660 ms | 648 ms: 1.02x faster | +-----------------------------------------+----------+------------------------+ | react_agent_100x_checkpoint_sync | 632 ms | 625 ms: 1.01x faster | +-----------------------------------------+----------+------------------------+ | wide_state_9x1200_sync | 18.7 ms | 18.5 ms: 1.01x faster | +-----------------------------------------+----------+------------------------+ | react_agent_100x_sync | 277 ms | 275 ms: 1.01x faster | +-----------------------------------------+----------+------------------------+ | react_agent_10x_sync | 23.4 ms | 23.3 ms: 1.01x faster | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_10x | 63.5 ms | 63.9 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | react_agent_10x_checkpoint_sync | 36.7 ms | 37.0 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | react_agent_10x_checkpoint | 39.1 ms | 39.4 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_100x_checkpoint_sync | 1.00 sec | 1.01 sec: 1.01x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_10x_sync | 55.5 ms | 56.2 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_100x_checkpoint | 829 ms | 891 ms: 1.07x slower | +-----------------------------------------+----------+------------------------+ | Geometric mean | (ref) | 1.00x faster | +-----------------------------------------+----------+------------------------+ Benchmark hidden because not significant (16): wide_state_9x1200, wide_state_25x300_checkpoint, wide_state_9x1200_checkpoint, wide_state_9x1200_checkpoint_sync, fanout_to_subgraph_100x_sync, react_agent_100x, fanout_to_subgraph_10x_checkpoint_sync, wide_state_15x600_checkpoint, wide_state_25x300, wide_state_25x300_sync, wide_state_15x600, fanout_to_subgraph_10x_checkpoint, wide_state_15x600_sync, wide_state_15x600_checkpoint_sync, wide_state_25x300_checkpoint_sync, react_agent_10x
import concurrent.futures
import functools
import inspect
Expand Down Expand Up @@ -37,7 +37,7 @@

@overload
def task(
*, retry: Optional[RetryPolicy] = None
*, name: Optional[str] = None, retry: Optional[RetryPolicy] = None
) -> Callable[[Callable[P, T]], Callable[P, SyncAsyncFuture[T]]]: ...


Expand All @@ -50,6 +50,7 @@
def task(
__func_or_none__: Optional[Union[Callable[P, T], Callable[P, Awaitable[T]]]] = None,
*,
name: Optional[str] = None,
retry: Optional[RetryPolicy] = None,
) -> Union[
Callable[[Callable[P, T]], Callable[P, SyncAsyncFuture[T]]],
Expand Down Expand Up @@ -120,6 +121,9 @@
) -> Union[
Callable[P, concurrent.futures.Future[T]], Callable[P, asyncio.Future[T]]
]:
if name is not None:
func.__name__ = name

call_func = functools.partial(call, func, retry=retry)
object.__setattr__(call_func, "_is_pregel_task", True)
return functools.update_wrapper(call_func, func)
Expand Down
26 changes: 24 additions & 2 deletions libs/langgraph/tests/test_pregel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5570,10 +5570,10 @@ def graph(state: dict) -> dict:


@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_SYNC)
def test_multiple_interrupts_imperative(
def test_multiple_interrupts_functional(
request: pytest.FixtureRequest, checkpointer_name: str, snapshot: SnapshotAssertion
):
"""Test multiple interrupts with an imperative API."""
"""Test multiple interrupts with an functional API."""
vbarda marked this conversation as resolved.
Show resolved Hide resolved
checkpointer = request.getfixturevalue(f"checkpointer_{checkpointer_name}")

counter = 0
Expand Down Expand Up @@ -6265,3 +6265,25 @@ async def workflow(inputs: dict, *, previous: Any):
# Test with another thread
assert await workflow.ainvoke({}, {"configurable": {"thread_id": "2"}}) == "!"
assert previous_ is None


def test_named_tasks_functional() -> None:
@task(name="custom_name")
def foo(state: dict) -> dict:
return "foo"

@task
def bar(state: dict) -> dict:
return "bar"

@entrypoint()
def workflow(inputs: dict) -> dict:
fut_foo = foo(inputs)
fut_bar = bar(fut_foo.result())
return fut_bar.result()

assert list(workflow.stream({}, stream_mode="updates")) == [
{"custom_name": "foo"},
{"bar": "bar"},
{"workflow": "bar"},
]
26 changes: 24 additions & 2 deletions libs/langgraph/tests/test_pregel_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -6812,8 +6812,8 @@ async def graph(state: dict) -> dict:

@NEEDS_CONTEXTVARS
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_ASYNC)
async def test_multiple_interrupts_imperative(checkpointer_name: str) -> None:
"""Test multiple interrupts with an imperative API."""
async def test_multiple_interrupts_functional(checkpointer_name: str) -> None:
"""Test multiple interrupts with an functional API."""
from langgraph.func import entrypoint, task

counter = 0
Expand Down Expand Up @@ -7325,3 +7325,25 @@ async def foo(inputs, previous=None) -> Any:

assert list(await foo.ainvoke({"a": "1"}, config)) == ["a", "b"]
assert previous_return_values == [None]


async def test_named_tasks_functional() -> None:
@task(name="custom_name")
async def foo(state: dict) -> dict:
return "foo"

@task
async def bar(state: dict) -> dict:
return "bar"

@entrypoint()
async def workflow(inputs: dict) -> dict:
foo_result = await foo(inputs)
bar_result = await bar(foo_result)
return bar_result

assert [c async for c in workflow.astream({}, stream_mode="updates")] == [
{"custom_name": "foo"},
{"bar": "bar"},
{"workflow": "bar"},
]
Loading