LangChain and LangGraph · Chapter 3 of 25

Chains

By the end of this chapter you can join a prompt, a model and an output parser into one chain with the | pipe, say what every piece has in common (they are all Runnables), add your own Python function as a step with RunnableLambda, run two chains on the same input with a dictionary, and explain what | really builds. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: LangChain basics
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.

Why you need this

Every reply so far took three steps by hand: fill the template, invoke the model, take .content from the message. RoomRaja's assistant will do this in many places (replies, classification, summaries), each with its own template, and sometimes with more steps: clean the WhatsApp text first, shorten the reply after.

Sid wants each of these to be one object you can invoke, pass around and reuse. In LangChain that object is a chain, and the way to build one is the pipe operator |, called LCEL (the LangChain Expression Language).

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    reply_chain.py: prompt, model and parser in one chain

    Build one chain with the pipe operator: a ChatPromptTemplate (the system message You are RoomRaja's tenant assistant. Answer in one or two sentences. and the human message {message}), then a ChatOpenAI for lab-chat, then a StrOutputParser.

    Invoke the chain once with the message: Can I get a rent receipt for my HRA claim?

    and print what the chain returns: the reply's text.

  2. Task 2

    clean_chain.py: your own step in a chain

    Mrunal copies messages out of WhatsApp, and each line starts with the time and the sender: [23/10/26, 9:14 pm] Rohan Mehta: geyser band hai, garam paani nahi aa raha

    Write a function clean(line) that keeps only the tenant's words (everything after the first colon followed by a space, stripped) and returns them in a dictionary under the key message. Put it at the front of a chain with RunnableLambda, followed by a prompt (the system message You are RoomRaja's tenant assistant. and the human message {message}), lab-chat and a StrOutputParser.

    Invoke the chain on each of these two lines, in order, and print each reply on its own line:

    [23/10/26, 9:14 pm] Rohan Mehta: geyser band hai, garam paani nahi aa raha

    [23/10/26, 9:20 pm] Neha Verma: Is the deposit two months or one month?

  3. Task 3

    two_chains.py: one chain feeding another

    Build two chains. The classify chain: the system message Classify the tenant's message as one of: rent, deposit, notice, maintenance, guest, food, room_change, complaint, praise. Reply with the label only. and the human message {message}, lab-chat and a StrOutputParser. The reply prompt: the system message You are RoomRaja's tenant assistant. The tenant's message is about {intent}. and the human message {message}.

    Join them into one chain, so that one invoke with the message runs the classify chain first and fills {intent} with its label, then asks lab-chat with the reply prompt and parses the text. Invoke it once with: Can I pay October rent in two parts?

    and print only the final reply.

  4. Task 4 · interview question

    Interview: pipe.py, what the pipe builds

    Build the chain of task 1 again (the same prompt, lab-chat and a StrOutputParser), then print three lines: the class name of the chain, then the class names of its steps in order, joined by a space, a pipe and a space, then the chain's reply to: Is there a washing machine?

    Read the names from the chain object; do not type them in.

    The interviewer will ask what the pipe operator really builds, and what happens, step by step, when you call invoke on it.

What you will learn

  • The pipe |: prompt, model and parser joined into one chain.
  • Runnables: the one interface every piece shares (invoke, batch, stream).
  • StrOutputParser: the model's message in, plain text out.
  • RunnableLambda: your own function as a step.
  • A dictionary in a chain: several steps on the same input.

More in LangChain basics

Build "Chains" yourself

RoomRaja, a Pune co-living company, answers tenants on WhatsApp by hand. You build its assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.