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

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

    Constructors

    Attributes

    Methods

    View source on GitHub
    [
    Edge
    ]
    =
    list
    (
    )
    )

    Parameters

    NameTypeDescription
    nodesdict[str, Node]
    Default:dict()
    edgeslist[Edge]
    Default:list()
    constructor
    __init__
    NameType
    nodesdict[str, Node]
    edgeslist[Edge]
    attribute
    nodes: dict[str, Node]
    attribute
    edges: list[Edge]
    method
    to_json
    method
    next_id
    method
    add_node
    method
    remove_node
    method
    add_edge
    method
    extend
    method
    reid
    method
    first_node
    method
    last_node
    method
    trim_first_node
    method
    trim_last_node
    method
    draw_ascii
    method
    print_ascii
    method
    draw_png
    method
    draw_mermaid
    method
    draw_mermaid_png

    Graph of nodes and edges.

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

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

    Convert the graph to a JSON-serializable format.

    Return a new unique node identifier.

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

    Add a node to the graph and return it.

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

    Add an edge to the graph and return it.

    Add all nodes and edges from another graph.

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

    Return a new graph with all nodes re-identified.

    Uses their unique, readable names where possible.

    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.

    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.

    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.

    Draw the graph as an ASCII art string.

    Print the graph as an ASCII art string.

    Draw the graph as a PNG image.

    Draw the graph as a Mermaid syntax string.

    Draw the graph as a PNG image using Mermaid.