{"record":{"id":"5652af6594cbb381","repo":"langchain-ai/langchain","slug":"install-pygraphviz-to-draw-graphs-pip-install-py","errorCode":null,"errorMessage":"Install pygraphviz to draw graphs: `pip install pygraphviz`.","messagePattern":"Install pygraphviz to draw graphs: `pip install pygraphviz`\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/runnables/graph_png.py","lineNumber":137,"sourceCode":"\n    def draw(self, graph: Graph, output_path: str | None = None) -> bytes | None:\n        \"\"\"Draw the given state graph into a PNG file.\n\n        Requires `graphviz` and `pygraphviz` to be installed.\n\n        Args:\n            graph: The graph to draw\n            output_path: The path to save the PNG. If `None`, PNG bytes are returned.\n\n        Raises:\n            ImportError: If `pygraphviz` is not installed.\n\n        Returns:\n            The PNG bytes if `output_path` is None, else None.\n        \"\"\"\n        if not _HAS_PYGRAPHVIZ:\n            msg = \"Install pygraphviz to draw graphs: `pip install pygraphviz`.\"\n            raise ImportError(msg)\n\n        # Create a directed graph\n        viz = pgv.AGraph(directed=True, nodesep=0.9, ranksep=1.0)\n\n        # Add nodes, conditional edges, and edges to the graph\n        self.add_nodes(viz, graph)\n        self.add_edges(viz, graph)\n        self.add_subgraph(viz, [node.split(\":\") for node in graph.nodes])\n\n        # Update entrypoint and END styles\n        self.update_styles(viz, graph)\n\n        # Save the graph as PNG\n        try:\n            return cast(\"bytes | None\", viz.draw(output_path, format=\"png\", prog=\"dot\"))\n        finally:\n            viz.close()\n","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/runnables/graph_png.py#L119-L155","documentation":"draw_png() uses pygraphviz to lay out and rasterize the graph. If the pygraphviz import failed at module load (_HAS_PYGRAPHVIZ False), it raises this ImportError with the install hint. pygraphviz requires both the Python package and the system graphviz headers, so installation can fail at the C level.","triggerScenarios":"Calling graph.draw_png() (or graph_png.draw_png) without pygraphviz installed, or where pygraphviz failed to build/import (missing system graphviz).","commonSituations":"Slim Docker images lacking graphviz system libs; Alpine/musl builds; macOS without `brew install graphviz`; Windows without Graphviz in PATH.","solutions":["Install system Graphviz first (apt install graphviz-dev / brew install graphviz), then pip install pygraphviz","Verify: python -c \"import pygraphviz\"","Use graph.draw_mermaid_png() (requests-based) as an alternative PNG renderer"],"exampleFix":"# before\ngraph.draw_png() # ImportError\n# after (debian/ubuntu)\napt-get install -y graphviz graphviz-dev && pip install pygraphviz\ngraph.draw_png()","handlingStrategy":"validation","validationCode":"try:\n    import pygraphviz  # noqa: F401\n    can_draw = True\nexcept ImportError:\n    can_draw = False\nif not can_draw:\n    png = graph.draw_mermaid_png()  # alternative renderer","typeGuard":null,"tryCatchPattern":"try:\n    png = graph.draw_png()\nexcept ImportError as e:\n    if \"pygraphviz\" in str(e):\n        png = graph.draw_mermaid_png()","preventionTips":["Install system graphviz dev headers before pip-installing pygraphviz","Bake `apt-get install graphviz graphviz-dev && pip install pygraphviz` into Docker build stages"],"tags":["graph","png","missing-dependency","importerror","pygraphviz","graphviz"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}