LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-corerunnablesgraphGraph
    Classā—Since v0.1

    Graph

    Graph of nodes and edges.

    Copy
    Graph(
      self,
      nodes: dict[str, Node] = dict(),
      edges: list[Edge] = list()
    )

    Parameters

    NameTypeDescription
    nodesdict[str, Node]
    Default:dict()

    Dictionary of nodes in the graph. Defaults to an empty dictionary.

    edgeslist[Edge]
    Default:list()

    List of edges in the graph. Defaults to an empty list.

    Constructors

    constructor
    __init__
    NameType
    nodesdict[str, Node]
    edgeslist[Edge]

    Attributes

    attribute
    nodes: dict[str, Node]
    attribute
    edges: list[Edge]

    Methods

    method
    to_json

    Convert the graph to a JSON-serializable format.

    method
    next_id

    Return a new unique node identifier.

    It that can be used to add a node to the graph.

    method
    add_node

    Add a node to the graph and return it.

    method
    remove_node

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

    method
    add_edge

    Add an edge to the graph and return it.

    method
    extend

    Add all nodes and edges from another graph.

    Note this doesn't check for duplicates, nor does it connect the graphs.

    method
    reid

    Return a new graph with all nodes re-identified.

    Uses their unique, readable names where possible.

    method
    first_node

    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.

    method
    last_node

    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.

    method
    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.

    method
    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.

    method
    draw_ascii

    Draw the graph as an ASCII art string.

    method
    print_ascii

    Print the graph as an ASCII art string.

    method
    draw_png

    Draw the graph as a PNG image.

    method
    draw_mermaid

    Draw the graph as a Mermaid syntax string.

    method
    draw_mermaid_png

    Draw the graph as a PNG image using Mermaid.

    View source on GitHub