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:
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 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.
Find the single node that is not a target of any edge.
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 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.
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.
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 #
- 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:
- 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:
- draw_mermaid(*, with_styles: bool = True, curve_style: CurveStyle = CurveStyle.LINEAR, node_colors: NodeStyles | None = None, wrap_label_n_words: int = 9) 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.
- 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) 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.
- 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.
- 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
- reid() Graph [source]#
Return a new graph with all nodes re-identified, using their unique, readable names where possible.
- Return type:
- 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]]]