[PR #6888] fix(langgraph): handle END in conditional edge path_map lookup #5382

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/6888

State: open
Merged: No


Summary

  • Fix KeyError('__end__') when a conditional router returns END but path_map does not include an explicit END mapping
  • Treat END as a pass-through value in BranchSpec._finish(), consistent with the behavior when no path_map is provided
  • Add regression test covering both the termination and normal-flow paths

Details

In _branch.py, the _finish method looks up each routing result in self.ends (the resolved path_map). When the router returns END ("__end__") and path_map doesn't explicitly include it, the lookup raises KeyError. Since END is a special terminal node, it should always be treated as a valid destination regardless of path_map contents.

Before:

destinations = [r if isinstance(r, Send) else self.ends[r] for r in result]

After:

destinations = [r if isinstance(r, Send) or r == END else self.ends[r] for r in result]

This is consistent with how END is handled when self.ends is None (no path_map provided), where the result passes through directly.

Fixes #6770

Test plan

  • Added test_conditional_edges_end_not_in_path_map regression test
  • Verified the exact reproduction from the issue now works
  • All 570 existing pregel tests pass (excluding postgres/redis which require external services)
  • All 18 state graph tests pass
**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/6888 **State:** open **Merged:** No --- ## Summary - Fix `KeyError('__end__')` when a conditional router returns `END` but `path_map` does not include an explicit `END` mapping - Treat `END` as a pass-through value in `BranchSpec._finish()`, consistent with the behavior when no `path_map` is provided - Add regression test covering both the termination and normal-flow paths ## Details In `_branch.py`, the `_finish` method looks up each routing result in `self.ends` (the resolved `path_map`). When the router returns `END` (`"__end__"`) and `path_map` doesn't explicitly include it, the lookup raises `KeyError`. Since `END` is a special terminal node, it should always be treated as a valid destination regardless of `path_map` contents. **Before:** ```python destinations = [r if isinstance(r, Send) else self.ends[r] for r in result] ``` **After:** ```python destinations = [r if isinstance(r, Send) or r == END else self.ends[r] for r in result] ``` This is consistent with how `END` is handled when `self.ends` is `None` (no `path_map` provided), where the result passes through directly. Fixes #6770 ## Test plan - [x] Added `test_conditional_edges_end_not_in_path_map` regression test - [x] Verified the exact reproduction from the issue now works - [x] All 570 existing pregel tests pass (excluding postgres/redis which require external services) - [x] All 18 state graph tests pass
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#5382