Skip to content

Commit

Permalink
langgraph: fix ismethod check in add_node (#3032)
Browse files Browse the repository at this point in the history
Fixes #2893 #2965
  • Loading branch information
vbarda authored Jan 14, 2025
1 parent 651ee8b commit f7fae7e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion libs/langgraph/langgraph/graph/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ def add_node(

ends = EMPTY_SEQ
try:
if (isfunction(action) or ismethod(getattr(action, "__call__", None))) and (
if (
isfunction(action)
or ismethod(action)
or ismethod(getattr(action, "__call__", None))
) and (
hints := get_type_hints(getattr(action, "__call__"))
or get_type_hints(action)
):
Expand Down
12 changes: 11 additions & 1 deletion libs/langgraph/tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,19 @@ def miss_all_hint(state, config):
def pre_foo(_) -> FooState:
return {"foo": "bar"}

def pre_bar(_) -> FooState:
return {"foo": "bar"}

class Foo:
def __call__(self, state: FooState) -> OutputState:
assert state.pop("foo") == "bar"
return {"input_state": state}

class Bar:
def my_node(self, state: FooState) -> OutputState:
assert state.pop("foo") == "bar"
return {"input_state": state}

graph = StateGraph(InputState, output=OutputState)
actions = [
complete_hint,
Expand All @@ -92,6 +100,8 @@ def __call__(self, state: FooState) -> OutputState:
miss_all_hint,
pre_foo,
Foo(),
pre_bar,
Bar().my_node,
]

for action in actions:
Expand All @@ -112,7 +122,7 @@ def get_name(action) -> str:
foo_state = FooState(foo="bar")
for i, c in enumerate(graph.stream(input_state, stream_mode="updates")):
node_name = get_name(actions[i])
if node_name == get_name(pre_foo):
if node_name in {"pre_foo", "pre_bar"}:
assert c[node_name] == foo_state
else:
assert c[node_name] == output_state
Expand Down

0 comments on commit f7fae7e

Please sign in to comment.