AI / LangGraph LangChain Interview questions
What are subgraphs in LangGraph?
A subgraph in LangGraph is a compiled graph that is used as a node inside a parent graph. Subgraphs allow you to encapsulate complex, reusable agent logic and compose multiple graphs hierarchically — exactly like functions in programming, where a subgraph is the 'function' and the parent graph is the 'caller'.
from langgraph.graph import StateGraph, START, END
# --- Define the subgraph ---
class SubgraphState(TypedDict):
messages: Annotated[list, operator.add]
search_results: list
sub_builder = StateGraph(SubgraphState)
sub_builder.add_node("search", search_node)
sub_builder.add_node("summarise", summarise_node)
sub_builder.add_edge(START, "search")
sub_builder.add_edge("search", "summarise")
sub_builder.add_edge("summarise", END)
research_subgraph = sub_builder.compile()
# --- Use it as a node in the parent graph ---
class ParentState(TypedDict):
messages: Annotated[list, operator.add]
parent_builder = StateGraph(ParentState)
parent_builder.add_node("research", research_subgraph) # subgraph as node
parent_builder.add_node("answer", answer_node)
parent_builder.add_edge(START, "research")
parent_builder.add_edge("research", "answer")
parent_builder.add_edge("answer", END)
graph = parent_builder.compile()
State key overlap between parent and subgraph determines how data flows between them. Keys present in both states are automatically mapped. Subgraphs can have their own checkpointers for independent persistence, or inherit the parent's checkpointer.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
