langchain-ai / langgraph

Build resilient language agents as graphs.
https://langchain-ai.github.io/langgraph/
MIT License
7.01k stars 1.12k forks source link

Graph Display not working for over 2 levels nested graph and when using create_react_agent #2607

Open kmprasad4u opened 1 day ago

kmprasad4u commented 1 day ago

Checked other resources

Example Code

# CHECK testingSubSubGraphNode2 for error details

import os
from typing import Literal
from langchain_core.messages import AIMessage
from langchain_core.tools import tool
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import MessagesState
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

@tool
def get_weather(city: Literal["nyc", "sf"]):
    """Use this to get weather information."""
    if city == "nyc":
        return "It might be cloudy in nyc"
    elif city == "sf":
        return "It's always sunny in sf"
    else:
        raise AssertionError("Unknown city")

os.environ["OPENAI_API_KEY"] = "sk..."
llm = ChatOpenAI(model="gpt-4o")
executor_agent = create_react_agent(llm, [get_weather])

def testingSubSubGraphNode1(state: MessagesState):
    return {
        "messages": [AIMessage(content="Testing Sub Sub Node 1")]
    }

def testingSubSubGraphNode2(state: MessagesState):

    # When below line alone is present it doesn't throw error
    # response = llm.invoke({"messages": "Hi, how are you?"})

    # But if below line is present instead, it throws below error while drawing image
    # ValueError: Found duplicate subgraph 'testingSubSubGraphNode2' -- this likely means that you're reusing a subgraph node with the same name. Please adjust your graph to have subgraph nodes with unique names
    response = executor_agent.invoke({"messages": "Hi, how are you?"})

    return {
        "messages": [AIMessage(content="Testing Sub Sub Node 2")]
    }

workflow = StateGraph(MessagesState)
workflow.add_node("testingSubSubGraphNode1", testingSubSubGraphNode1)
workflow.add_node("testingSubSubGraphNode2", testingSubSubGraphNode2)

workflow.add_edge(START, "testingSubSubGraphNode1")
workflow.add_edge("testingSubSubGraphNode1", "testingSubSubGraphNode2")
workflow.add_edge("testingSubSubGraphNode2", END)
subSubGraph = workflow.compile()

# ********************************************************************************************************************

def testingSubGraphNode(state: MessagesState):
    return {
        "messages": [AIMessage(content="Testing Sub Node 1")]
    }

workflow = StateGraph(MessagesState)
workflow.add_node("testingSubGraphNode1", testingSubGraphNode)
workflow.add_node("testingSubGraphNode2", subSubGraph)
workflow.add_edge(START, "testingSubGraphNode1")
workflow.add_edge("testingSubGraphNode1", "testingSubGraphNode2")
workflow.add_edge("testingSubGraphNode2", END)
subGraph = workflow.compile()

# ********************************************************************************************************************

def testingParentNode(state: MessagesState):
    return {
        "messages": [AIMessage(content="Testing Parent Node")]
    }

workflow = StateGraph(MessagesState)
workflow.add_node("testingParentNode1", testingParentNode)
workflow.add_node("testingParentNode2", subGraph)
workflow.add_edge(START, "testingParentNode1")
workflow.add_edge("testingParentNode1", "testingParentNode2")
workflow.add_edge("testingParentNode2", END)
parentGraph = workflow.compile()

if __name__ == "__main__":    
    subSubGraphImage = subSubGraph.get_graph(xray=True).draw_mermaid_png() # This works
    print("created sub sub graph image") 

    subGraphImage = subGraph.get_graph(xray=True).draw_mermaid_png() # This works
    print("created sub graph image")

    #graphJson = parentGraph.get_graph(xray=True).to_json()
    #print(str(graphJson))

    # This doesn't work while using create_react_agent in sub sub graph
    parentImage = parentGraph.get_graph(xray=True).draw_mermaid_png() 
    print("created parent graph image")

Error Message and Stack Trace (if applicable)

ValueError: Found duplicate subgraph 'testingSubSubGraphNode2' -- this likely means that you're reusing a subgraph node with the same name. Please adjust your graph to have subgraph nodes with unique names

Description

I am trying to create nested graphs and using create_react_agent in the third level. But when displaying the graph, i get error message. If I just use llm inside the method, I don't get the error. I have tried to simplify the code as much as possible to illustrate the problem.

System Info

langchain==0.3.7 langchain-core==0.3.18 langchain-community==0.3.7 langgraph==0.2.48

gbaian10 commented 1 day ago
parentImage = parentGraph.get_graph(xray=2).draw_mermaid_png() 

True is actually 1, since bool is a subclass of int. You can directly input the number representing the sublayer depth you want to expand.

kmprasad4u commented 1 day ago

Yes I did try it, Less than 3 it doesn't throw error. But that doesn't give the full depth in the image if there are over 3 levels and the create_react_agent is used in the 3rd level.

Since the default is False, I assumed True is to display all levels and I would expect an option to display all levels instead of hardcoding a number like that.