LCEL: LangChain Expression Language
What You'll Learn
You'll discover how LCEL lets you build LangChain chains using a simple pipe operator (|) that makes your code cleaner, more readable, and automatically adds powerful streaming and async capabilities.
The Core Idea
Remember those Sequential Chains from the previous lesson? LCEL is LangChain's modern, declarative way to compose them. Instead of explicitly calling chain constructors and passing outputs around, you use the pipe operator (|) to connect components—just like piping commands in a Unix shell.
Think of it like plumbing: Each component (prompt template, LLM, output parser) is a pipe segment. The | operator connects them, and data flows through automatically from left to right.
Here's the beautiful part: when you compose chains with LCEL, LangChain automatically gives you:
- Streaming support: Get tokens as they're generated, not all at once
- Async execution: Run chains concurrently without blocking
- Batch processing: Process multiple inputs efficiently
- Built-in error handling: Graceful failure modes
LCEL Syntax in Action
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.schema.output_parser import StrOutputParser
# Define components
prompt = ChatPromptTemplate.from_template("Tell me a joke about {topic}")
model = ChatOpenAI()
parser = StrOutputParser()
# Compose with LCEL - just pipe them together!
chain = prompt | model | parser
# Use it
result = chain.invoke({"topic": "Python"})
That's it! The pipe operator connects your prompt → model → parser into a runnable chain. No constructors, no manual wiring—just intuitive flow.
Key Takeaway: LCEL uses the pipe operator (|) to compose LangChain components declaratively, automatically enabling streaming, async execution, and batch processing without extra code.