Module 3 of 12
Getting Started & Core Setup
Current Study Module
LangChain - LangChain Expression Language (LCEL)
Prompts & Structured Outputs
Module 02 • Core Architecture

LangChain Expression Language (LCEL)

LangChain Expression Language (LCEL) is a declarative, composable runtime that unifies synchronous execution, asynchronous concurrency, real-time token streaming, and automatic tracing. Learn how to build high-throughput, production-resilient pipelines using the pipe operator (|).

1. The Unified Runnable Protocol

In LCEL, any object that implements the Runnable interface can be piped together seamlessly. Every Runnable automatically exposes standard synchronous and asynchronous methods:

MethodAsync CounterpartDescription
invoke(input)ainvoke(input)Executes the chain on a single input dictionary and returns the final output.
batch([inputs])abatch([inputs])Processes an array of inputs concurrently with automatic thread pooling.
stream(input)astream(input)Yields output chunks (tokens or intermediate state) as soon as they are computed.
-astream_events(input)Streams detailed intermediate events (token chunks, retriever outputs, tool starts).

2. Real-Time Token Streaming

Users perceive applications to be 10x faster when responses stream token-by-token rather than waiting for a complete 10-second response. LCEL provides native token streaming without manual callback handlers:

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

prompt = ChatPromptTemplate.from_template("Write an executive summary of {topic}.")
model = ChatOpenAI(model="gpt-4o-mini", streaming=True)
chain = prompt | model | StrOutputParser()

# Stream tokens as they generate from the neural network
print("Streaming response:")
for chunk in chain.stream({"topic": "Quantum Key Distribution in Cryptography"}):
    print(chunk, end="", flush=True)

3. Parallel Execution with RunnableParallel

Often, your application needs to execute multiple independent tasks simultaneously—such as generating a humorous response and an analytical response, or retrieving documents while preprocessing queries:

from langchain_core.runnables import RunnableParallel, RunnablePassthrough

# Define sub-chains
formal_chain = (
    ChatPromptTemplate.from_template("Provide a strictly formal definition of: {term}")
    | model
    | StrOutputParser()
)

analogy_chain = (
    ChatPromptTemplate.from_template("Explain {term} using an analogy for a 10-year-old.")
    | model
    | StrOutputParser()
)

# Combine into a parallel multi-branch runnable
parallel_chain = RunnableParallel(
    formal=formal_chain,
    analogy=analogy_chain,
    original_term=RunnablePassthrough()
)

result = parallel_chain.invoke({"term": "Backpropagation in Neural Networks"})
print("Formal Definition:", result["formal"])
print("Analogy:", result["analogy"])

4. Fault-Tolerant Fallbacks & Redundancy

Production systems must handle rate limits (HTTP 429), server downtime (HTTP 500), and billing threshold breaches. With LCEL, you can configure automatic fallback hierarchies across models or providers:

# Primary high-tier model
primary_model = ChatOpenAI(model="gpt-4o", max_retries=1)

# Fallback budget model
fallback_model = ChatOpenAI(model="gpt-4o-mini")

# Chain with graceful automatic fallback
resilient_model = primary_model.with_fallbacks([fallback_model])
resilient_chain = prompt | resilient_model | StrOutputParser()

# If gpt-4o fails or hits rate-limit, gpt-4o-mini executes transparently
output = resilient_chain.invoke({"topic": "Cybersecurity Zero Trust Architecture"})

Explore Related Tools

Hand-picked utilities and calculators related to this tool.

Related Tools