In the previous articles, we learned about:
- Chat Models
- PromptTemplate
- ChatPromptTemplate
- Output Parsers
- Structured Output
Each topic introduced a new piece of the LangChain ecosystem.
However, we've mostly been using them individually.
Real-world AI applications are different.
Instead of isolated components, they connect multiple building blocks together to form a complete workflow.
This is where the pipe() method becomes incredibly useful.
Rather than manually calling each component one after another, pipe() lets you connect them into a clean, reusable chain.
In this article, you'll learn:
- What
pipe()is - Why it's useful
- How AI chains work
- Connecting PromptTemplate with Chat Models
- Connecting ChatPromptTemplate with Chat Models
- Adding Output Parsers
- Chaining multiple components
- OpenAI examples
- Gemini examples
- Best practices
Let's get started.
What is pipe()?
The pipe() method connects the output of one component to the input of another.
Think of it like a pipeline.
Input
│
▼
Prompt
│
▼
Chat Model
│
▼
Output Parser
│
▼
Final Result
Instead of manually passing values between components, LangChain does it for you.
Why Do We Need pipe()?
Without pipe():
const prompt = PromptTemplate.fromTemplate(`Explain {topic} in one line`)
const formattedPrompt =
await prompt.format({
topic:"Node.js"
});
const response =
await model.invoke(
formattedPrompt
);
const output =
await parser.invoke(response);
Every step is manual.
With pipe():
const chain =
prompt
.pipe(model)
.pipe(parser);
const response =
await chain.invoke({
topic:"Node.js"
});
Much cleaner.
Understanding AI Chains
Imagine you're working in a factory.
Raw Material
↓
Machine 1
↓
Machine 2
↓
Machine 3
↓
Finished Product
Each machine performs one task.
LangChain works exactly the same way.
Prompt
↓
Chat Model
↓
Parser
↓
Application
Every component has one responsibility.
Chain 1 – PromptTemplate → Chat Model
Let's build our first chain.
import { PromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";
const prompt =
PromptTemplate.fromTemplate(
"Explain {topic}."
);
const model =
new ChatOpenAI({
model:"gpt-4.1-mini"
});
const chain =
prompt.pipe(model);
const response =
await chain.invoke({
topic:"Node.js"
});
console.log(response.content);
Notice that pipe() automatically formats the prompt and sends it to the model.
Gemini Version
const model =
new ChatGoogleGenerativeAI({
model:"gemini-2.5-flash"
});
const chain =
prompt.pipe(model);
const response =
await chain.invoke({
topic:"React"
});
console.log(response.content);
Only the model changes.
Chain 2 – Prompt → Model → StringOutputParser
Without a parser, the model returns an AIMessage.
Sometimes we only need the generated text.
import { StringOutputParser } from "@langchain/core/output_parsers";
const parser =
new StringOutputParser();
const chain =
prompt
.pipe(model)
.pipe(parser);
const response =
await chain.invoke({
topic:"Node.js"
});
console.log(response);
Now the output is a plain string.
Chain 3 – ChatPromptTemplate → Model
const prompt =
ChatPromptTemplate.fromMessages([
[
"system",
"You are a senior backend engineer."
],
[
"human",
"Explain {topic}."
]
]);
const chain =
prompt.pipe(model);
const response =
await chain.invoke({
topic:"Streams"
});
console.log(response.content);
The prompt is automatically converted into chat messages before reaching the model.
Chain 4 – ChatPromptTemplate → Model → Parser
const parser =
new StringOutputParser();
const chain =
prompt
.pipe(model)
.pipe(parser);
const response =
await chain.invoke({
topic:"Buffers"
});
console.log(response);
Now the output is a plain string again.
Chain 5 – Structured Output
We've already learned withStructuredOutput().
It can also be part of a chain.
const structuredModel =
model.withStructuredOutput(schema);
const chain =
prompt.pipe(
structuredModel
);
const response =
await chain.invoke({
topic:"Employee"
});
console.log(response);
The output is now a validated JavaScript object.
Mixing Components
One of LangChain's biggest strengths is composability.
Prompt
↓
OpenAI
↓
Parser
or
Chat Prompt
↓
Gemini
↓
Structured Output
or
Prompt
↓
OpenAI
↓
Structured Output
↓
Application
You can mix components as needed.
Real-World Project 1 – Blog Generator
Topic
↓
PromptTemplate
↓
OpenAI
↓
StringOutputParser
↓
Blog Content
Real-World Project 2 – Resume Analyzer
Resume
↓
Prompt
↓
OpenAI
↓
Structured Output
↓
Resume Object
Real-World Project 3 – FAQ Generator
Topic
↓
Prompt
↓
Gemini
↓
String Parser
↓
FAQs
Why pipe() is So Powerful
Imagine building a complete AI application.
Without pipe(), you would manually connect every step.
With pipe(), each component focuses on a single responsibility and LangChain handles the flow between them.
This makes your code:
- Easier to read
- Easier to test
- Easier to maintain
- Easier to extend
As your applications grow, this composability becomes one of LangChain's biggest advantages.
Best Practices
- Keep each component focused on one task.
- Reuse prompt templates across multiple chains.
- Add output parsers only when needed.
- Use structured output for machine-readable responses.
- Prefer small, composable chains over large monolithic functions.
Common Beginner Mistakes
- Calling
format()manually before usingpipe(). - Parsing outputs manually instead of using output parsers.
- Creating duplicate chains for similar workflows.
- Mixing prompt construction with business logic.
- Forgetting that each component should have a single responsibility..
Final Thoughts
The pipe() method is one of the most elegant features in LangChain.js. It lets you connect prompts, chat models, and output processors into reusable AI workflows with minimal code.
By understanding how to build AI chains, you've already started thinking in the same way LangChain itself is designed: small, composable components that work together seamlessly.
This mindset will make the upcoming Runnable concepts much easier to understand and apply in real-world projects.
Happy Coding! 🚀