State Transitions Not working as Expected in Agent Setup #440

Closed
opened 2026-02-20 17:40:08 -05:00 by yindo · 4 comments
Owner

Originally created by @jeffersonRosman on GitHub (Feb 6, 2025).

Checked other resources

  • This is a bug, not a usage question. For questions, please use GitHub Discussions.
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

Example Code

import logging
from langgraph.graph import MessagesState, StateGraph
from langgraph.checkpoint.memory import MemorySaver
from langgraph.llm import HumanMessage

# Define the AgentState class
class AgentState(MessagesState):
    error: bool
    error_msg: str
    code: str
    external_libs: bool

# Define the nodes
def code_generator(state: AgentState):
    prompt = "Can you provide me with Python code for a function that takes a string as input and returns True if the string is a palindrome and False otherwise? The function should not use any libraries."
    msg = [HumanMessage(content=prompt)]
    function_str = "def is_palindrome(s): return s == s[::-1]"
    return {"messages": [function_str], "code": function_str}

def validate_code(state: AgentState):
    function = state["code"]
    try:
        exec(function)
        return {"error": False}
    except Exception as e:
        return {"error": True, "error_msg": str(e)}

def validate_external_libs(state: AgentState):
    return {"external_libs": False}

def install_external_libs(state: AgentState):
    return {"external_libs": False}

def correct_python_erros(state: AgentState):
    corrected_function_str = "def is_palindrome(s): return s == s[::-1]"
    return {"messages": [corrected_function_str], "code": corrected_function_str, "error": False}

def execute_python_function(state: AgentState):
    function = state["code"]
    exec(function)
    return "Done"

# Define the edges
def check_code(state: AgentState):
    error = state.get("error", None)
    if error:
        return "correct_python_erros"
    else:
        return "validate_external_libs"

def check_external_libs(state: AgentState):
    external_libs = state.get("external_libs", False)
    if external_libs:
        return "install_external_libs"
    else:
        return "execute_python_function"

# Initialize the agent and pass the schema
agent_init = StateGraph(AgentState)
agent_init.add_node("code_generator", code_generator)
agent_init.add_node("validate_code", validate_code)
agent_init.add_node("validate_external_libs", validate_external_libs)
agent_init.add_node("install_external_libs", install_external_libs)
agent_init.add_node("correct_python_erros", correct_python_erros)
agent_init.add_node("execute_python_function", execute_python_function)

# Add the edges
from langgraph.graph import START, END
agent_init.add_edge(START, "code_generator")
agent_init.add_edge("code_generator", "validate_code")
agent_init.add_conditional_edges("validate_code", check_code)
agent_init.add_edge("correct_python_erros", "validate_code")
agent_init.add_conditional_edges("validate_external_libs", check_external_libs)
agent_init.add_edge("install_external_libs", "execute_python_function")
agent_init.add_edge("execute_python_function", END)

# Create the agent
memory = MemorySaver()
agent = agent_init.compile(checkpointer=memory)

# Generate and display the diagram
from IPython.display import Image, display
display(Image(agent.get_graph().draw_mermaid_png()))

Error Message and Stack Trace (if applicable)

Incorrect daigram 
Please cehck the diagram in the description

Description

State Transitions Not Displayed Correctly in Mermaid Diagram (see the diagram at the end)

Description:

When generating a mermaid diagram using agent.get_graph().draw_mermaid_png() in LangGraph, the state transitions are not displayed correctly. The transitions do not reflect the defined conditional logic between nodes.

Steps to Reproduce:

Define an AgentState and nodes with transition logic as shown in the code snippet below.
Add nodes and conditional edges to the agent.
Generate the mermaid diagram with agent.get_graph().draw_mermaid_png().
Observe the incorrect relationships in the generated diagram.

Diagram

Image

System Info

State Transitions Not Displayed Correctly in Mermaid Diagram (see the diagram at the end)

Description:

When generating a mermaid diagram using agent.get_graph().draw_mermaid_png() in LangGraph, the state transitions are not displayed correctly. The transitions do not reflect the defined conditional logic between nodes.

Steps to Reproduce:

Define an AgentState and nodes with transition logic as shown in the code snippet below.
Add nodes and conditional edges to the agent.
Generate the mermaid diagram with agent.get_graph().draw_mermaid_png().
Observe the incorrect relationships in the generated diagram.

