Search topics…
Tutorials
Explore
June 6 Offline Event →
LangChain & GenAI · Module 4

Chains & LCEL

Master LangChain Chains and LCEL — LLMChain, Sequential Chains, Router Chains, Parallel Chains, RunnableLambda, RunnablePassthrough, chain debugging and real-world examples.

⏱ 50 Min Read Module 4 of 8 Updated: May 2026

Chains are the fundamental orchestration mechanism in LangChain — they connect prompts, models, parsers, and custom functions into coherent pipelines. This module covers the full spectrum: from the legacy LLMChain to modern LCEL patterns including sequential, parallel, and router chains. By the end, you'll be able to build arbitrarily complex LLM workflows using the composable Runnable interface.

Day 7

Chains Intro & LCEL Pipes

Why this matters

Chains connect prompts, models, and parsers with LCEL's pipe operator — multi-step logic without spaghetti code.

LCEL composes Runnables with |: output of one step feeds the next. Chains support .invoke(), .stream(), and .batch().

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

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("human", "{question}"),
])
chain = prompt | ChatOpenAI(model="gpt-4o-mini") | StrOutputParser()
print(chain.invoke({"question": "What is LCEL?"}))
Prefer LCEL over legacy LLMChain — better typing, streaming, and LangSmith traces.

Common mistakes

  • Hard-coding API keys in source instead of environment variables.
  • Passing raw strings where ChatPromptTemplate expects message tuples.
  • Skipping text splitting before embedding large PDFs (context overflow).

Interview checkpoints

  • Q: Explain chains & lcel in LangChain. A: One-sentence definition + one API name.
  • Q: Common bug? A: Keys, message format, or missing split/embed step.

Practice

  1. Basic: Sketch a minimal chains & lcel snippet.
  2. Intermediate: Run a notebook cell demonstrating Chains & LCEL.
  3. Advanced: Break Chains & LCEL intentionally and interpret the error.

Recap

  • You can explain chains & lcel clearly.
  • You know one mistake to avoid.
  • You see how this connects to the next lesson.

Next: Advanced Chains

Day 8

Sequential, Router & Parallel Chains

Why this matters

Router and parallel chains route inputs dynamically and run steps concurrently — patterns for real workflows.

Sequential chains pass outputs forward; router chains branch by input type; RunnableParallel runs steps concurrently.

  • RunnableLambda — wrap Python functions in the chain.
  • RunnablePassthrough.assign — add fields to dict state.
  • Debug with chain.get_graph() and LangSmith run trees.

Common mistakes

  • Hard-coding API keys in source instead of environment variables.
  • Passing raw strings where ChatPromptTemplate expects message tuples.
  • Skipping text splitting before embedding large PDFs (context overflow).

Interview checkpoints

  • Q: Explain advanced chains in LangChain. A: One-sentence definition + one API name.
  • Q: Common bug? A: Keys, message format, or missing split/embed step.

Practice

  1. Basic: Sketch a minimal advanced chains snippet.
  2. Intermediate: Run a notebook cell demonstrating Advanced Chains.
  3. Advanced: Break Advanced Chains intentionally and interpret the error.

Recap

  • You can explain advanced chains clearly.
  • You know one mistake to avoid.
  • You see how this connects to the next lesson.

Next: Document Loaders

← Prompts & Parsers Document Loaders →