[PR #6543] Fix type signature for add_conditional_edges path_map parameter #5111

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

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

State: closed
Merged: No


Summary

Fixes the type signature incompatibility in add_conditional_edges where dict[str, str] could not be assigned to dict[Hashable, str] due to Python's type invariance.

Problem

The current type signature uses dict[Hashable, str] for the path_map parameter:

def add_conditional_edges(
    self,
    source: str,
    path: Callable[..., Hashable | Sequence[Hashable]],
    path_map: dict[Hashable, str] | list[str] | None = None,
) -> Self:

This causes pyright/mypy errors when passing a dict[str, str]:

path_map: dict[str, str] = {"yes": "approved", "no": "rejected"}
graph.add_conditional_edges("__start__", check_approval, path_map)
# Error: dict[str, str] is not assignable to dict[Hashable, str]

Solution

Introduced a TypeVar bound to Hashable that links the return type of path with the key type of path_map:

_PathReturnT = TypeVar("_PathReturnT", bound=Hashable)

def add_conditional_edges(
    self,
    source: str,
    path: Callable[..., _PathReturnT | Sequence[_PathReturnT]]
        | Callable[..., Awaitable[_PathReturnT | Sequence[_PathReturnT]]]
        | Runnable[Any, _PathReturnT | Sequence[_PathReturnT]],
    path_map: dict[_PathReturnT, str] | list[str] | None = None,
) -> Self:

This allows:

  • dict[str, str] when path returns str
  • dict[MyEnum, str] when path returns MyEnum
  • Type safety is preserved since _PathReturnT is bound to Hashable

Testing

Verified the fix works with the example from the issue:

from typing import TypedDict
from langgraph.graph import END, StateGraph

class State(TypedDict):
    approved: bool

def check_approval(state: State) -> str:
    return "yes" if state["approved"] else "no"

graph = StateGraph(State)
graph.add_node("approved", approved_node)
graph.add_node("rejected", rejected_node)

# This now works without type errors!
path_map: dict[str, str] = {"yes": "approved", "no": "rejected"}
graph.add_conditional_edges("__start__", check_approval, path_map)

Closes #6540

**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/6543 **State:** closed **Merged:** No --- ## Summary Fixes the type signature incompatibility in `add_conditional_edges` where `dict[str, str]` could not be assigned to `dict[Hashable, str]` due to Python's type invariance. ## Problem The current type signature uses `dict[Hashable, str]` for the `path_map` parameter: ```python def add_conditional_edges( self, source: str, path: Callable[..., Hashable | Sequence[Hashable]], path_map: dict[Hashable, str] | list[str] | None = None, ) -> Self: ``` This causes pyright/mypy errors when passing a `dict[str, str]`: ```python path_map: dict[str, str] = {"yes": "approved", "no": "rejected"} graph.add_conditional_edges("__start__", check_approval, path_map) # Error: dict[str, str] is not assignable to dict[Hashable, str] ``` ## Solution Introduced a `TypeVar` bound to `Hashable` that links the return type of `path` with the key type of `path_map`: ```python _PathReturnT = TypeVar("_PathReturnT", bound=Hashable) def add_conditional_edges( self, source: str, path: Callable[..., _PathReturnT | Sequence[_PathReturnT]] | Callable[..., Awaitable[_PathReturnT | Sequence[_PathReturnT]]] | Runnable[Any, _PathReturnT | Sequence[_PathReturnT]], path_map: dict[_PathReturnT, str] | list[str] | None = None, ) -> Self: ``` This allows: - `dict[str, str]` when `path` returns `str` - `dict[MyEnum, str]` when `path` returns `MyEnum` - Type safety is preserved since `_PathReturnT` is bound to `Hashable` ## Testing Verified the fix works with the example from the issue: ```python from typing import TypedDict from langgraph.graph import END, StateGraph class State(TypedDict): approved: bool def check_approval(state: State) -> str: return "yes" if state["approved"] else "no" graph = StateGraph(State) graph.add_node("approved", approved_node) graph.add_node("rejected", rejected_node) # This now works without type errors! path_map: dict[str, str] = {"yes": "approved", "no": "rejected"} graph.add_conditional_edges("__start__", check_approval, path_map) ``` Closes #6540
yindo added the pull-request label 2026-02-20 17:51:14 -05:00
yindo closed this issue 2026-02-20 17:51:14 -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#5111