Chains with the Pipe (LCEL)
So far you did three steps by hand. Fill the prompt. Call the model. Parse the answer.
A chain glues those steps together into one thing you can call.
LCEL is the LangChain Expression Language. It is just a fancy name for using the | symbol to glue steps.
The conveyor belt
Picture a conveyor belt in a factory. Each station does one job and passes the item along.
The prompt station fills in blanks. The model station writes an answer. The parser station cleans it up.
The | symbol is the belt between stations.
Your first chain
The dict goes into the prompt. The messages go into the model. The AIMessage goes into the parser. You get a string.
One line to build. One line to call.
Before and after
Here is the same work without a chain.
The chain does exactly this. The output of each step becomes the input of the next.
Nothing magic. Just less typing and fewer places to make mistakes.
Chains have the same three calls
A chain is a Runnable. So it has invoke, batch and stream, just like a model.
batch runs all three in parallel. Same chain, many inputs, many answers.
Streaming works too. That is the whole next lesson.
What the pipe really builds
When you write a | b | c, Python builds a RunnableSequence.
You almost never write RunnableSequence by hand. But it helps to know that the pipe is not magic. It is a class.
Chains inside chains
A chain is a Runnable. So you can put a chain inside another chain.
The first chain returns a string. The little lambda wraps it in a dict with the key the second prompt needs.
LangChain turns a plain function into a Runnable step for you. More on that in lesson 10.
Why chains are worth it
One object to call, test and reuse.
invoke, batch, stream and async all come for free.
Every step shows up in tracing tools like LangSmith.
Swap a model or a prompt without touching the rest.
Remember: the pipe passes the output of one step as the input of the next. prompt | model | parser is the shape you will type most.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.