Graph#

class langchain_core.runnables.graph.Graph(nodes: dict[str, ~langchain_core.runnables.graph.Node] = <factory>, edges: list[~langchain_core.runnables.graph.Edge] = <factory>)[source]#

Graph of nodes and edges.

Parameters:
  • nodes (dict[str, Node]) – Dictionary of nodes in the graph. Defaults to an empty dictionary.

  • edges (list[Edge]) – List of edges in the graph. Defaults to an empty list.

Attributes

Methods

__init__([nodes, edges])

add_edge(source, target[, data, conditional])

Add an edge to the graph and return it.

add_node(data[, id, metadata])

Add a node to the graph and return it.

draw_ascii()

Draw the graph as an ASCII art string.

draw_mermaid(*[, with_styles, curve_style, ...])

Draw the graph as a Mermaid syntax string.

draw_mermaid_png(*[, curve_style, ...])

Draw the graph as a PNG image using Mermaid.

draw_png()

Draw the graph as a PNG image.

extend(graph, *[, prefix])

Add all nodes and edges from another graph.

first_node()

Find the single node that is not a target of any edge.

last_node()

Find the single node that is not a source of any edge.

next_id()

Return a new unique node identifier that can be used to add a node to the graph.

print_ascii()

Print the graph as an ASCII art string.

reid()

Return a new graph with all nodes re-identified, using their unique, readable names where possible.

remove_node(node)

Remove a node from the graph and all edges connected to it.

to_json(*[, with_schemas])

Convert the graph to a JSON-serializable format.

trim_first_node()

Remove the first node if it exists and has a single outgoing edge, i.e., if removing it would not leave the graph without a "first" node.

trim_last_node()

Remove the last node if it exists and has a single incoming edge, i.e., if removing it would not leave the graph without a "last" node.

__init__(nodes: dict[str, ~langchain_core.runnables.graph.Node] = <factory>, edges: list[~langchain_core.runnables.graph.Edge] = <factory>) None#
Parameters:
  • nodes (dict[str, Node])

  • edges (list[Edge])

Return type:

None

add_edge(source: Node, target: Node, data: Stringifiable | None = None, conditional: bool = False) Edge[source]#

Add an edge to the graph and return it.

Parameters:
  • source (Node) – The source node of the edge.

  • target (Node) – The target node of the edge.

  • data (Stringifiable | None) – Optional data associated with the edge. Defaults to None.

  • conditional (bool) – Whether the edge is conditional. Defaults to False.

Returns:

The edge that was added to the graph.

Raises:

ValueError – If the source or target node is not in the graph.

Return type:

Edge

add_node(data: type[BaseModel] | RunnableType, id: str | None = None, *, metadata: dict[str, Any] | None = None) Node[source]#

Add a node to the graph and return it.

Parameters:
  • data (Union[type[BaseModel], RunnableType]) – The data of the node.

  • id (Optional[str]) – The id of the node. Defaults to None.

  • metadata (Optional[dict[str, Any]]) – Optional metadata for the node. Defaults to None.

Returns:

The node that was added to the graph.

Raises:

ValueError – If a node with the same id already exists.

Return type:

Node

draw_ascii() str[source]#

Draw the graph as an ASCII art string.

Return type:

str

draw_mermaid(*, with_styles: bool = True, curve_style: CurveStyle = CurveStyle.LINEAR, node_colors: NodeStyles | None = None, wrap_label_n_words: int = 9, frontmatter_config: dict[str, Any] | None = None) str[source]#

Draw the graph as a Mermaid syntax string.

