langgraph icon indicating copy to clipboard operation
langgraph copied to clipboard

Cannot draw graph w/ xray=True when graph contains a subgraph w/ edges targeting the subgraph entry point

Open mhmaguire opened this issue 8 months ago • 8 comments

Checked other resources

  • [X] I added a very descriptive title to this issue.
  • [X] I searched the LangChain documentation with the integrated search.
  • [X] I used the GitHub search to find a similar question and didn't find it.
  • [X] I am sure that this is a bug in LangChain rather than my code.

Example Code

from typing import TypedDict, Annotated, List
from langchain_core.messages import AnyMessage
from langgraph.graph import StateGraph, add_messages
from IPython.display import Image, display
import random


class State(TypedDict):
    messages: Annotated[List[AnyMessage], add_messages]


def node(name):
    def _node(state: State):
        return {'messages': [('human', f'entered {name} node')]}

    return _node


parent = StateGraph(State)
child = StateGraph(State)

child.add_node('c_one', node('c_one'))
child.add_node('c_two', node('c_two'))

child.add_edge('__start__', 'c_one')
child.add_edge('c_two', 'c_one')

child.add_conditional_edges('c_one',
                            lambda x: str(random.randrange(0, 2)),
                            {
                                '0': 'c_two',
                                '1': '__end__'
                                
                            })

parent.add_node('p_one', node('p_one'))
parent.add_node('p_two', child.compile())

parent.add_edge('__start__', 'p_one')
parent.add_edge('p_two', 'p_one')

parent.add_conditional_edges('p_one',
                            lambda x: str(random.randrange(0, 2)),
                            {
                                '0': 'p_two',
                                '1': '__end__'
                                
                            })


app = parent.compile()

for event in app.stream({'messages': [('human', 'start')]}):
    print(event)


display(Image(app.get_graph(xray=True).draw_mermaid_png())) # this fails

-->

{'p_one': {'messages': [('human', 'entered p_one node')]}}
{'p_two': {'messages': [HumanMessage(content='start', id='53318df0-564d-4f9b-b1ad-a8bcf03e2219'), HumanMessage(content='entered p_one node', id='c67dcfa5-e1cd-4bc0-a9b5-2ca6676150bd'), HumanMessage(content='entered c_one node', id='35b095bb-3113-4d93-8afa-b8225cb72042'), HumanMessage(content='entered c_two node', id='8f231424-9c4f-49d2-a61e-12917d953454'), HumanMessage(content='entered c_one node', id='b8bdc719-ff93-40ef-adf5-c4e90d541a2a')]}}
{'p_one': {'messages': [('human', 'entered p_one node')]}}
{'p_two': {'messages': [HumanMessage(content='start', id='53318df0-564d-4f9b-b1ad-a8bcf03e2219'), HumanMessage(content='entered p_one node', id='c67dcfa5-e1cd-4bc0-a9b5-2ca6676150bd'), HumanMessage(content='entered c_one node', id='35b095bb-3113-4d93-8afa-b8225cb72042'), HumanMessage(content='entered c_two node', id='8f231424-9c4f-49d2-a61e-12917d953454'), HumanMessage(content='entered c_one node', id='b8bdc719-ff93-40ef-adf5-c4e90d541a2a'), HumanMessage(content='entered p_one node', id='8cf2a906-baa2-41c6-84f8-615546a90dcf'), HumanMessage(content='entered c_one node', id='05991dcf-6d6e-4968-bb3a-5f1b39bbac0b')]}}
{'p_one': {'messages': [('human', 'entered p_one node')]}}
{'p_two': {'messages': [HumanMessage(content='start', id='53318df0-564d-4f9b-b1ad-a8bcf03e2219'), HumanMessage(content='entered p_one node', id='c67dcfa5-e1cd-4bc0-a9b5-2ca6676150bd'), HumanMessage(content='entered c_one node', id='35b095bb-3113-4d93-8afa-b8225cb72042'), HumanMessage(content='entered c_two node', id='8f231424-9c4f-49d2-a61e-12917d953454'), HumanMessage(content='entered c_one node', id='b8bdc719-ff93-40ef-adf5-c4e90d541a2a'), HumanMessage(content='entered p_one node', id='8cf2a906-baa2-41c6-84f8-615546a90dcf'), HumanMessage(content='entered c_one node', id='05991dcf-6d6e-4968-bb3a-5f1b39bbac0b'), HumanMessage(content='entered p_one node', id='a67bd7a7-dbb0-418b-913f-dc69f02592a7'), HumanMessage(content='entered c_one node', id='5785cb97-4bcd-4c76-a8d6-bb3e62e967f4')]}}
{'p_one': {'messages': [('human', 'entered p_one node')]}}

Error Message and Stack Trace (if applicable)

see below

Description

I'm trying to draw a graph which contains a subgraph as a node. The subgraph has edges which target the entry node, which seems to be the problem in get_graph(xray=True)

I expect to see an image of the graph.

Instead I get an exception.

System Info

see below

mhmaguire avatar Jun 04 '24 22:06 mhmaguire