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 stateful entrypoint #3007

Closed
wants to merge 2 commits into from
Closed
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
49 changes: 49 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: 61.3 ms +- 1.1 ms ......................................... fanout_to_subgraph_10x_sync: Mean +- std dev: 53.2 ms +- 0.8 ms ......................................... fanout_to_subgraph_10x_checkpoint: Mean +- std dev: 74.6 ms +- 1.0 ms ......................................... fanout_to_subgraph_10x_checkpoint_sync: Mean +- std dev: 94.9 ms +- 1.3 ms ......................................... fanout_to_subgraph_100x: Mean +- std dev: 608 ms +- 25 ms ......................................... fanout_to_subgraph_100x_sync: Mean +- std dev: 517 ms +- 11 ms ......................................... fanout_to_subgraph_100x_checkpoint: Mean +- std dev: 757 ms +- 15 ms ......................................... fanout_to_subgraph_100x_checkpoint_sync: Mean +- std dev: 956 ms +- 17 ms ......................................... react_agent_10x: Mean +- std dev: 30.9 ms +- 0.7 ms ......................................... react_agent_10x_sync: Mean +- std dev: 23.0 ms +- 0.2 ms ......................................... react_agent_10x_checkpoint: Mean +- std dev: 38.5 ms +- 0.8 ms ......................................... react_agent_10x_checkpoint_sync: Mean +- std dev: 36.7 ms +- 0.4 ms ......................................... react_agent_100x: Mean +- std dev: 344 ms +- 7 ms ......................................... react_agent_100x_sync: Mean +- std dev: 272 ms +- 2 ms ......................................... react_agent_100x_checkpoint: Mean +- std dev: 636 ms +- 5 ms ......................................... react_agent_100x_checkpoint_sync: Mean +- std dev: 630 ms +- 13 ms ......................................... wide_state_25x300: Mean +- std dev: 23.4 ms +- 0.4 ms ......................................... wide_state_25x300_sync: Mean +- std dev: 15.3 ms +- 0.1 ms ......................................... wide_state_25x300_checkpoint: Mean +- std dev: 249 ms +- 14 ms ......................................... wide_state_25x300_checkpoint_sync: Mean +- std dev: 246 ms +- 13 ms ......................................... wide_state_15x600: Mean +- std dev: 27.2 ms +- 0.6 ms ......................................... wide_state_15x600_sync: Mean +- std dev: 17.7 ms +- 0.2 ms ......................................... wide_state_15x600_checkpoint: Mean +- std dev: 429 ms +- 14 ms ......................................... wide_state_15x600_checkpoint_sync: Mean +- std dev: 425 ms +- 13 ms ......................................... wide_state_9x1200: Mean +- std dev: 27.2 ms +- 0.5 ms ......................................... wide_state_9x1200_sync: Mean +- std dev: 17.7 ms +- 0.2 ms ......................................... wide_state_9x1200_checkpoint: Mean +- std dev: 280 ms +- 13 ms ......................................... wide_state_9x1200_checkpoint_sync: Mean +- std dev: 275 ms +- 12 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_checkpoint | 796 ms | 757 ms: 1.05x faster | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_100x | 624 ms | 608 ms: 1.03x faster | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_100x_checkpoint_sync | 974 ms | 956 ms: 1.02x faster | +-----------------------------------------+---------+-----------------------+ | react_agent_100x_checkpoint | 648 ms | 636 ms: 1.02x faster | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_10x_checkpoint | 75.3 ms | 74.6 ms: 1.01x faster | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_10x_checkpoint_sync | 95.6 ms | 94.9 ms: 1.01x faster | +-----------------------------------------+---------+-----------------------+ | fanout_to_subgraph_10x | 61.7 ms | 61.3 ms: 1.01x faster | +-----------------------------------------+---------+-----------------------+ | react_agent_10x_checkpoint_sync | 37.0 ms | 36.7 ms: 1.01x faster | +-----------------------------------------+---------+-----------------------+ | react_agent_100x_sync | 273 ms | 272 ms: 1.01x faster | +-----------------------------------------+---------+-----------------------+ | wide_state_9x1200_sync | 17.8 ms | 17.7 ms: 1.01x faster | +-----------------------------------------+---------+-----------------------+ | wide_state_25x300_sync | 15.4 ms | 15.3 ms: 1.00x faster | +-----------------------------------------+---------+-----------------------+ | wide_state_15x600_sync | 17.8 ms | 17.7 ms: 1.00x faster | +-----------------------------------------+---------+-----------------------+ | Geometric mean | (ref) | 1.01x faster | +-----------------------------------------+---------+-----------------------+ Benchmark hidden because not significant (16): wide_state_9x1200_checkpoint, wide_state_9x1200_checkpoint_sync, wide_state_15x600_checkpoint, fanout_to_subgraph_100x_sync, react_agent_10x, fanout_to_subgraph_10x_sync, wide_state_9x1200, wide_state_25x300_checkpoint, wide_state_15x600, react_agent_100x, wide_state_25x300_checkpoint_sync, react_agent_100x_checkpoint_sync, wide_state_15x600_checkpoint_sync, react_agent_10x_sync, react_agent_10x_checkpoint, wide_state_25x300
import concurrent
import concurrent.futures
import functools
Expand All @@ -9,8 +9,10 @@
Awaitable,
Callable,
Optional,
Type,
TypeVar,
Union,
get_type_hints,
overload,
)

Expand All @@ -20,6 +22,7 @@
from langgraph.channels.last_value import LastValue
from langgraph.checkpoint.base import BaseCheckpointSaver
from langgraph.constants import END, START, TAG_HIDDEN
from langgraph.graph.state import StateGraph
from langgraph.pregel import Pregel
from langgraph.pregel.call import get_runnable_for_func
from langgraph.pregel.read import PregelNode
Expand Down Expand Up @@ -47,6 +50,14 @@
return fut


def get_store() -> BaseStore:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's for accessing the store from inside the tasks?

"""Get the current store."""
from langgraph.constants import CONFIG_KEY_STORE
from langgraph.utils.config import get_configurable

return get_configurable()[CONFIG_KEY_STORE]


@overload
def task(
*, retry: Optional[RetryPolicy] = None
Expand Down Expand Up @@ -153,3 +164,41 @@
)

return _imp


def g(
*,
checkpointer: Optional[BaseCheckpointSaver] = None,
store: Optional[BaseStore] = None,
input: Optional[Type[Any]] = None,
output: Optional[Type[Any]] = None,
) -> Callable[[types.FunctionType], Pregel]:
"""Generate a single node StateGraph from a callable.

Args:
checkpointer: Checkpointer to use for the graph.
store: Store to use for the graph.
input: Schema projection applied to run input to the graph.
output: Schema projection applied to run output from the graph.

Returns:
Callable: A decorator that can be applied to a function to generate a Pregel graph.
"""

def decorator(func: Callable) -> Pregel:
"""Decorator to generate a Pregel graph from a callable."""
node_name = func.__name__
type_hints = get_type_hints(func)
if not type_hints:
state_schema = dict
else:
state_schema = list(type_hints.values())[0]
builder = (
StateGraph(state_schema=state_schema, input=input, output=output)
.add_node(func)
.set_entry_point(node_name)
)
graph = builder.compile(checkpointer=checkpointer, store=store)
return graph

return decorator
Loading