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: handle more callable types in tasks #3203

Merged
merged 1 commit into from
Jan 24, 2025
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
2 changes: 2 additions & 0 deletions 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: 64.4 ms +- 1.6 ms ......................................... fanout_to_subgraph_10x_sync: Mean +- std dev: 56.3 ms +- 0.8 ms ......................................... fanout_to_subgraph_10x_checkpoint: Mean +- std dev: 83.0 ms +- 1.6 ms ......................................... fanout_to_subgraph_10x_checkpoint_sync: Mean +- std dev: 102 ms +- 2 ms ......................................... fanout_to_subgraph_100x: Mean +- std dev: 754 ms +- 36 ms ......................................... fanout_to_subgraph_100x_sync: Mean +- std dev: 565 ms +- 22 ms ......................................... fanout_to_subgraph_100x_checkpoint: Mean +- std dev: 965 ms +- 50 ms ......................................... fanout_to_subgraph_100x_checkpoint_sync: Mean +- std dev: 1.03 sec +- 0.03 sec ......................................... react_agent_10x: Mean +- std dev: 31.3 ms +- 0.7 ms ......................................... react_agent_10x_sync: Mean +- std dev: 23.7 ms +- 0.3 ms ......................................... react_agent_10x_checkpoint: Mean +- std dev: 40.4 ms +- 0.9 ms ......................................... react_agent_10x_checkpoint_sync: Mean +- std dev: 38.1 ms +- 0.9 ms ......................................... react_agent_100x: Mean +- std dev: 350 ms +- 6 ms ......................................... react_agent_100x_sync: Mean +- std dev: 281 ms +- 3 ms ......................................... react_agent_100x_checkpoint: Mean +- std dev: 707 ms +- 16 ms ......................................... react_agent_100x_checkpoint_sync: Mean +- std dev: 677 ms +- 18 ms ......................................... wide_state_25x300: Mean +- std dev: 24.3 ms +- 0.5 ms ......................................... wide_state_25x300_sync: Mean +- std dev: 16.3 ms +- 0.2 ms ......................................... wide_state_25x300_checkpoint: Mean +- std dev: 254 ms +- 15 ms ......................................... wide_state_25x300_checkpoint_sync: Mean +- std dev: 251 ms +- 18 ms ......................................... wide_state_15x600: Mean +- std dev: 28.0 ms +- 0.6 ms ......................................... wide_state_15x600_sync: Mean +- std dev: 18.5 ms +- 0.2 ms ......................................... wide_state_15x600_checkpoint: Mean +- std dev: 438 ms +- 17 ms ......................................... wide_state_15x600_checkpoint_sync: Mean +- std dev: 431 ms +- 16 ms ......................................... wide_state_9x1200: Mean +- std dev: 27.9 ms +- 0.5 ms ......................................... wide_state_9x1200_sync: Mean +- std dev: 18.5 ms +- 0.2 ms ......................................... wide_state_9x1200_checkpoint: Mean +- std dev: 285 ms +- 17 ms ......................................... wide_state_9x1200_checkpoint_sync: Mean +- std dev: 278 ms +- 15 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 | +=========================================+==========+========================+ | wide_state_9x1200_sync | 18.8 ms | 18.5 ms: 1.02x faster | +-----------------------------------------+----------+------------------------+ | wide_state_9x1200 | 28.1 ms | 27.9 ms: 1.01x faster | +-----------------------------------------+----------+------------------------+ | wide_state_15x600_sync | 18.4 ms | 18.5 ms: 1.00x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_10x_sync | 56.0 ms | 56.3 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | wide_state_15x600 | 27.8 ms | 28.0 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_10x | 64.0 ms | 64.4 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | wide_state_15x600_checkpoint_sync | 426 ms | 431 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | react_agent_10x_sync | 23.5 ms | 23.7 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | react_agent_10x | 31.0 ms | 31.3 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | wide_state_15x600_checkpoint | 433 ms | 438 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_100x_sync | 555 ms | 565 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_100x_checkpoint_sync | 1.01 sec | 1.03 sec: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_25x300 | 23.8 ms | 24.3 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_25x300_sync | 15.9 ms | 16.3 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | react_agent_100x | 341 ms | 350 ms: 1.03x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_10x_checkpoint_sync | 99.4 ms | 102 ms: 1.03x slower | +-----------------------------------------+----------+------------------------+ | react_agent_100x_sync | 274 ms | 281 ms: 1.03x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_10x_checkpoint | 80.6 ms | 83.0 ms: 1.03x slower | +-----------------------------------------+----------+------------------------+ | react_agent_10x_checkpoint | 39.0 ms | 40.4 ms: 1.03x slower | +-----------------------------------------+----------+------------------------+ | react_agent_10x_checkpoint_sync | 36.6 ms | 38.1 ms: 1.04x slower | +-----------------------------------------+----------+------------------------+ | react_agent_100x_checkpoint | 664 ms | 707 ms: 1.07x slower | +-----------------------------------------+----------+------------------------+ | react_agent_100x_checkpoint_sync | 625 ms | 677 ms: 1.08x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_100x_checkpoint | 876 ms | 965 ms: 1.10x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_100x | 669 ms | 754 ms: 1.13x slower | +-----------------------------------------+----------+------------------------+ | Geometric mean
import concurrent.futures
import functools
import inspect
Expand Down Expand Up @@ -123,8 +123,10 @@
]:
if name is not None:
if hasattr(func, "__func__"):
# handle class methods
func.__func__.__name__ = name
else:
# handle regular functions / partials / callable classes, etc.
func.__name__ = name

