mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-25 09:02:25 +02:00
langgraph: handle task naming for reused class methods (#3216)
This commit is contained in:
@@ -123,7 +123,11 @@ def task(
|
||||
if name is not None:
|
||||
if hasattr(func, "__func__"):
|
||||
# handle class methods
|
||||
func.__func__.__name__ = name
|
||||
# NOTE: we're modifying the instance method to avoid modifying
|
||||
# the original class method in case it's shared across multiple tasks
|
||||
instance_method = functools.partial(func.__func__, func.__self__) # type: ignore [union-attr]
|
||||
instance_method.__name__ = name # type: ignore [attr-defined]
|
||||
func = instance_method
|
||||
else:
|
||||
# handle regular functions / partials / callable classes, etc.
|
||||
func.__name__ = name
|
||||
|
||||
@@ -6277,6 +6277,7 @@ def test_named_tasks_functional() -> None:
|
||||
|
||||
# class method task
|
||||
foo = task(f.foo, name="custom_foo")
|
||||
other_foo = task(f.foo, name="other_foo")
|
||||
|
||||
# regular function task
|
||||
@task(name="custom_bar")
|
||||
@@ -6300,6 +6301,7 @@ def test_named_tasks_functional() -> None:
|
||||
@entrypoint()
|
||||
def workflow(inputs: dict) -> dict:
|
||||
fut_foo = foo(inputs)
|
||||
other_foo(inputs)
|
||||
fut_bar = bar(fut_foo.result())
|
||||
fut_baz = baz_task(fut_bar.result())
|
||||
fut_custom_baz = custom_baz_task(fut_baz.result())
|
||||
@@ -6308,6 +6310,7 @@ def test_named_tasks_functional() -> None:
|
||||
|
||||
assert list(workflow.stream("", stream_mode="updates")) == [
|
||||
{"custom_foo": "foo"},
|
||||
{"other_foo": "foo"},
|
||||
{"custom_bar": "foo|bar"},
|
||||
{"baz": "foo|bar|baz"},
|
||||
{"custom_baz": "foo|bar|baz|custom_baz"},
|
||||
|
||||
@@ -7338,6 +7338,7 @@ async def test_named_tasks_functional() -> None:
|
||||
|
||||
# class method task
|
||||
foo = task(f.foo, name="custom_foo")
|
||||
other_foo = task(f.foo, name="other_foo")
|
||||
|
||||
# regular function task
|
||||
@task(name="custom_bar")
|
||||
@@ -7361,6 +7362,7 @@ async def test_named_tasks_functional() -> None:
|
||||
@entrypoint()
|
||||
async def workflow(inputs: dict) -> dict:
|
||||
foo_result = await foo(inputs)
|
||||
await other_foo(inputs)
|
||||
bar_result = await bar(foo_result)
|
||||
baz_result = await baz_task(bar_result)
|
||||
custom_baz_result = await custom_baz_task(baz_result)
|
||||
@@ -7369,6 +7371,7 @@ async def test_named_tasks_functional() -> None:
|
||||
|
||||
assert [c async for c in workflow.astream("", stream_mode="updates")] == [
|
||||
{"custom_foo": "foo"},
|
||||
{"other_foo": "foo"},
|
||||
{"custom_bar": "foo|bar"},
|
||||
{"baz": "foo|bar|baz"},
|
||||
{"custom_baz": "foo|bar|baz|custom_baz"},
|
||||
|
||||
Reference in New Issue
Block a user