langchain_core.runnables.graph.Graph

class langchain_core.runnables.graph.Graph(nodes: ~typing.Dict[str, ~langchain_core.runnables.graph.Node] = <factory>, edges: ~typing.List[~langchain_core.runnables.graph.Edge] = <factory>)[source]

Graph of nodes and edges.

Attributes

nodes

edges

Methods

__init__([nodes, edges])

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

Add an edge to the graph and return it.

add_node(data[, id])

Add a node to the graph and return it.

draw_ascii()

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

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

draw_png()

extend(graph)

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()

print_ascii()

remove_node(node)

Remove a node from the graphm 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, ie.

trim_last_node()

Remove the last node if it exists and has a single incoming edge, ie.

Parameters
  • nodes (Dict[str, Node]) –

  • edges (List[Edge]) –

__init__(nodes: ~typing.Dict[str, ~langchain_core.runnables.graph.Node] = <factory>, edges: ~typing.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: Optional[str] = None, conditional: bool = False) Edge[source]

Add an edge to the graph and return it.

Parameters
  • source (Node) –

  • target (Node) –

  • data (Optional[str]) –

  • conditional (bool) –

Return type

Edge

add_node(data: Union[Type[BaseModel], RunnableType], id: Optional[str] = None) Node[source]

Add a node to the graph and return it.

Parameters
  • data (Union[Type[BaseModel], RunnableType]) –

  • id (Optional[str]) –

Return type

Node

draw_ascii() str[source]
Return type

str

draw_mermaid(*, with_styles: bool = True, curve_style: CurveStyle = CurveStyle.LINEAR, node_colors: NodeColors = NodeColors(start='#ffdfba', end='#baffc9', other='#fad7de'), wrap_label_n_words: int = 9) str[source]
Parameters
  • with_styles (bool) –

  • curve_style (CurveStyle) –

  • node_colors (NodeColors) –

  • wrap_label_n_words (int) –

Return type

str

draw_mermaid_png(*, curve_style: CurveStyle = CurveStyle.LINEAR, node_colors: NodeColors = NodeColors(start='#ffdfba', end='#baffc9', other='#fad7de'), wrap_label_n_words: int = 9, output_file_path: Optional[str] = None, draw_method: MermaidDrawMethod = MermaidDrawMethod.API, background_color: str = 'white', padding: int = 10) bytes[source]
Parameters
  • curve_style (CurveStyle) –

  • node_colors (NodeColors) –

  • wrap_label_n_words (int) –

  • output_file_path (Optional[str]) –

  • draw_method (MermaidDrawMethod) –

  • background_color (str) –

  • padding (int) –

Return type

bytes

draw_png(output_file_path: str, fontname: Optional[str] = None, labels: Optional[LabelsDict] = None) None[source]
draw_png(output_file_path: None, fontname: Optional[str] = None, labels: Optional[LabelsDict] = None) bytes
extend(graph: Graph) 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) –

Return type

None

first_node() Optional[Node][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

Optional[Node]

last_node() Optional[Node][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

Optional[Node]

next_id() str[source]
Return type

str

print_ascii() None[source]
Return type

None

remove_node(node: Node) None[source]

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

Parameters

node (Node) –

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) –

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, ie. 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, ie. if removing it would not leave the graph without a “last” node.

Return type

None