Learn / Frameworks / LangChain / LCEL in Depth

LangChain · Lesson 9 of 15

LCEL in Depth

Runnables, parallel branches, retries and how the pipe operator actually works.

  • Intermediate
  • 16 min read
  • 3 objectives

Before this lessonLesson 8: Vector Stores

What you will learn

  • Pipe runnables
  • Use RunnableParallel
  • Add a retry

Your Progress

0 of 15 lessons 0%

  • Lessons0 / 15
  • Completed0
  • Est. time left~ 4 hours

Create a free account to keep your progress on every device.

LCEL (LangChain Expression Language) treats every step as a Runnable. The | operator builds a graph you can invoke, batch or stream.

The pipe

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

prompt = ChatPromptTemplate.from_messages([
    ("system", "Answer in one sentence."),
    ("human", "{question}"),
])
chain = prompt | model | StrOutputParser()
print(chain.invoke({"question": "What is RAG?"}))

Parallel branches

from langchain_core.runnables import RunnableParallel, RunnablePassthrough

combo = RunnableParallel(
    question=RunnablePassthrough(),
    context=lambda q: retriever.invoke(q),
)
rag = combo | prompt | model | StrOutputParser()

Retries and fallbacks

reliable = chain.with_retry(stop_after_attempt=3).with_fallbacks([backup_chain])

Each runnable has the same interface, so you can swap a model or a parser without rewriting callers.

Up next · Lesson 10Streaming and CallbacksStream tokens to a client and hook callbacks for logging and tokens used.