langgraph: fix ismethod check in add_node (#3032)

Fixes #2893 #2965
This commit is contained in:
Vadym Barda
2025-01-14 17:17:50 -05:00
committed by GitHub
parent 651ee8bd24
commit f7fae7e140
2 changed files with 16 additions and 2 deletions
+5 -1
View File
@@ -361,7 +361,11 @@ class StateGraph(Graph):
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)
):
+11 -1
View File
@@ -79,11 +79,19 @@ def test_state_schema_with_type_hint():
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,
@@ -92,6 +100,8 @@ def test_state_schema_with_type_hint():
miss_all_hint,
pre_foo,
Foo(),
pre_bar,
Bar().my_node,
]
for action in actions:
@@ -112,7 +122,7 @@ def test_state_schema_with_type_hint():
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