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: use input schema from conditional edge #2516

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
35 changes: 30 additions & 5 deletions libs/langgraph/langgraph/graph/state.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dataclasses

Check notice on line 1 in libs/langgraph/langgraph/graph/state.py

View workflow job for this annotation

GitHub Actions / benchmark

Benchmark results

......................................... fanout_to_subgraph_10x: Mean +- std dev: 59.9 ms +- 2.2 ms ......................................... fanout_to_subgraph_10x_sync: Mean +- std dev: 52.6 ms +- 1.0 ms ......................................... WARNING: the benchmark result may be unstable * the standard deviation (11.0 ms) is 12% of the mean (93.8 ms) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. fanout_to_subgraph_10x_checkpoint: Mean +- std dev: 93.8 ms +- 11.0 ms ......................................... fanout_to_subgraph_10x_checkpoint_sync: Mean +- std dev: 96.6 ms +- 1.0 ms ......................................... fanout_to_subgraph_100x: Mean +- std dev: 625 ms +- 36 ms ......................................... fanout_to_subgraph_100x_sync: Mean +- std dev: 507 ms +- 8 ms ......................................... fanout_to_subgraph_100x_checkpoint: Mean +- std dev: 949 ms +- 46 ms ......................................... fanout_to_subgraph_100x_checkpoint_sync: Mean +- std dev: 943 ms +- 21 ms ......................................... react_agent_10x: Mean +- std dev: 31.2 ms +- 0.7 ms ......................................... react_agent_10x_sync: Mean +- std dev: 22.8 ms +- 0.3 ms ......................................... react_agent_10x_checkpoint: Mean +- std dev: 47.5 ms +- 0.9 ms ......................................... react_agent_10x_checkpoint_sync: Mean +- std dev: 37.3 ms +- 0.5 ms ......................................... react_agent_100x: Mean +- std dev: 348 ms +- 6 ms ......................................... react_agent_100x_sync: Mean +- std dev: 275 ms +- 3 ms ......................................... react_agent_100x_checkpoint: Mean +- std dev: 951 ms +- 17 ms ......................................... react_agent_100x_checkpoint_sync: Mean +- std dev: 854 ms +- 14 ms ......................................... wide_state_25x300: Mean +- std dev: 24.3 ms +- 0.5 ms ......................................... wide_state_25x300_sync: Mean +- std dev: 15.5 ms +- 0.2 ms ......................................... wide_state_25x300_checkpoint: Mean +- std dev: 279 ms +- 3 ms ......................................... wide_state_25x300_checkpoint_sync: Mean +- std dev: 268 ms +- 5 ms ......................................... wide_state_15x600: Mean +- std dev: 28.4 ms +- 0.5 ms ......................................... wide_state_15x600_sync: Mean +- std dev: 18.0 ms +- 0.3 ms ......................................... wide_state_15x600_checkpoint: Mean +- std dev: 481 ms +- 8 ms ......................................... wide_state_15x600_checkpoint_sync: Mean +- std dev: 467 ms +- 9 ms ......................................... wide_state_9x1200: Mean +- std dev: 28.4 ms +- 0.6 ms ......................................... wide_state_9x1200_sync: Mean +- std dev: 17.9 ms +- 0.1 ms ......................................... wide_state_9x1200_checkpoint: Mean +- std dev: 314 ms +- 3 ms ......................................... wide_state_9x1200_checkpoint_sync: Mean +- std dev: 301 ms +- 4 ms

Check notice on line 1 in libs/langgraph/langgraph/graph/state.py

View workflow job for this annotation

GitHub Actions / benchmark

Comparison against main

