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

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

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) Edge[source]¶

Add an edge to the graph and return it.

Parameters
  • source (Node) –

  • target (Node) –

  • data (Optional[str]) –

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_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() Dict[str, List[Dict[str, Any]]][source]¶

Convert the graph to a JSON-serializable format.

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