[PR #255] [CLOSED] Implement Enum-Based Declarations for Graph Nodes #1335

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

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/langgraph/pull/255
Author: @Undertone0809
Created: 4/1/2024
Status: Closed

Base: mainHead: zeeland/add-graph-node-enum-support


📝 Commits (2)

  • 0b73086 Add graph add_node enum type support
  • 755a24a Merge branch 'main' into zeeland/add-graph-node-enum-support

📊 Changes

3 files changed (+49 additions, -18 deletions)

View changed files

📝 examples/plan-and-execute/plan-and-execute.ipynb (+16 -10)
📝 langgraph/graph/graph.py (+25 -6)
📝 langgraph/graph/state.py (+8 -2)

📄 Description

Summary

  • Replaced string-based node definitions with enums in plan-and-execute.ipynb for better readability and fewer errors.
  • Updated graph.py and state.py to accept enums in graph operations, enhancing code maintainability.
  • Added conversion logic for enum to string in affected methods, ensuring backward compatibility.

Original:

from langgraph.graph import StateGraph, END

workflow = StateGraph(PlanExecute)

# Add the plan node
workflow.add_node("planner", plan_step)

# Add the execution step
workflow.add_node("agent", execute_step)

# Add a replan node
workflow.add_node("replan", replan_step)

workflow.set_entry_point("planner")

# From plan we go to agent
workflow.add_edge("planner", "agent")

# From agent, we replan
workflow.add_edge("agent", "replan")

workflow.add_conditional_edges(
    "replan",
    # Next, we pass in the function that will determine which node is called next.
    should_end,
    {
        # If `tools`, then we call the tool node.
        True: END,
        False: "agent",
    },
)

# Finally, we compile it!
# This compiles it into a LangChain Runnable,
# meaning you can use it as you would any other runnable
app = workflow.compile()

After:

from langgraph.graph import StateGraph, END
from enum import Enum

class NodeType(Enum):
    PLAN = "planner"
    EXECUTE = "agent"
    REPLAN = "replan"

workflow = StateGraph(PlanExecute)

# Add the plan node
workflow.add_node(NodeType.PLAN, plan_step)

# Add the execution step
workflow.add_node(NodeType.EXECUTE, execute_step)

# Add a replan node
workflow.add_node(NodeType.REPLAN, replan_step)

workflow.set_entry_point(NodeType.PLAN)

# From plan we go to agent
workflow.add_edge(NodeType.PLAN, NodeType.EXECUTE)

# From agent, we replan
workflow.add_edge(NodeType.EXECUTE, NodeType.REPLAN)

workflow.add_conditional_edges(
    NodeType.REPLAN,
    # Next, we pass in the function that will determine which node is called next.
    should_end,
    {
        # If `tools`, then we call the tool node.
        True: END,
        False: NodeType.EXECUTE,
    },
)

# Finally, we compile it!
# This compiles it into a LangChain Runnable,
# meaning you can use it as you would any other runnable
app = workflow.compile()

Reason for changes:

Adopting enum-based node declarations enhances code clarity and standardizes data types, reducing the likelihood of errors due to misspelled strings. This approach streamlines development and maintenance processes by providing a more structured and error-resistant method for defining nodes within the codebase.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/langgraph/pull/255 **Author:** [@Undertone0809](https://github.com/Undertone0809) **Created:** 4/1/2024 **Status:** ❌ Closed **Base:** `main` ← **Head:** `zeeland/add-graph-node-enum-support` --- ### 📝 Commits (2) - [`0b73086`](https://github.com/langchain-ai/langgraph/commit/0b73086a60d220be1ac01b070bec2bb8ed0d22f1) Add graph add_node enum type support - [`755a24a`](https://github.com/langchain-ai/langgraph/commit/755a24a9d958b9b81856a547cc490f43e8b51b84) Merge branch 'main' into zeeland/add-graph-node-enum-support ### 📊 Changes **3 files changed** (+49 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `examples/plan-and-execute/plan-and-execute.ipynb` (+16 -10) 📝 `langgraph/graph/graph.py` (+25 -6) 📝 `langgraph/graph/state.py` (+8 -2) </details> ### 📄 Description ## Summary - Replaced string-based node definitions with enums in plan-and-execute.ipynb for better readability and fewer errors. - Updated graph.py and state.py to accept enums in graph operations, enhancing code maintainability. - Added conversion logic for enum to string in affected methods, ensuring backward compatibility. Original: ```python from langgraph.graph import StateGraph, END workflow = StateGraph(PlanExecute) # Add the plan node workflow.add_node("planner", plan_step) # Add the execution step workflow.add_node("agent", execute_step) # Add a replan node workflow.add_node("replan", replan_step) workflow.set_entry_point("planner") # From plan we go to agent workflow.add_edge("planner", "agent") # From agent, we replan workflow.add_edge("agent", "replan") workflow.add_conditional_edges( "replan", # Next, we pass in the function that will determine which node is called next. should_end, { # If `tools`, then we call the tool node. True: END, False: "agent", }, ) # Finally, we compile it! # This compiles it into a LangChain Runnable, # meaning you can use it as you would any other runnable app = workflow.compile() ``` After: ```python from langgraph.graph import StateGraph, END from enum import Enum class NodeType(Enum): PLAN = "planner" EXECUTE = "agent" REPLAN = "replan" workflow = StateGraph(PlanExecute) # Add the plan node workflow.add_node(NodeType.PLAN, plan_step) # Add the execution step workflow.add_node(NodeType.EXECUTE, execute_step) # Add a replan node workflow.add_node(NodeType.REPLAN, replan_step) workflow.set_entry_point(NodeType.PLAN) # From plan we go to agent workflow.add_edge(NodeType.PLAN, NodeType.EXECUTE) # From agent, we replan workflow.add_edge(NodeType.EXECUTE, NodeType.REPLAN) workflow.add_conditional_edges( NodeType.REPLAN, # Next, we pass in the function that will determine which node is called next. should_end, { # If `tools`, then we call the tool node. True: END, False: NodeType.EXECUTE, }, ) # Finally, we compile it! # This compiles it into a LangChain Runnable, # meaning you can use it as you would any other runnable app = workflow.compile() ``` ## Reason for changes: Adopting enum-based node declarations enhances code clarity and standardizes data types, reducing the likelihood of errors due to misspelled strings. This approach streamlines development and maintenance processes by providing a more structured and error-resistant method for defining nodes within the codebase. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-20 17:44:29 -05:00
yindo closed this issue 2026-02-20 17:44:29 -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#1335