Pipe Operator & Data Flow
LangChain's pipe operator explained simply using a kitchen analogy.
The Pipe Operator (|) Explained Simply
Think of LangChain like a Kitchen Assembly Line.
The pipe operator | is the conveyor belt that moves ingredients from one station to the next.
1. The Conveyor Belt (|)
When you write code with |, you aren't cooking yet. You are just writing the Recipe.
chain = Chopping | Cooking | PlatingThis says: "First chop the ingredients, then cook them, then put them on a plate." Nothing happens until you yell "Order Up!"
2. Order Up! (.invoke)
To actually start the machine, you use .invoke().
chain.invoke("Carrots")Here is what happens step-by-step:
- Carrots go into the Chopping station.
- The output (Chopped Carrots) moves to the Cooking station.
- The output (Cooked Carrots) moves to the Plating station.
- The final dish is served to you.
Important Rule: Each station replaces what it received. The "Cooking" station takes chopped carrots and turns them into stew. The chopped carrots are gone!
3. Saving Ingredients (.assign)
Sometimes, you need the original vegetable later in the line (maybe as a garnish). But strict "replacement" means it would be lost!
To fix this, we use .assign.
Think of it like putting the chopped carrots in a Side Bowl that travels down the line with the main dish.
chain = (
RunnablePassthrough.assign(
side_bowl = lambda x: chop(x["veggie"])
)
| cook_main_dish
)Now the "Cooking" station receives both:
- The main veggie
- The
side_bowl(chopped veggie)
4. Two Cooks at Once (Parallel)
What if you want to cook a steak AND make a salad at the same time?
You use a Dictionary {}.
meal = {
"steak": grill_steak,
"salad": toss_salad
}LangChain sees this and says "Hey! I can do these at the same time!" It waits for both to finish before putting them on the tray.
5. Serving Spoon by Spoon (Streaming)
Sometimes you don't want to wait for the whole pot to boil. You want to serve soup spoon by spoon as it's ready. This is Streaming.
To keep it streaming, every station must act like a funnel—passing things through immediately.
6. The Manager's Whisper (Config)
Sometimes the manager needs to whisper instructions to the cooks, like "This customer is VIP" or "No salt!" You don't want to put these notes inside the food.
So you use a Config object. It's a secret note passed invisibly to every station.
chain.invoke(
"Steak",
config={"metadata": {"customer": "VIP"}}
)Any cook can open this note and see: "Ah, VIP customer. Extra garnish!"
Summary
|(Pipe): The Conveyor Belt..invoke(): The "Start" button..assign: The Side Bowl (keep data).{}(Dict): Two cooks working at once.- Config: The Manager's secret note.