DEV Community

Cover image for Langgraph Visualization with get_graph
Exson Joseph
Exson Joseph

Posted on • Edited on

2

Langgraph Visualization with get_graph

LangGraph is a library created to make it easier to create stateful, multi-agent applications that make use of Large Language Models (LLMs). By expressing complex agent operations as cyclic graphs, it makes it possible to create more dynamic and adaptable behaviors than conventional linear execution models.

Creating and managing chat state graphs can be challenging, but with tools like StateGraph from the langgraph library, you can seamlessly build and visualize complex workflows. This post walks you through a simple example to illustrate how to define custom nodes and edges in a state graph and visualize it using multiple formats.

Prerequisites

Before diving into the code, ensure you have the following Python libraries installed:

pip install langgraph grandalf
Enter fullscreen mode Exit fullscreen mode

Building the Graph

Here’s the core code that constructs a simple state graph, including custom nodes and transitions:

from IPython.display import Image
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END

class State(TypedDict):
    "State to manage all chats"

class MyNode:
    "Custom Node"

# Initialize the graph builder with a state
builder = StateGraph(State)

# Add a custom node
builder.add_node("myNode", MyNode)

# Define the edges for transitions
builder.add_edge(START, "myNode")
builder.add_edge("myNode", END)

# Compile the graph
chat_graph = builder.compile()
Enter fullscreen mode Exit fullscreen mode

Mermaid PNG

This can be rendered directly in a Jupyter Notebook using

IPython.display.Image

Image(chat_graph.get_graph().draw_mermaid_png())
Enter fullscreen mode Exit fullscreen mode

Image description

ASCII

The ASCII output provides a minimal yet clear visualization of the node transitions.

print(chat_graph.get_graph().draw_ascii())
Enter fullscreen mode Exit fullscreen mode

Image description

Mermaid Code

The draw_mermaid() function outputs the source code for a Mermaid

diagram.print(chat_graph.get_graph().draw_mermaid())
Enter fullscreen mode Exit fullscreen mode
%%{init: {'flowchart': {'curve': 'linear'}}}%%
graph TD;
    __start__([<p>__start__</p>]):::first
    myNode(myNode)
    __end__([<p>__end__</p>]):::last
    __start__ --> myNode;
    myNode --> __end__;
    classDef default fill:#f2f0ff,line-height:1.2
    classDef first fill-opacity:0
    classDef last fill:#bfb6fc

Enter fullscreen mode Exit fullscreen mode

You can copy the Mermaid code from the terminal and visit mermaid.live. Paste the code into the Code section, and you will see the graph rendered under the Diagram section.

Conclusion

StateGraph is a powerful tool for managing and visualizing state transitions in a chat workflow. By combining custom nodes, edges, and flexible visualization options, it simplifies the process of understanding and debugging complex state machines.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

AWS Security LIVE! Stream

Streaming live from AWS re:Inforce

Tune into Security LIVE! at re:Inforce for expert takes on modern security challenges.

Learn More

AWS Security LIVE! From re:Inforce 2025

Tune into AWS Security LIVE! streaming live from the AWS re:Inforce expo floor in Philadelphia from 8:00AM ET-6:00PM ET.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️