langchain_core.runnables.graph.Graph¶

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

Attributes

nodes

edges

Methods

__init__([nodes, edges])

add_edge(source, target)

Add an edge to the graph and return it.

add_node(data)

Add a node to the graph and return it.

draw_ascii()

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.

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.

__init__(nodes: ~typing.Dict[str, ~langchain_core.runnables.graph.Node] = <factory>, edges: ~typing.List[~langchain_core.runnables.graph.Edge] = <factory>) None¶
add_edge(source: Node, target: Node) Edge[source]¶

Add an edge to the graph and return it.

add_node(data: Union[Type[BaseModel], RunnableType]) Node[source]¶

Add a node to the graph and return it.

draw_ascii() str[source]¶
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.

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.

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.

next_id() str[source]¶
print_ascii() None[source]¶
remove_node(node: Node) None[source]¶

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

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.

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.