Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 32 ยท Chains and LCEL

LCEL and the Pipe Operator

Learn the LangChain Expression Language: compose prompts, models and parsers with the | pipe operator, see how RunnableSequence works, and build your own pipe in Python.

The LangChain Expression Language (LCEL) is the short way to connect Runnables: write a | b | c and you get a new Runnable that runs a, feeds its output to b, and feeds that to c. You have already seen prompt | model | parser. This lesson explains what the pipe really does, what you get for free from it, and how to read and debug chains.

A chain is a Runnable

Python
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

prompt = ChatPromptTemplate.from_messages([
    ("system", "You explain things to a {audience}."),
    ("human", "What is {topic}?"),
])

chain = prompt | model | StrOutputParser()

chain.invoke({"audience": "ten-year-old", "topic": "an API"})
chain.batch([{"audience": "chef", "topic": "recursion"},
             {"audience": "pilot", "topic": "hashing"}])
for piece in chain.stream({"audience": "nurse", "topic": "caching"}):
    print(piece, end="")

chain is a RunnableSequence. Because it is a Runnable, it has invoke, batch, stream and the async versions automatically. Streaming flows through every step that can stream, and tracing records each step without you writing any logging code. That is the payoff of the shared interface from The Runnable Interface.

flowchart LR
    I["{audience, topic}"] --> P["ChatPromptTemplate"]
    P -->|"PromptValue"| M["Chat model"]
    M -->|"AIMessage"| S["StrOutputParser"]
    S -->|"str"| O["Output"]

Each arrow carries the previous step's output into the next step's input.

How | works

The pipe is ordinary Python operator overloading. Runnable defines __or__, which returns a RunnableSequence([self, other]). Two further rules make LCEL pleasant:

  1. Coercion. On the right-hand side, a plain function becomes a RunnableLambda, and a dict becomes a RunnableParallel (next lesson).
  2. Flattening. a | b | c makes one three-step sequence, not nested pairs.

So this works:

Python
chain = prompt | model | StrOutputParser() | (lambda text: text.upper())

Understanding the mechanism helps you write your own composable pieces. Here it is in about thirty lines:

Python Stand-in: build your own pipe operator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The real RunnableSequence adds batching, streaming, config propagation, async and error handling, but its core is that loop.

Inspecting a chain

Chains are inspectable, which makes debugging much easier than with hand-written glue code:

Python
chain.get_graph().print_ascii()       # draws the steps in the terminal
chain.steps                           # the list of Runnables
prompt.invoke({"audience": "chef", "topic": "recursion"}).to_messages()   # run one step alone

Run steps individually when a result looks wrong. If the prompt output is wrong, the model is not at fault.

You can also turn on global debug output or, better, LangSmith tracing (see Installing LangChain and Setting Up Keys) to see the input and output of every step.

Seeing every step's value (runnable)

Debugging pipelines often means printing between steps. This stand-in adds a tap step that prints and passes the value along unchanged, a handy trick in real chains too (wrap a print function in RunnableLambda, returning its input).

Python Stand-in: a tap step for debugging a pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Type mismatches: the common error

Every step must accept what the previous one returns. Typical mismatches:

Mistake Symptom Fix
Putting the model before the prompt (wrong order) Error about invalid input Prompt first, model second
Passing a string to a chain whose prompt needs a dict TypeError or "expected mapping" Pass {"topic": ...}
Parser after a step that returns a dict Parser error Parse only models' messages or strings
Prompt variable missing in the dict KeyError naming the variable Supply all variables

A habit that prevents most of these: write the chain left to right and say aloud what type flows across each pipe.

Why not just call functions?

You could write parse(model(prompt(x))). LCEL adds, for the same line of code:

  • Streaming through all steps;
  • Batching and async versions without rewriting;
  • Tracing of every step;
  • Retries and fallbacks on any step (.with_retry(), .with_fallbacks(), next lessons);
  • Composition with parallel, branching and graph tools;
  • Serialization of the structure for inspection.

For a three-step script, plain functions are fine. LCEL earns its keep when the same chain needs to stream to a web UI, run in batch, and be traced in production.

Older code uses classes like LLMChain, SequentialChain and ConversationalRetrievalChain. They are deprecated. Everything they did is a few lines of LCEL, and anything more stateful belongs in LangGraph.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does prompt | model | parser create?

    The pipe returns a RunnableSequence, so it supports invoke, batch, stream and async.
  2. 2. What happens when you put a plain Python function on the right of a pipe?

    LCEL wraps functions in RunnableLambda, and dicts in RunnableParallel.
  3. 3. Which is the correct order for a basic chain?

    Prompt formats the input, the model generates, and the parser converts the reply.
  4. 4. Which of these do you get for free from composing with LCEL?

    Composition gives the whole chain the Runnable methods and tracing hooks.

Summary

  • a | b | c builds a RunnableSequence: each output feeds the next input.
  • A chain is itself a Runnable and inherits invoke, batch, stream, async and tracing.
  • Functions and dicts are coerced into RunnableLambda and RunnableParallel.
  • Debug by running steps alone, tapping values or tracing with LangSmith.
  • Old LLMChain style classes are deprecated; use LCEL and LangGraph.

Next: RunnableParallel and RunnablePassthrough