+-----------------------------------------+----------+-----------------------+ | Benchmark | main | changes | +=========================================+==========+=======================+ | react_agent_100x_checkpoint_sync | 914 ms | 854 ms: 1.07x faster | +-----------------------------------------+----------+-----------------------+ | react_agent_100x_checkpoint | 1.01 sec | 951 ms: 1.06x faster | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_100x_checkpoint | 984 ms | 949 ms: 1.04x faster | +-----------------------------------------+----------+-----------------------+ | react_agent_100x_sync | 284 ms | 275 ms: 1.03x faster | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_10x | 61.3 ms | 59.9 ms: 1.02x faster | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_100x_sync | 510 ms | 507 ms: 1.01x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_9x1200_checkpoint | 315 ms | 314 ms: 1.00x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_15x600 | 28.2 ms | 28.4 ms: 1.01x slower | +-----------------------------------------+----------+-----------------------+ | wide_state_25x300_checkpoint_sync | 266 ms | 268 ms: 1.01x slower | +-----------------------------------------+----------+-----------------------+ | wide_state_9x1200_checkpoint_sync | 299 ms | 301 ms: 1.01x slower | +-----------------------------------------+----------+-----------------------+ | react_agent_10x_sync | 22.5 ms | 22.8 ms: 1.01x slower | +-----------------------------------------+----------+-----------------------+ | react_agent_10x | 30.8 ms | 31.2 ms: 1.01x slower | +-----------------------------------------+----------+-----------------------+ | react_agent_10x_checkpoint | 46.9 ms | 47.5 ms: 1.01x slower | +-----------------------------------------+----------+-----------------------+ | react_agent_10x_checkpoint_sync | 36.8 ms | 37.3 ms: 1.01x slower | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_100x | 616 ms | 625 ms: 1.02x slower | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_100x_checkpoint_sync | 928 ms | 943 ms: 1.02x slower | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_10x_sync | 51.6 ms | 52.6 ms: 1.02x slower | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_10x_checkpoint_sync | 94.3 ms | 96.6 ms: 1.02x slower | +-----------------------------------------+----------+-----------------------+ | Geometric mean | (ref) | 1.00x faster | +-----------------------------------------+----------+-----------------------+ Benchmark hidden because not significant (10): wide_state_9x1200_sync, wide_state_25x300_sync, wide_state_25x300_checkpoint, wide_state_25x300, wide_state_15x600_checkpoint, wide_state_15x600_sync, wide_state_15x600_checkpoint_sync, react_agent_100x, wide_state_9x1200, fanout_to_subgraph_10x_checkpoint
import inspect
import logging
import typing
Expand Down Expand Up @@ -760,12 +760,37 @@
config, cast(Sequence[Union[Send, ChannelWriteEntry]], writes)
)

# detect branch input schema
input = None

# detect input schema annotation in the branch callable
try:
if (
isinstance(branch.path, RunnableCallable)
Copy link
Contributor

Choose a reason for hiding this comment

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

What if it's a RUnnableLambda?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yea might need to check for that too just in case

and (
isfunction(branch.path.func)
or ismethod(getattr(branch.path.func, "__call__", None))
)
and (
hints := get_type_hints(getattr(branch.path.func, "__call__"))
or get_type_hints(branch.path.func)
)
):
first_parameter_name = next(
iter(
inspect.signature(
cast(FunctionType, branch.path.func)
).parameters.keys()
)
)
if input_hint := hints.get(first_parameter_name):
if isinstance(input_hint, type) and get_type_hints(input_hint):
input = input_hint
except (TypeError, StopIteration):
pass

schema = input or self.builder.schema
# attach branch publisher
schema = (
self.builder.nodes[start].input
if start in self.builder.nodes
else self.builder.schema
)
self.nodes[start] |= branch.run(
branch_writer,
_get_state_reader(self.builder, schema) if with_reader else None,
Expand Down
66 changes: 66 additions & 0 deletions libs/langgraph/tests/test_state.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import operator
import warnings
from dataclasses import dataclass, field
from typing import Annotated as Annotated2
Expand Down Expand Up @@ -318,3 +319,68 @@ def class_method(self, state):

# class method
assert _get_node_name(MyClass().class_method) == "class_method"


def test_input_schema_conditional_edge():
vbarda marked this conversation as resolved.
Show resolved Hide resolved
class OverallState(TypedDict):
foo: Annotated[int, operator.add]
bar: str

class PrivateState(TypedDict):
baz: str

builder = StateGraph(OverallState)

def node_1(state: OverallState):
return {"foo": 1, "baz": "bar"}

def node_2(state: PrivateState):
return {"foo": 1, "bar": state["baz"]}

def node_3(state: OverallState):
return {"foo": 1}

def router(state: OverallState):
if state["foo"] == 2:
return "node_3"
else:
return "__end__"

builder.add_node(node_1)
builder.add_node(node_2)
builder.add_node(node_3)
builder.add_conditional_edges("node_2", router)
builder.add_edge("__start__", "node_1")
builder.add_edge("node_1", "node_2")
graph = builder.compile()
assert graph.invoke({"foo": 0}) == {"foo": 3, "bar": "bar"}


def test_private_input_schema_conditional_edge():
class OverallState(TypedDict):
foo: Annotated[int, operator.add]
bar: str

class PrivateState(TypedDict):
baz: str

builder = StateGraph(OverallState)

def node_1(state: OverallState):
return {"foo": 1, "baz": "meow"}

def node_2(state: PrivateState):
return {"foo": 1, "bar": state["baz"]}

def router(state: PrivateState):
if state["baz"] == "meow":
return "node_2"
else:
return "__end__"

builder.add_node(node_1)
builder.add_node(node_2)
builder.add_conditional_edges("node_1", router)
builder.add_edge("__start__", "node_1")
graph = builder.compile()
assert graph.invoke({"foo": 0}) == {"foo": 2, "bar": "meow"}
Loading