langgraph
langgraph copied to clipboard
Support forward references hint in routing_function
Problem
When users use forward references in the routing_function, it throws NameError.
this code works in 0.2.76 but fails in 0.3.21:
if typing.TYPE_CHECKING:
from state import State
def node_1(state: 'State'):
return state
def route_func(state: 'State'):
return 'node_2'
def node_2(state: 'State'):
return state
builder.add_node("node_1", node_1)
builder.add_node("node_2", node_2)
builder.add_edge(START, "node_1")
builder.add_conditional_edges("node_1", route_func)
Solution
Added a try-except block around the get_type_hints call.
If the function raises an exception while resolving type hints, it simply skips the unresolved annotations, as if user had not provide an annotation in the router.