LangChain Expression Language Explained: Chains, Runnables and Streaming
LangChain Expression Language (LCEL) builds chains from runnables with one pipe operator. Learn the five Runnable methods, streaming and batching.
By Galactic News Editorial · Published 2026-11-20T16:30:27+00:00 · Updated 2026-11-20T16:30:27+00:00
LangChain Expression Language Explained: Chains, Runnables and Streaming
LangChain Expression Language, or LCEL, is the standard way to build chains in LangChain. It uses one operator, the pipe, to connect components into a pipeline. The result supports streaming, batching, and async calls out of the box. This guide explains how it works, with working patterns you can copy.
What Is the LangChain Expression Language?
LCEL is a declarative syntax for composing chains. It was announced by LangChain in 2023 as a replacement for the older chain classes. The classic LLMChain class was deprecated in LangChain 0.1.17. The recommended approach today is to compose runnables with the pipe operator.
A runnable is any component that implements the Runnable interface. Prompts, models, output parsers, and retrievers are all runnables. When you connect two runnables with the pipe operator, the output of the left one feeds the input of the right one. The composed chain is itself a runnable, so it can be piped again.
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini")
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
chain = prompt | llm | StrOutputParser()
print(chain.invoke({"topic": "bears"}))
Three parts, one line, one call. The prompt formats the input. The model generates a response. The parser turns the message into a plain string.
The Runnable Interface: Five Methods in Every Chain
Every runnable exposes the same five invocation methods. That uniformity is the whole point of LCEL. Learn the interface once, and it works for every component.
| Method | What it does | When to use it |
|---|---|---|
| invoke | Runs one input, returns one output | Single synchronous calls |
| ainvoke | Async version of invoke | Async servers and notebooks |
| stream | Yields output chunks as they arrive | Live chat UIs |
| astream | Async streaming | FastAPI streaming endpoints |
| batch | Runs a list of inputs | Processing many items at once |
Streaming is the feature most developers reach for first. Call stream on a chain of prompt, model, and string parser, and each token flows through as it arrives. No extra code is needed to make a chain streamable. If every component supports streaming, the chain does too.
Composing Chains: Parallel Steps and Passthroughs
Real chains need more than a straight line. LCEL provides three helpers for common shapes.
- RunnableParallel. Runs several runnables on the same input and returns a dict of results. Use it to call a model and a retriever at the same time.
- RunnablePassthrough. Passes its input through unchanged. Use it to carry the original question alongside a retrieved context.
- RunnableLambda. Wraps any Python function as a runnable. Use it for formatting, filtering, or small transformations inside a chain.
A typical retrieval chain shows all three at work. The question goes to a retriever and passes through unchanged. The retrieved documents and the question feed a prompt. The prompt feeds the model. The model feeds the parser. Each piece is small and testable on its own.
Streaming, Batching and Fallbacks in Practice
Batching runs many inputs through a chain at once. LangChain can parallelise the work internally, so a batch of fifty questions finishes faster than fifty separate calls. Async variants exist for every method, so the same chain works in a FastAPI endpoint without blocking.
Fallbacks add resilience. Call with_fallbacks on any runnable to name a backup model. If the first model fails, the chain retries with the backup. Retries work the same way, through with_retry. These helpers attach to any runnable, which means they work on a whole chain as easily as on one model.
When LCEL Is the Wrong Tool
LCEL shines for fixed pipelines: prompt, retrieve, generate, parse. It is a poor fit for open-ended agent loops, where the model decides the next step. For that, use LangGraph or the create_agent constructor, which run the model in a loop with tools. LCEL chains are also hard to debug when they grow past a dozen steps. Break long chains into named sub-chains and test each one separately.
The Bottom Line
LCEL turns chains into a single composable interface. Every component is a runnable. The pipe operator joins them. Streaming, batching, and async support come free. Start with prompt, model, and parser. Add parallel steps and fallbacks as the pipeline grows. Reach for an agent framework only when the model itself must choose the next step.