Diagram

Image

Code

import logging
from langgraph.graph import MessagesState, StateGraph
from langgraph.checkpoint.memory import MemorySaver
from langgraph.llm import HumanMessage

# Define the AgentState class
class AgentState(MessagesState):
    error: bool
    error_msg: str
    code: str
    external_libs: bool

# Define the nodes
def code_generator(state: AgentState):
    prompt = "Can you provide me with Python code for a function that takes a string as input and returns True if the string is a palindrome and False otherwise? The function should not use any libraries."
    msg = [HumanMessage(content=prompt)]
    function_str = "def is_palindrome(s): return s == s[::-1]"
    return {"messages": [function_str], "code": function_str}

def validate_code(state: AgentState):
    function = state["code"]
    try:
        exec(function)
        return {"error": False}
    except Exception as e:
        return {"error": True, "error_msg": str(e)}

def validate_external_libs(state: AgentState):
    return {"external_libs": False}

def install_external_libs(state: AgentState):
    return {"external_libs": False}

def correct_python_erros(state: AgentState):
    corrected_function_str = "def is_palindrome(s): return s == s[::-1]"
    return {"messages": [corrected_function_str], "code": corrected_function_str, "error": False}

def execute_python_function(state: AgentState):
    function = state["code"]
    exec(function)
    return "Done"

# Define the edges
def check_code(state: AgentState):
    error = state.get("error", None)
    if error:
        return "correct_python_erros"
    else:
        return "validate_external_libs"

def check_external_libs(state: AgentState):
    external_libs = state.get("external_libs", False)
    if external_libs:
        return "install_external_libs"
    else:
        return "execute_python_function"

# Initialize the agent and pass the schema
agent_init = StateGraph(AgentState)
agent_init.add_node("code_generator", code_generator)
agent_init.add_node("validate_code", validate_code)
agent_init.add_node("validate_external_libs", validate_external_libs)
agent_init.add_node("install_external_libs", install_external_libs)
agent_init.add_node("correct_python_erros", correct_python_erros)
agent_init.add_node("execute_python_function", execute_python_function)

# Add the edges
from langgraph.graph import START, END
agent_init.add_edge(START, "code_generator")
agent_init.add_edge("code_generator", "validate_code")
agent_init.add_conditional_edges("validate_code", check_code)
agent_init.add_edge("correct_python_erros", "validate_code")
agent_init.add_conditional_edges("validate_external_libs", check_external_libs)
agent_init.add_edge("install_external_libs", "execute_python_function")
agent_init.add_edge("execute_python_function", END)

# Create the agent
memory = MemorySaver()
agent = agent_init.compile(checkpointer=memory)

# Generate and display the diagram
from IPython.display import Image, display
display(Image(agent.get_graph().draw_mermaid_png()))


