Visualize the workflow

A state graph you cannot see is a state graph you cannot debug. LangGraph ships with a built-in visualization that renders the compiled graph as a Mermaid PNG. Checking this image into your repo doubles as live documentation of the agent architecture.

retriever.py
python
def generate_workflow_png(output_path: str = "workflow.png") -> str:
    """Render the agent state graph to a PNG file."""
    workflow = StateGraph(RetrievalState)

    # Structure-only nodes are fine for visualization
    workflow.add_node("analyze",  lambda state: state)
    workflow.add_node("descend",  lambda state: state)
    workflow.add_node("retrieve", lambda state: state)
    workflow.add_node("generate", lambda state: state)

    workflow.set_entry_point("analyze")
    workflow.add_conditional_edges(
        "analyze", lambda state: "retrieve",
        {"descend": "descend", "retrieve": "retrieve", "end": END},
    )
    workflow.add_edge("descend",  "analyze")
    workflow.add_edge("retrieve", "generate")
    workflow.add_edge("generate", END)

    graph = workflow.compile()
    graph_image = graph.get_graph().draw_mermaid_png()

    with open(output_path, "wb") as f:
        f.write(graph_image)
    return output_path

We build a structure-only version of the workflow with dummy node functions. draw_mermaid_png calls the LangGraph server to render the Mermaid diagram as a PNG. Perfect for README embedding.

main.py
python
from retriever import generate_workflow_png

# Generate a visualization alongside the tree cache
workflow_png_path = TREE_CACHE_PATH.parent / "workflow.png"
generate_workflow_png(output_path=str(workflow_png_path))
print(f"Workflow diagram saved to {workflow_png_path}")

The main entry point renders the PNG on every run. Cheap, and keeps your documentation honest if the graph ever changes.

Quiz: Quiz

Loading practice…