Home / Function/ draw() — langchain Function Reference

draw() — langchain Function Reference

Architecture documentation for the draw() function in graph_png.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  80a735b5_4997_c6b1_cea7_cd7a0638ba48["draw()"]
  5a670bac_6993_ecfd_ad71_f79187c3df76["PngDrawer"]
  80a735b5_4997_c6b1_cea7_cd7a0638ba48 -->|defined in| 5a670bac_6993_ecfd_ad71_f79187c3df76
  05cb2749_4b9a_b841_c17a_7346d04b345e["add_nodes()"]
  80a735b5_4997_c6b1_cea7_cd7a0638ba48 -->|calls| 05cb2749_4b9a_b841_c17a_7346d04b345e
  18b74f26_5e8d_d54f_32a6_6c80561653ab["add_edges()"]
  80a735b5_4997_c6b1_cea7_cd7a0638ba48 -->|calls| 18b74f26_5e8d_d54f_32a6_6c80561653ab
  6d25c928_1947_3d19_99bb_13bb0058b41d["add_subgraph()"]
  80a735b5_4997_c6b1_cea7_cd7a0638ba48 -->|calls| 6d25c928_1947_3d19_99bb_13bb0058b41d
  7f677b4d_9894_50a2_95a4_6e4ecc8b4799["update_styles()"]
  80a735b5_4997_c6b1_cea7_cd7a0638ba48 -->|calls| 7f677b4d_9894_50a2_95a4_6e4ecc8b4799
  style 80a735b5_4997_c6b1_cea7_cd7a0638ba48 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/runnables/graph_png.py lines 120–154

    def draw(self, graph: Graph, output_path: str | None = None) -> bytes | None:
        """Draw the given state graph into a PNG file.

        Requires `graphviz` and `pygraphviz` to be installed.

        Args:
            graph: The graph to draw
            output_path: The path to save the PNG. If `None`, PNG bytes are returned.

        Raises:
            ImportError: If `pygraphviz` is not installed.

        Returns:
            The PNG bytes if `output_path` is None, else None.
        """
        if not _HAS_PYGRAPHVIZ:
            msg = "Install pygraphviz to draw graphs: `pip install pygraphviz`."
            raise ImportError(msg)

        # Create a directed graph
        viz = pgv.AGraph(directed=True, nodesep=0.9, ranksep=1.0)

        # Add nodes, conditional edges, and edges to the graph
        self.add_nodes(viz, graph)
        self.add_edges(viz, graph)
        self.add_subgraph(viz, [node.split(":") for node in graph.nodes])

        # Update entrypoint and END styles
        self.update_styles(viz, graph)

        # Save the graph as PNG
        try:
            return cast("bytes | None", viz.draw(output_path, format="png", prog="dot"))
        finally:
            viz.close()

Domain

Subdomains

Frequently Asked Questions

What does draw() do?
draw() is a function in the langchain codebase, defined in libs/core/langchain_core/runnables/graph_png.py.
Where is draw() defined?
draw() is defined in libs/core/langchain_core/runnables/graph_png.py at line 120.
What does draw() call?
draw() calls 4 function(s): add_edges, add_nodes, add_subgraph, update_styles.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free