Originally created by @jeffersonRosman on GitHub (Feb 6, 2025). ### Checked other resources - [x] This is a bug, not a usage question. For questions, please use GitHub Discussions. - [x] I added a clear and detailed title that summarizes the issue. - [x] I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example). - [x] I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue. ### Example Code ```python import logging from langgraph.graph import MessagesState, StateGraph from langgraph.checkpoint.memory import MemorySaver from langgraph.llm import HumanMessage # Define the AgentState class class AgentState(MessagesState): error: bool error_msg: str code: str external_libs: bool # Define the nodes def code_generator(state: AgentState): prompt = "Can you provide me with Python code for a function that takes a string as input and returns True if the string is a palindrome and False otherwise? The function should not use any libraries." msg = [HumanMessage(content=prompt)] function_str = "def is_palindrome(s): return s == s[::-1]" return {"messages": [function_str], "code": function_str} def validate_code(state: AgentState): function = state["code"] try: exec(function) return {"error": False} except Exception as e: return {"error": True, "error_msg": str(e)} def validate_external_libs(state: AgentState): return {"external_libs": False} def install_external_libs(state: AgentState): return {"external_libs": False} def correct_python_erros(state: AgentState): corrected_function_str = "def is_palindrome(s): return s == s[::-1]" return {"messages": [corrected_function_str], "code": corrected_function_str, "error": False} def execute_python_function(state: AgentState): function = state["code"] exec(function) return "Done" # Define the edges def check_code(state: AgentState): error = state.get("error", None) if error: return "correct_python_erros" else: return "validate_external_libs" def check_external_libs(state: AgentState): external_libs = state.get("external_libs", False) if external_libs: return "install_external_libs" else: return "execute_python_function" # Initialize the agent and pass the schema agent_init = StateGraph(AgentState) agent_init.add_node("code_generator", code_generator) agent_init.add_node("validate_code", validate_code) agent_init.add_node("validate_external_libs", validate_external_libs) agent_init.add_node("install_external_libs", install_external_libs) agent_init.add_node("correct_python_erros", correct_python_erros) agent_init.add_node("execute_python_function", execute_python_function) # Add the edges from langgraph.graph import START, END agent_init.add_edge(START, "code_generator") agent_init.add_edge("code_generator", "validate_code") agent_init.add_conditional_edges("validate_code", check_code) agent_init.add_edge("correct_python_erros", "validate_code") agent_init.add_conditional_edges("validate_external_libs", check_external_libs) agent_init.add_edge("install_external_libs", "execute_python_function") agent_init.add_edge("execute_python_function", END) # Create the agent memory = MemorySaver() agent = agent_init.compile(checkpointer=memory) # Generate and display the diagram from IPython.display import Image, display display(Image(agent.get_graph().draw_mermaid_png())) ``` ### Error Message and Stack Trace (if applicable) ```shell Incorrect daigram Please cehck the diagram in the description ``` ### Description State Transitions Not Displayed Correctly in Mermaid Diagram (see the diagram at the end) # Description: When generating a mermaid diagram using agent.get_graph().draw_mermaid_png() in LangGraph, the state transitions are not displayed correctly. The transitions do not reflect the defined conditional logic between nodes. # Steps to Reproduce: Define an AgentState and nodes with transition logic as shown in the code snippet below. Add nodes and conditional edges to the agent. Generate the mermaid diagram with agent.get_graph().draw_mermaid_png(). Observe the incorrect relationships in the generated diagram. # Diagram <img width="849" alt="Image" src="https://github.com/user-attachments/assets/898d21c9-4ece-4eb5-9b62-570ff37c5d4b" /> ### System Info State Transitions Not Displayed Correctly in Mermaid Diagram (see the diagram at the end) # Description: When generating a mermaid diagram using agent.get_graph().draw_mermaid_png() in LangGraph, the state transitions are not displayed correctly. The transitions do not reflect the defined conditional logic between nodes. # Steps to Reproduce: Define an AgentState and nodes with transition logic as shown in the code snippet below. Add nodes and conditional edges to the agent. Generate the mermaid diagram with agent.get_graph().draw_mermaid_png(). Observe the incorrect relationships in the generated diagram. # Diagram <img width="849" alt="Image" src="https://github.com/user-attachments/assets/898d21c9-4ece-4eb5-9b62-570ff37c5d4b" /> # Code ```python import logging from langgraph.graph import MessagesState, StateGraph from langgraph.checkpoint.memory import MemorySaver from langgraph.llm import HumanMessage # Define the AgentState class class AgentState(MessagesState): error: bool error_msg: str code: str external_libs: bool # Define the nodes def code_generator(state: AgentState): prompt = "Can you provide me with Python code for a function that takes a string as input and returns True if the string is a palindrome and False otherwise? The function should not use any libraries." msg = [HumanMessage(content=prompt)] function_str = "def is_palindrome(s): return s == s[::-1]" return {"messages": [function_str], "code": function_str} def validate_code(state: AgentState): function = state["code"] try: exec(function) return {"error": False} except Exception as e: return {"error": True, "error_msg": str(e)} def validate_external_libs(state: AgentState): return {"external_libs": False} def install_external_libs(state: AgentState): return {"external_libs": False} def correct_python_erros(state: AgentState): corrected_function_str = "def is_palindrome(s): return s == s[::-1]" return {"messages": [corrected_function_str], "code": corrected_function_str, "error": False} def execute_python_function(state: AgentState): function = state["code"] exec(function) return "Done" # Define the edges def check_code(state: AgentState): error = state.get("error", None) if error: return "correct_python_erros" else: return "validate_external_libs" def check_external_libs(state: AgentState): external_libs = state.get("external_libs", False) if external_libs: return "install_external_libs" else: return "execute_python_function" # Initialize the agent and pass the schema agent_init = StateGraph(AgentState) agent_init.add_node("code_generator", code_generator) agent_init.add_node("validate_code", validate_code) agent_init.add_node("validate_external_libs", validate_external_libs) agent_init.add_node("install_external_libs", install_external_libs) agent_init.add_node("correct_python_erros", correct_python_erros) agent_init.add_node("execute_python_function", execute_python_function) # Add the edges from langgraph.graph import START, END agent_init.add_edge(START, "code_generator") agent_init.add_edge("code_generator", "validate_code") agent_init.add_conditional_edges("validate_code", check_code) agent_init.add_edge("correct_python_erros", "validate_code") agent_init.add_conditional_edges("validate_external_libs", check_external_libs) agent_init.add_edge("install_external_libs", "execute_python_function") agent_init.add_edge("execute_python_function", END) # Create the agent memory = MemorySaver() agent = agent_init.compile(checkpointer=memory) # Generate and display the diagram from IPython.display import Image, display display(Image(agent.get_graph().draw_mermaid_png()))
yindo closed this issue 2026-02-20 17:40:08 -05:00
Author
Owner

