[PR #4186] Support forward references hint in routing_function #3684

Closed
opened 2026-02-20 17:49:00 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langchain-ai/langgraph/pull/4186

State: closed
Merged: No


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.

**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/4186 **State:** closed **Merged:** No --- #### 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: ```python 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.
yindo added the pull-request label 2026-02-20 17:49:00 -05:00
yindo closed this issue 2026-02-20 17:49:00 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#3684