Consider a LangChain graph where you create two nodes and connect them with an edge. What will be the output when you print the edges of the graph?
from langchain.graphs import Graph from langchain.graphs.nodes import Node g = Graph() node1 = Node(id="1", data="Start") node2 = Node(id="2", data="End") g.add_node(node1) g.add_node(node2) g.add_edge("1", "2") print(g.edges)
Edges are directional from source to target node.
The edge is added from node with id '1' to node with id '2', so the edges list contains the tuple ('1', '2').
Given the LangChain Node class, which code snippet correctly creates a node with id '3' and metadata containing {'type': 'question'}?
Check the parameter names and types carefully.
The Node constructor expects id as string, data as string, and metadata as a dictionary under the key 'metadata'. Option A matches this exactly.
After running the following code, what is the value of g.nodes?
from langchain.graphs import Graph from langchain.graphs.nodes import Node g = Graph() node1 = Node(id="1", data="A") node2 = Node(id="2", data="B") node3 = Node(id="3", data="C") g.add_node(node1) g.add_node(node2) g.add_node(node3) g.add_edge("1", "2") g.add_edge("2", "3") print(g.nodes)
Remember that g.nodes stores nodes keyed by their id.
The graph's nodes attribute is a dictionary mapping node ids to Node objects. So it contains keys '1', '2', '3' with their respective Node instances.
Given a LangChain graph g with nodes having ids '1' and '2', which code snippet will cause a runtime error when adding an edge?
from langchain.graphs import Graph from langchain.graphs.nodes import Node g = Graph() g.add_node(Node(id='1', data='X')) g.add_node(Node(id='2', data='Y'))
Check if both nodes exist before adding an edge.
Adding an edge to a non-existent node id '3' causes a runtime error because node '3' is not in the graph.
In LangChain's graph, if you remove a node with id '5' that has edges connected to it, what happens to those edges?
Think about graph consistency when nodes are removed.
Removing a node also removes all edges connected to it to keep the graph consistent and avoid dangling references.