[PR #6572] Fix: Command(goto) now overrides explicit edges from add_edge() #5135

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

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

State: closed
Merged: No


Description

When a node returns Command(goto="target"), the graph was executing both the goto target AND the next node from add_edge(), merging their states. This violates the expected behavior where goto should completely override the explicit edge.

Root Cause

Edge writers in langgraph/graph/state.py were unconditionally writing to default branch channels even when a Command(goto=...) was present, causing both the goto target and the default successor to trigger.

Solution

Modified attach_edge(), attach_branch(), and related methods to skip default branch writes when Command.goto is detected by:

  • Adding _has_goto() helper to detect Command with goto
  • Adding _skip_branch_if_goto() mapper function that returns SKIP_WRITE when goto is present
  • Updating 3 edge writer types (direct, join, conditional) to use the mapper

Changes

  • File: libs/langgraph/langgraph/graph/state.py
  • Lines changed: ~30 lines added
  • Backward compatible: Yes - only affects nodes returning Command with goto

Testing

Verified with:

  • Original reproduction script
  • Direct edge override test
  • Join edge override test
  • Normal edges work without goto
  • Multiple goto targets
  • Conditional edges with goto

Closes #6571

**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/6572 **State:** closed **Merged:** No --- ## Description When a node returns `Command(goto="target")`, the graph was executing both the goto target AND the next node from `add_edge()`, merging their states. This violates the expected behavior where `goto` should completely override the explicit edge. ## Root Cause Edge writers in `langgraph/graph/state.py` were unconditionally writing to default branch channels even when a `Command(goto=...)` was present, causing both the goto target and the default successor to trigger. ## Solution Modified `attach_edge()`, `attach_branch()`, and related methods to skip default branch writes when `Command.goto` is detected by: - Adding `_has_goto()` helper to detect Command with goto - Adding `_skip_branch_if_goto()` mapper function that returns `SKIP_WRITE` when goto is present - Updating 3 edge writer types (direct, join, conditional) to use the mapper ## Changes - **File**: `libs/langgraph/langgraph/graph/state.py` - **Lines changed**: ~30 lines added - **Backward compatible**: ✅ Yes - only affects nodes returning Command with goto ## Testing Verified with: - ✅ Original reproduction script - ✅ Direct edge override test - ✅ Join edge override test - ✅ Normal edges work without goto - ✅ Multiple goto targets - ✅ Conditional edges with goto Closes #6571
yindo added the pull-request label 2026-02-20 17:51:16 -05:00
yindo closed this issue 2026-02-20 17:51:16 -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#5135