call_func = functools.partial(call, func, retry=retry)
Expand Down
15 changes: 12 additions & 3 deletions libs/langgraph/langgraph/pregel/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,21 @@ def get_runnable_for_task(func: Callable[..., Any]) -> RunnableSeq:
if key in CACHE:
return CACHE[key]
else:
if hasattr(func, "__name__"):
name = func.__name__
elif hasattr(func, "func"):
name = func.func.__name__
elif hasattr(func, "__class__"):
name = func.__class__.__name__
else:
name = str(func)

if is_async_callable(func):
run = RunnableCallable(
None,
func,
explode_args=True,
name=func.__name__,
name=name,
trace=False,
recurse=False,
)
Expand All @@ -182,14 +191,14 @@ def get_runnable_for_task(func: Callable[..., Any]) -> RunnableSeq:
func,
functools.wraps(func)(functools.partial(run_in_executor, None, func)),
explode_args=True,
name=func.__name__,
name=name,
trace=False,
recurse=False,
)
seq = RunnableSeq(
run,
ChannelWrite([ChannelWriteEntry(RETURN)], tags=[TAG_HIDDEN]),
name=func.__name__,
name=name,
trace_inputs=functools.partial(
_explode_args_trace_inputs, inspect.signature(func)
),
Expand Down
41 changes: 32 additions & 9 deletions libs/langgraph/tests/test_pregel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import enum
import functools
import json
import logging
import operator
Expand Down Expand Up @@ -6268,26 +6269,48 @@ async def workflow(inputs: dict, *, previous: Any):


def test_named_tasks_functional() -> None:

class Foo:
def foo(self, state: dict) -> dict:
return "foo"
def foo(self, value: str) -> dict:
return value + "foo"

f = Foo()

# class method task
foo = task(f.foo, name="custom_foo")

# regular function task
@task(name="custom_bar")
def bar(state: dict) -> dict:
return "bar"
def bar(value: str) -> dict:
return value + "|bar"

def baz(update: str, value: str) -> dict:
return value + f"|{update}"

# partial function task (unnamed)
baz_task = task(functools.partial(baz, "baz"))
# partial function task (named_)
custom_baz_task = task(functools.partial(baz, "custom_baz"), name="custom_baz")

class Qux:
def __call__(self, value: str) -> dict:
return value + "|qux"

qux_task = task(Qux(), name="qux")

@entrypoint()
def workflow(inputs: dict) -> dict:
fut_foo = foo(inputs)
fut_bar = bar(fut_foo.result())
return fut_bar.result()
fut_baz = baz_task(fut_bar.result())
fut_custom_baz = custom_baz_task(fut_baz.result())
fut_qux = qux_task(fut_custom_baz.result())
return fut_qux.result()

assert list(workflow.stream({}, stream_mode="updates")) == [
assert list(workflow.stream("", stream_mode="updates")) == [
{"custom_foo": "foo"},
{"custom_bar": "bar"},
{"workflow": "bar"},
{"custom_bar": "foo|bar"},
{"baz": "foo|bar|baz"},
{"custom_baz": "foo|bar|baz|custom_baz"},
{"qux": "foo|bar|baz|custom_baz|qux"},
{"workflow": "foo|bar|baz|custom_baz|qux"},
]
41 changes: 32 additions & 9 deletions libs/langgraph/tests/test_pregel_async.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import functools
import logging
import operator
import random
Expand Down Expand Up @@ -7329,26 +7330,48 @@ async def foo(inputs, previous=None) -> Any:

@NEEDS_CONTEXTVARS
async def test_named_tasks_functional() -> None:

class Foo:
async def foo(self, state: dict) -> dict:
return "foo"
async def foo(self, value: str) -> dict:
return value + "foo"

f = Foo()

# class method task
foo = task(f.foo, name="custom_foo")

# regular function task
@task(name="custom_bar")
async def bar(state: dict) -> dict:
return "bar"
async def bar(value: str) -> dict:
return value + "|bar"

async def baz(update: str, value: str) -> dict:
return value + f"|{update}"

# partial function task (unnamed)
baz_task = task(functools.partial(baz, "baz"))
# partial function task (named_)
custom_baz_task = task(functools.partial(baz, "custom_baz"), name="custom_baz")

class Qux:
def __call__(self, value: str) -> dict:
return value + "|qux"

qux_task = task(Qux(), name="qux")

@entrypoint()
async def workflow(inputs: dict) -> dict:
foo_result = await foo(inputs)
bar_result = await bar(foo_result)
return bar_result
baz_result = await baz_task(bar_result)
custom_baz_result = await custom_baz_task(baz_result)
qux_result = await qux_task(custom_baz_result)
return qux_result

assert [c async for c in workflow.astream({}, stream_mode="updates")] == [
assert [c async for c in workflow.astream("", stream_mode="updates")] == [
{"custom_foo": "foo"},
{"custom_bar": "bar"},
{"workflow": "bar"},
{"custom_bar": "foo|bar"},
{"baz": "foo|bar|baz"},
{"custom_baz": "foo|bar|baz|custom_baz"},
{"qux": "foo|bar|baz|custom_baz|qux"},
{"workflow": "foo|bar|baz|custom_baz|qux"},
]
Loading