langchain-ai/langchain · error · ImportError

Install grandalf to draw graphs: `pip install grandalf`.

Error message

Install grandalf to draw graphs: `pip install grandalf`.

What it means

draw_ascii() builds a sugiyama layout using the optional grandalf package. If the import of grandalf failed at module load time (_HAS_GRANDALF is False), _build_sugiyama_layout raises this ImportError instead of an opaque NameError. It tells you exactly which extra to install.

Source

Thrown at libs/core/langchain_core/runnables/graph_ascii.py:206

        self.point(x0 + width, y0, "+")
        self.point(x0, y0 + height, "+")
        self.point(x0 + width, y0 + height, "+")


class _EdgeViewer:
    def __init__(self) -> None:
        self.pts: list[tuple[float]] = []

    def setpath(self, pts: list[tuple[float]]) -> None:
        self.pts = pts


def _build_sugiyama_layout(
    vertices: Mapping[str, str], edges: Sequence[LangEdge]
) -> Any:
    if not _HAS_GRANDALF:
        msg = "Install grandalf to draw graphs: `pip install grandalf`."
        raise ImportError(msg)

    #
    # Just a reminder about naming conventions:
    # +------------X
    # |
    # |
    # |
    # |
    # Y
    #

    vertices_ = {id_: Vertex(f" {data} ") for id_, data in vertices.items()}
    edges_ = [Edge(vertices_[s], vertices_[e], data=cond) for s, e, _, cond in edges]
    vertices_list = vertices_.values()
    graph = Graph(vertices_list, edges_)

    for vertex in vertices_list:
        vertex.view = VertexViewer(vertex.data)

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Install grandalf: pip install grandalf (or uv add grandalf / uv sync --group dev if working in the monorepo)
  2. Verify with python -c "import grandalf"
  3. Alternatively use graph.draw_mermaid() which needs no extra dependency

Example fix

# before
graph.draw_ascii() # ImportError
# after
pip install grandalf
graph.draw_ascii()
Defensive patterns

Strategy: validation

Validate before calling

try:
    import grandalf  # noqa: F401
    HAS_GRANDALF = True
except ImportError:
    HAS_GRANDALF = False

if not HAS_GRANDALF:
    raise RuntimeError("grandalf required for draw_ascii(); pip install grandalf")

Try / catch

try:
    art = graph.draw_ascii()
except ImportError as e:
    if "grandalf" in str(e):
        art = graph.draw_mermaid()

Prevention

When it happens

Trigger: Calling graph.draw_ascii() (or graph_ascii.draw_ascii) in an environment where grandalf is not installed or failed to import.

Common situations: Fresh venv/container with only langchain-core installed; grandalf is an optional dependency and is not pulled in by default; CI environments that strip extras.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/909d8e324a7fd3aa. Report an issue: GitHub.