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 1 commit
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: 60.9 ms +- 2.5 ms ......................................... fanout_to_subgraph_10x_sync: Mean +- std dev: 53.4 ms +- 1.0 ms ......................................... WARNING: the benchmark result may be unstable * the standard deviation (13.0 ms) is 13% of the mean (98.1 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: 98.1 ms +- 13.0 ms ......................................... fanout_to_subgraph_10x_checkpoint_sync: Mean +- std dev: 98.1 ms +- 1.3 ms ......................................... fanout_to_subgraph_100x: Mean +- std dev: 674 ms +- 40 ms ......................................... fanout_to_subgraph_100x_sync: Mean +- std dev: 518 ms +- 10 ms ......................................... fanout_to_subgraph_100x_checkpoint: Mean +- std dev: 1.04 sec +- 0.06 sec ......................................... fanout_to_subgraph_100x_checkpoint_sync: Mean +- std dev: 959 ms +- 26 ms ......................................... react_agent_10x: Mean +- std dev: 31.7 ms +- 1.0 ms ......................................... react_agent_10x_sync: Mean +- std dev: 23.5 ms +- 0.5 ms ......................................... react_agent_10x_checkpoint: Mean +- std dev: 49.2 ms +- 1.5 ms ......................................... react_agent_10x_checkpoint_sync: Mean +- std dev: 38.4 ms +- 1.2 ms ......................................... react_agent_100x: Mean +- std dev: 358 ms +- 10 ms ......................................... react_agent_100x_sync: Mean +- std dev: 295 ms +- 14 ms ......................................... react_agent_100x_checkpoint: Mean +- std dev: 1.04 sec +- 0.03 sec ......................................... react_agent_100x_checkpoint_sync: Mean +- std dev: 910 ms +- 20 ms ......................................... wide_state_25x300: Mean +- std dev: 24.6 ms +- 0.5 ms ......................................... wide_state_25x300_sync: Mean +- std dev: 15.9 ms +- 0.2 ms ......................................... wide_state_25x300_checkpoint: Mean +- std dev: 288 ms +- 4 ms ......................................... wide_state_25x300_checkpoint_sync: Mean +- std dev: 271 ms +- 3 ms ......................................... wide_state_15x600: Mean +- std dev: 28.8 ms +- 0.6 ms ......................................... wide_state_15x600_sync: Mean +- std dev: 18.3 ms +- 0.4 ms ......................................... wide_state_15x600_checkpoint: Mean +- std dev: 492 ms +- 8 ms ......................................... wide_state_15x600_checkpoint_sync: Mean +- std dev: 473 ms +- 7 ms ......................................... wide_state_9x1200: Mean +- std dev: 28.9 ms +- 0.6 ms ......................................... wide_state_9x1200_sync: Mean +- std dev: 18.3 ms +- 0.2 ms ......................................... wide_state_9x1200_checkpoint: Mean +- std dev: 321 ms +- 5 ms ......................................... wide_state_9x1200_checkpoint_sync: Mean +- std dev: 305 ms +- 5 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 | +=========================================+==========+========================+ | wide_state_25x300 | 24.3 ms | 24.6 ms: 1.01x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_100x_sync | 510 ms | 518 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_15x600_checkpoint_sync | 465 ms | 473 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_9x1200_checkpoint_sync | 299 ms | 305 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_15x600 | 28.2 ms | 28.8 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_9x1200_checkpoint | 315 ms | 321 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_25x300_checkpoint_sync | 266 ms | 271 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_9x1200 | 28.3 ms | 28.9 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_9x1200_sync | 17.9 ms | 18.3 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_15x600_sync | 17.9 ms | 18.3 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_15x600_checkpoint | 481 ms | 492 ms: 1.02x slower | +-----------------------------------------+----------+------------------------+ | wide_state_25x300_sync | 15.5 ms | 15.9 ms: 1.03x slower | +-----------------------------------------+----------+------------------------+ | react_agent_10x | 30.8 ms | 31.7 ms: 1.03x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_100x_checkpoint_sync | 928 ms | 959 ms: 1.03x slower | +-----------------------------------------+----------+------------------------+ | react_agent_100x | 346 ms | 358 ms: 1.03x slower | +-----------------------------------------+----------+------------------------+ | wide_state_25x300_checkpoint | 279 ms | 288 ms: 1.04x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_10x_sync | 51.6 ms | 53.4 ms: 1.04x slower | +-----------------------------------------+----------+------------------------+ | react_agent_100x_checkpoint | 1.01 sec | 1.04 sec: 1.04x slower | +-----------------------------------------+----------+------------------------+ | react_agent_100x_sync | 284 ms | 295 ms: 1.04x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_10x_checkpoint_sync | 94.3 ms | 98.1 ms: 1.04x slower | +-----------------------------------------+----------+------------------------+ | react_agent_10x_sync | 22.5 ms | 23.5 ms: 1.04x slower | +-----------------------------------------+----------+------------------------+ | react_agent_10x_checkpoint_sync | 36.8 ms | 38.4 ms: 1.04x slower | +-----------------------------------------+----------+------------------------+ | react_agent_10x_checkpoint | 46.9 ms | 49.2 ms: 1.05x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subgraph_100x_checkpoint | 984 ms | 1.04 sec: 1.05x slower | +-----------------------------------------+----------+------------------------+ | fanout_to_subg
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
36 changes: 36 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,38 @@ 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"}
Loading