[PR #6893] fix(langgraph): use TypeVar for add_conditional_edges path_map key type #5383

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

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

State: open
Merged: No


Summary

Fixes #6540.

add_conditional_edges declared path_map as dict[Hashable, str]. Because dict is invariant in its key type, passing a dict[str, str] produced a false-positive type error in mypy and Pyright — even though str is a subtype of Hashable.

  • Introduces _K = TypeVar("_K", bound=Hashable) and uses it in the signatures of add_conditional_edges and set_conditional_entry_point, linking the path callable's return type to the path_map key type
  • Changes BranchSpec.ends and BranchSpec.from_path internals to dict[Any, str]NamedTuple does not support Generic, and the keys are only used for dict lookup and str() conversion internally
  • Adds a regression test in test_type_checking.py documenting that dict[str, str] is accepted without # type: ignore

Test plan

  • make format — no changes
  • make lint — ruff + mypy clean (Success: no issues found in 64 source files)
  • make test — 948 passed, 5 skipped, 0 failures

🤖 Generated with Claude Code

**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/6893 **State:** open **Merged:** No --- ## Summary Fixes #6540. `add_conditional_edges` declared `path_map` as `dict[Hashable, str]`. Because `dict` is **invariant** in its key type, passing a `dict[str, str]` produced a false-positive type error in mypy and Pyright — even though `str` is a subtype of `Hashable`. - Introduces `_K = TypeVar("_K", bound=Hashable)` and uses it in the signatures of `add_conditional_edges` and `set_conditional_entry_point`, linking the path callable's return type to the path_map key type - Changes `BranchSpec.ends` and `BranchSpec.from_path` internals to `dict[Any, str]` — `NamedTuple` does not support `Generic`, and the keys are only used for dict lookup and `str()` conversion internally - Adds a regression test in `test_type_checking.py` documenting that `dict[str, str]` is accepted without `# type: ignore` ## Test plan - [x] `make format` — no changes - [x] `make lint` — ruff + mypy clean (`Success: no issues found in 64 source files`) - [x] `make test` — 948 passed, 5 skipped, 0 failures 🤖 Generated with [Claude Code](https://claude.com/claude-code)
yindo added the pull-request label 2026-02-20 17:51:37 -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#5383