Home / Class/ Graph Class — langchain Architecture

Graph Class — langchain Architecture

Architecture documentation for the Graph class in graph.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  1243f14d_45dc_19c7_1b52_e30be74e4e4e["Graph"]
  f57b5d4e_9a50_bd2a_1e42_0be6340fc473["graph.py"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|defined in| f57b5d4e_9a50_bd2a_1e42_0be6340fc473
  be4a3475_1b59_d5f6_f825_17725f7a6c68["to_json()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| be4a3475_1b59_d5f6_f825_17725f7a6c68
  d3d84c43_4526_95c2_d34f_a4c40c6b99df["__bool__()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| d3d84c43_4526_95c2_d34f_a4c40c6b99df
  e70ea6f2_81c3_cec5_42d4_5035bf97fec9["next_id()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| e70ea6f2_81c3_cec5_42d4_5035bf97fec9
  90bea279_7163_e2fa_1870_2dd496f39339["add_node()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| 90bea279_7163_e2fa_1870_2dd496f39339
  0f5b06ab_9913_7e39_8907_6cd1f39b605e["remove_node()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| 0f5b06ab_9913_7e39_8907_6cd1f39b605e
  e281d4d9_2f38_aaf3_d68e_e85acf0f35e3["add_edge()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| e281d4d9_2f38_aaf3_d68e_e85acf0f35e3
  f2f2eea8_9945_e633_e87d_57e3c0d7e617["extend()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| f2f2eea8_9945_e633_e87d_57e3c0d7e617
  b81a4229_5f13_37c6_d82a_b9cffbe3c71f["reid()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| b81a4229_5f13_37c6_d82a_b9cffbe3c71f
  1f7542ef_8e75_d8fa_c496_9eecbce6fc6f["first_node()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| 1f7542ef_8e75_d8fa_c496_9eecbce6fc6f
  d720f267_2353_585c_79d4_8e0fe164abd0["last_node()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| d720f267_2353_585c_79d4_8e0fe164abd0
  29d3937c_1f3d_779b_a996_d98f93b3f745["trim_first_node()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| 29d3937c_1f3d_779b_a996_d98f93b3f745
  5cbc5ee0_fea8_ff67_4cc4_e18b4535df4f["trim_last_node()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| 5cbc5ee0_fea8_ff67_4cc4_e18b4535df4f
  b544814d_76e9_e58b_c7c1_4d12e08381f9["draw_ascii()"]
  1243f14d_45dc_19c7_1b52_e30be74e4e4e -->|method| b544814d_76e9_e58b_c7c1_4d12e08381f9

Relationship Graph

Source Code

libs/core/langchain_core/runnables/graph.py lines 253–703

class Graph:
    """Graph of nodes and edges.

    Args:
        nodes: Dictionary of nodes in the graph. Defaults to an empty dictionary.
        edges: List of edges in the graph. Defaults to an empty list.
    """

    nodes: dict[str, Node] = field(default_factory=dict)
    edges: list[Edge] = field(default_factory=list)

    def to_json(self, *, with_schemas: bool = False) -> dict[str, list[dict[str, Any]]]:
        """Convert the graph to a JSON-serializable format.

        Args:
            with_schemas: Whether to include the schemas of the nodes if they are
                Pydantic models.

        Returns:
            A dictionary with the nodes and edges of the graph.
        """
        stable_node_ids = {
            node.id: i if is_uuid(node.id) else node.id
            for i, node in enumerate(self.nodes.values())
        }
        edges: list[dict[str, Any]] = []
        for edge in self.edges:
            edge_dict = {
                "source": stable_node_ids[edge.source],
                "target": stable_node_ids[edge.target],
            }
            if edge.data is not None:
                edge_dict["data"] = edge.data  # type: ignore[assignment]
            if edge.conditional:
                edge_dict["conditional"] = True
            edges.append(edge_dict)

        return {
            "nodes": [
                {
                    "id": stable_node_ids[node.id],
                    **node_data_json(node, with_schemas=with_schemas),
                }
                for node in self.nodes.values()
            ],
            "edges": edges,
        }

    def __bool__(self) -> bool:
        """Return whether the graph has any nodes."""
        return bool(self.nodes)

    def next_id(self) -> str:
        """Return a new unique node identifier.

        It that can be used to add a node to the graph.
        """
        return uuid4().hex

    def add_node(
        self,
        data: type[BaseModel] | RunnableType | None,
        id: str | None = None,
        *,
        metadata: dict[str, Any] | None = None,
    ) -> Node:
        """Add a node to the graph and return it.

        Args:
            data: The data of the node.
            id: The id of the node.
            metadata: Optional metadata for the node.

        Returns:
            The node that was added to the graph.

        Raises:
            ValueError: If a node with the same id already exists.
        """
        if id is not None and id in self.nodes:
            msg = f"Node with id {id} already exists"

Frequently Asked Questions

What is the Graph class?
Graph is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/graph.py.
Where is Graph defined?
Graph is defined in libs/core/langchain_core/runnables/graph.py at line 253.

Analyze Your Own Codebase

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

Try Supermodel Free