langgraph icon indicating copy to clipboard operation
langgraph copied to clipboard

Support forward references hint in routing_function

Open RockeyDon opened this issue 7 months ago • 1 comments

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.

RockeyDon avatar Apr 07 '25 13:04 RockeyDon