Parameters:
  • with_styles (bool) – Whether to include styles in the syntax. Defaults to True.

  • curve_style (CurveStyle) – The style of the edges. Defaults to CurveStyle.LINEAR.

  • node_colors (NodeStyles | None) – The colors of the nodes. Defaults to NodeStyles().

  • wrap_label_n_words (int) – The number of words to wrap the node labels at. Defaults to 9.

  • frontmatter_config (dict[str, Any], optional) –

    Mermaid frontmatter config. Can be used to customize theme and styles. Will be converted to YAML and added to the beginning of the mermaid graph. Defaults to None.

    See more here: https://mermaid.js.org/config/configuration.html.

    Example: ```python {

    ”config”: {

    “theme”: “neutral”, “look”: “handDrawn”, “themeVariables”: { “primaryColor”: “#e2e2e2”},

    }

Returns:

The Mermaid syntax string.

Return type:

str

draw_mermaid_png(*, curve_style: CurveStyle = CurveStyle.LINEAR, node_colors: NodeStyles | None = None, wrap_label_n_words: int = 9, output_file_path: str | None = None, draw_method: MermaidDrawMethod = MermaidDrawMethod.API, background_color: str = 'white', padding: int = 10, frontmatter_config: dict[str, Any] | None = None) bytes[source]#

Draw the graph as a PNG image using Mermaid.

Parameters:
  • curve_style (CurveStyle) – The style of the edges. Defaults to CurveStyle.LINEAR.

  • node_colors (NodeStyles | None) – The colors of the nodes. Defaults to NodeStyles().

  • wrap_label_n_words (int) – The number of words to wrap the node labels at. Defaults to 9.

  • output_file_path (str | None) – The path to save the image to. If None, the image is not saved. Defaults to None.

  • draw_method (MermaidDrawMethod) – The method to use to draw the graph. Defaults to MermaidDrawMethod.API.

  • background_color (str) – The color of the background. Defaults to “white”.

  • padding (int) – The padding around the graph. Defaults to 10.

  • frontmatter_config (dict[str, Any], optional) –

    Mermaid frontmatter config. Can be used to customize theme and styles. Will be converted to YAML and added to the beginning of the mermaid graph. Defaults to None.

    See more here: https://mermaid.js.org/config/configuration.html.

    Example: ```python {

    ”config”: {

    “theme”: “neutral”, “look”: “handDrawn”, “themeVariables”: { “primaryColor”: “#e2e2e2”},

    }

Returns:

The PNG image as bytes.

Return type:

bytes

draw_png(output_file_path: str, fontname: str | None = None, labels: LabelsDict | None = None) None[source]#
draw_png(output_file_path: None, fontname: str | None = None, labels: LabelsDict | None = None) bytes

Draw the graph as a PNG image.

Parameters:
  • output_file_path – The path to save the image to. If None, the image is not saved. Defaults to None.

  • fontname – The name of the font to use. Defaults to None.

  • labels – Optional labels for nodes and edges in the graph. Defaults to None.

Returns:

The PNG image as bytes if output_file_path is None, None otherwise.

extend(graph: Graph, *, prefix: str = '') tuple[Node | None, Node | None][source]#

Add all nodes and edges from another graph. Note this doesn’t check for duplicates, nor does it connect the graphs.

Parameters:
  • graph (Graph) – The graph to add.

  • prefix (str) – The prefix to add to the node ids. Defaults to “”.

Returns:

A tuple of the first and last nodes of the subgraph.

Return type:

tuple[Node | None, Node | None]

first_node() Node | None[source]#

Find the single node that is not a target of any edge. If there is no such node, or there are multiple, return None. When drawing the graph, this node would be the origin.

Return type:

Node | None

last_node() Node | None[source]#

Find the single node that is not a source of any edge. If there is no such node, or there are multiple, return None. When drawing the graph, this node would be the destination.

Return type:

Node | None

next_id() str[source]#

Return a new unique node identifier that can be used to add a node to the graph.

Return type:

str

print_ascii() None[source]#

Print the graph as an ASCII art string.

Return type:

None

reid() Graph[source]#

Return a new graph with all nodes re-identified, using their unique, readable names where possible.

Return type:

Graph

remove_node(node: Node) None[source]#

Remove a node from the graph and all edges connected to it.

Parameters:

node (Node) – The node to remove.

Return type:

None

to_json(*, with_schemas: bool = False) dict[str, list[dict[str, Any]]][source]#

Convert the graph to a JSON-serializable format.

Parameters:

with_schemas (bool) – Whether to include the schemas of the nodes if they are Pydantic models. Defaults to False.

Returns:

A dictionary with the nodes and edges of the graph.

Return type:

dict[str, list[dict[str, Any]]]

trim_first_node() None[source]#

Remove the first node if it exists and has a single outgoing edge, i.e., if removing it would not leave the graph without a “first” node.

Return type:

None

trim_last_node() None[source]#

Remove the last node if it exists and has a single incoming edge, i.e., if removing it would not leave the graph without a “last” node.

Return type:

None