0Pricing
AI Agents · Lesson

LCEL (LangChain Expression Language)

Compose runnables with the pipe operator '|', use RunnablePassthrough, and stream over the chain.

LCEL (LangChain Expression Language) is a free AI Agents lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is LCEL?

LangChain Expression Language is the modern way to compose chains. It uses the pipe operator | to chain Runnables.

It replaced the old "Chain" classes (LLMChain, SequentialChain) which are now legacy.

A Basic LCEL Chain

from langchain.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain.schema.output_parser import StrOutputParser

prompt = ChatPromptTemplate.from_template('Translate {text} to French.')
model = ChatOpenAI(model='gpt-4o-mini')
parser = StrOutputParser()

chain = prompt | model | parser
print(chain.invoke({'text': 'Hello, world!'}))

RunnablePassthrough

Pass input through unchanged. Useful for keeping data alongside an LLM call:

from langchain_core.runnables import RunnablePassthrough

chain = (
    {'context': retriever, 'question': RunnablePassthrough()}
    | prompt
    | model
    | parser
)
chain.invoke('What is RAG?')

RunnableParallel

Run multiple Runnables in parallel; their outputs become a dict:

from langchain_core.runnables import RunnableParallel

chain = RunnableParallel(
    summary=summary_prompt | model | parser,
    keywords=keywords_prompt | model | parser
) | merge_prompt | model | parser

result = chain.invoke({'text': '...'})

RunnableLambda

Wrap any Python function in a Runnable:

from langchain_core.runnables import RunnableLambda

def uppercase(text: str) -> str:
    return text.upper()

chain = prompt | model | parser | RunnableLambda(uppercase)

Streaming Through LCEL

for chunk in chain.stream({'text': 'Hello'}):
    print(chunk, end='', flush=True)

Batch and Async

Every Runnable supports batch, ainvoke, astream:

results = chain.batch([
    {'text': 'a'}, {'text': 'b'}, {'text': 'c'}
])
# All three run in parallel.

Schemas

Add type hints — LCEL validates inputs and outputs:

from pydantic import BaseModel

class Input(BaseModel):
    text: str

class Output(BaseModel):
    translation: str

chain = chain.with_types(input_type=Input, output_type=Output)

with_config and Fallbacks

Pin model, set retries, add fallback model:

fast = ChatOpenAI(model='gpt-4o-mini')
big = ChatOpenAI(model='gpt-4o')

chain = prompt | fast.with_fallbacks([big]) | parser

with_retry

class Chain:
    def invoke(self, x):
        return f'processed: {x}'
    def with_retry(self, stop_after_attempt=3, wait_exponential_jitter=True):
        print(f'Chain will retry up to {stop_after_attempt} times with jittered backoff')
        return self

chain = Chain()
chain = chain.with_retry(stop_after_attempt=3, wait_exponential_jitter=True)
print(chain.invoke('input data'))

Building a RAG Chain

Putting it together:

rag_chain = (
    {'context': retriever | format_docs, 'question': RunnablePassthrough()}
    | rag_prompt
    | model
    | StrOutputParser()
)

def format_docs(docs):
    return '\n\n'.join(d.page_content for d in docs)

rag_chain.invoke('What is in the handbook?')

Debugging LCEL

Inspect intermediate steps:

chain.get_graph().print_ascii()

# Or run step-by-step:
for step in chain.stream_log({'text': '...'}):
    print(step)

Pipe Operator

What is the role of RunnablePassthrough in LCEL?

Recap

LCEL is the foundation of modern LangChain. Learn pipe, Parallel, Lambda, Passthrough, and you can build any chain.

Frequently asked questions

Is the “LCEL (LangChain Expression Language)” lesson free?

Yes — the full text of “LCEL (LangChain Expression Language)” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.

What will I learn in “LCEL (LangChain Expression Language)”?

Compose runnables with the pipe operator '|', use RunnablePassthrough, and stream over the chain. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AI Agents?

No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “LCEL (LangChain Expression Language)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AI Agents lesson?

Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. LangChain Architecture: Models, Prompts, Chains
  2. Loaders, Splitters and Vector Stores
  3. LCEL (LangChain Expression Language)
  4. Building a RAG Chain End-to-End
← Back to AI Agents