Sitemap
Data And Beyond

Selected stories around Data Science, Machine Learning, Artificial Intelligence, Programming, and Technology topics. Writing guide: https://medium.com/data-and-beyond/how-to-write-for-data-and-beyond-b83ff0f3813e

Member-only story

How to Call a Subgraph from a Node in LangGraph ?

--

In the previous article, we learned what subgraphs are and why they’re useful.

We’ll begin with the first way to connect a subgraph to a parent graph. This approach is used when the parent graph and the subgraph have different state schemas.

Take a Getting Ready for Office routine. One of the steps is Making Tea. But making tea is not a single action — it involves several steps, such as boiling water, adding tea leaves, milk, sugar, and masala.

Since Making Tea is a complete workflow on its own, it makes sense to build it as a subgraph instead of a single node. The parent graph only needs to know when the tea is ready. It doesn’t need to know how the tea was made.

Let’s build the Tea Making subgraph and connect it to the Getting Ready for Office parent graph.

The Subgraph

class TeaState(TypedDict):
water_ml: int
milk_ml: int
tea_leaves_tsp: int
sugar_tsp: int
tea_masala_tsp: int
tea_ready: str

def boil_water(state: TeaState):
return {"tea_ready": f"Boiled {state['water_ml']}ml water"}

def add_tea_leaves(state: TeaState):
return {"tea_ready": state["tea_ready"] + f" -> added…

--

--

Nachiket Mehendale
Nachiket Mehendale

Written by Nachiket Mehendale

Exploring Generative AI and small ideas. Learning, reflecting, evolving.

Data And Beyond
Data And Beyond

Published in Data And Beyond

Selected stories around Data Science, Machine Learning, Artificial Intelligence, Programming, and Technology topics. Writing guide: https://medium.com/data-and-beyond/how-to-write-for-data-and-beyond-b83ff0f3813e