@eyurtsev commented on GitHub (Feb 7, 2025):

"Observe the incorrect relationships in the generated diagram."

Could you describe what the incorrect relationship? If you can communicate the problem more effectively, it makes it easier for a maintainer to look and confirm the problem.

For example, are you seeing an edge that shouldn't exist? Is an edge missing? if so which edge is missing? Is a node not showing etc.

@eyurtsev commented on GitHub (Feb 7, 2025): "Observe the incorrect relationships in the generated diagram." Could you describe what the incorrect relationship? If you can communicate the problem more effectively, it makes it easier for a maintainer to look and confirm the problem. For example, are you seeing an edge that shouldn't exist? Is an edge missing? if so which edge is missing? Is a node not showing etc.
Author
Owner

@jeffersonRosman commented on GitHub (Feb 7, 2025):

Conditional edges are not pointing to the correct edge
for exmaple

def check_code(state: AgentState):
    error = state.get("error", None)
    if error:
        return "correct_python_erros"
    else:
        return "validate_external_libs"


agent_init.add_conditional_edges("validate_code", check_code)

it say validate_code node go only to correct_python_erros or validate_external_libs
but in the diagram you can see that the validate_code node is going to 5 different nodes

Image

@jeffersonRosman commented on GitHub (Feb 7, 2025): Conditional edges are not pointing to the correct edge for exmaple ``` python def check_code(state: AgentState): error = state.get("error", None) if error: return "correct_python_erros" else: return "validate_external_libs" agent_init.add_conditional_edges("validate_code", check_code) ``` it say validate_code node go only to **correct_python_erros** or **validate_external_libs** but in the diagram you can see that the **_validate_code_** node is going to 5 different nodes ![Image](https://github.com/user-attachments/assets/88a6acbb-83bc-429d-a717-17973b464be6)
Author
Owner

@vbarda commented on GitHub (Feb 7, 2025):

@jeffersonRosman you need to add a type annotation to the conditional edges or specify a path_map in .add_conditional_edges, e.g.:

def check_code(state: AgentState) -> Literal["correct_python_erros", "validate_external_libs"]:

or

agent_init.add_conditional_edges(
    "validate_code", 
    check_code,
    path_map=["correct_python_erros", "validate_external_libs"]
)
@vbarda commented on GitHub (Feb 7, 2025): @jeffersonRosman you need to add a type annotation to the conditional edges or specify a path_map in `.add_conditional_edges`, e.g.: ```python def check_code(state: AgentState) -> Literal["correct_python_erros", "validate_external_libs"]: ``` or ```python agent_init.add_conditional_edges( "validate_code", check_code, path_map=["correct_python_erros", "validate_external_libs"] ) ```
Author
Owner

@jeffersonRosman commented on GitHub (Feb 7, 2025):

@vbarda
I integreated your sugestions and now it is working as expected

Image
amazing
thanks!

@jeffersonRosman commented on GitHub (Feb 7, 2025): @vbarda I integreated your sugestions and now it is working as expected ![Image](https://github.com/user-attachments/assets/abe0840f-258a-4b14-b4d3-ae467115f534) amazing thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#440