So far in this LangChain.js series, we've learned how to:
- Work with Chat Models
- Create reusable prompts using
PromptTemplate - Build conversational prompts using
ChatPromptTemplate - Parse AI responses using Output Parsers
- Generate structured objects using
withStructuredOutput() - Connect components using
pipe()
These topics may seem unrelated at first.
However, they all have one thing in common.
Every one of these components implements the Runnable Interface.
This is one of the most important concepts in LangChain because it provides a common way for different components to work together.
Once you understand the Runnable Interface, concepts like RunnableSequence, RunnableParallel, RAG, and AI Agents become much easier to understand.
In this article, you'll learn:
- What a Runnable is
- Why LangChain introduced the Runnable Interface
- How the Runnable contract works
- Runnable architecture
- How prompts, chat models, and output parsers all implement the same interface
- Why
pipe()works so seamlessly
Let's begin.
The Problem Before Runnables
Imagine every LangChain component had its own API.
For example:
PromptTemplate
↓
createPrompt()
↓
Chat Model
↓
generateText()
↓
Output Parser
↓
parseResult()
↓
Application
Each component would have different methods, different inputs, and different outputs.
As an application grows, connecting these components becomes increasingly difficult.
Now imagine replacing OpenAI with Gemini.
Or adding an Output Parser.
Or adding a Retriever.
Every integration could require different code.
That would make LangChain difficult to use and maintain.
There had to be a better solution.
Why Did LangChain Introduce the Runnable Interface?
LangChain introduced the Runnable Interface to provide one consistent way for components to receive input, perform work, and return output.
Instead of every component exposing different methods, they all follow the same contract.
Conceptually, every Runnable behaves like this:
Input
↓
Process
↓
Output
That's the core idea.
Regardless of whether the component is a prompt template, a language model, or an output parser, it always follows this pattern.
What is a Runnable?
A Runnable is any LangChain component that can:
- Accept input
- Process the input
- Produce output
That's it.
This simple contract is what makes LangChain so composable.
Think of it like a factory machine.
Raw Material
↓
Machine
↓
Finished Product
Each machine doesn't need to know how the previous machine works.
It only needs an input and produces an output.
LangChain components work exactly the same way.
Runnable Architecture
A typical Runnable looks like this:
Input
↓
Runnable
↓
Output
Now imagine connecting several of them together.
Input
↓
Runnable
↓
Runnable
↓
Runnable
↓
Final Output
Each Runnable performs one task before passing its output to the next component.
This simple architecture powers everything in LangChain.
The Runnable Contract
Every Runnable follows the same contract:
- It receives an input.
- It performs its responsibility.
- It returns an output.
This consistency allows completely different components to work together without special integration code.
Instead of worrying about how each component works internally, you only need to know:
- What input it expects.
- What output it returns.
This is a common design pattern in software engineering and makes applications easier to compose and maintain.
PromptTemplate is a Runnable
Let's revisit PromptTemplate.
const prompt = PromptTemplate.fromTemplate(
"Explain {topic}."
);
When you provide:
{
topic: "Node.js"
}
The prompt template produces:
Explain Node.js.
Conceptually:
Input
↓
PromptTemplate
↓
Formatted Prompt
It accepts input and returns output.
That means PromptTemplate satisfies the Runnable contract.
ChatPromptTemplate is a Runnable
Now consider ChatPromptTemplate.
const prompt =
ChatPromptTemplate.fromMessages([
[
"system",
"You are a programming instructor."
],
[
"human",
"Explain {topic}."
]
]);
Input:
{
topic: "React"
}
Output:
System:
You are a programming instructor.
Human:
Explain React.
Again:
Input
↓
ChatPromptTemplate
↓
Messages
Different job.
Same contract.
Chat Models are Runnables
Next comes the language model.
For example:
const response =
await model.invoke(
"Explain Node.js."
);
Conceptually:
Prompt
↓
Chat Model
↓
AI Response
The model receives text and generates an answer.
It also follows the Runnable contract.
Output Parsers are Runnables
An Output Parser accepts the AI response and transforms it.
Example:
AI Message
↓
StringOutputParser
↓
Plain String
Or:
AI Message
↓
StructuredOutputParser
↓
JavaScript Object
Once again:
- Input
- Processing
- Output
Exactly the same contract.
Why This Design Matters
Let's compare two approaches.
Without a common interface
Prompt
↓
Special Method A
↓
Model
↓
Special Method B
↓
Parser
↓
Special Method C
Every component behaves differently.
You need to learn multiple APIs.
With the Runnable Interface
Runnable
↓
Runnable
↓
Runnable
Every component behaves consistently.
Once you understand one Runnable, you've already learned the foundation for all the others.
This is one of LangChain's biggest strengths.
A Real-World Analogy
Think about a water pipeline.
Water Tank
↓
Pipe
↓
Filter
↓
Pipe
↓
Storage Tank
Each component has one responsibility:
- The tank stores water.
- The filter cleans water.
- The pipes move water.
Because every component connects using the same pipe size, the system is easy to assemble.
LangChain works in a similar way.
Prompt
↓
Model
↓
Parser
↓
Application
Each component focuses on one job and passes its output to the next.
Why pipe() Works
In the previous article, we wrote:
const chain =
prompt
.pipe(model)
.pipe(parser);
At first glance, this feels almost magical.
Why can a prompt connect directly to a model?
Why can a model connect directly to a parser?
The answer is simple.
Every component implements the same Runnable Interface.
Because they all follow the same contract, LangChain knows how to connect them automatically.
We'll explore this mechanism in more detail in Part 2.
OpenAI Example
const prompt = PromptTemplate.fromTemplate(
"Explain {topic}."
);
const parser = new StringOutputParser();
const chain = prompt
.pipe(openAIModel)
.pipe(parser);
const result = await chain.invoke({
topic: "Node.js",
});
console.log(result);
Even though this chain contains three different components, they work together because each one is a Runnable.
Gemini Example
const prompt = PromptTemplate.fromTemplate(
"Explain {topic}."
);
const parser = new StringOutputParser();
const chain = prompt
.pipe(geminiModel)
.pipe(parser);
const result = await chain.invoke({
topic: "React",
});
console.log(result);
Only the model changes.
The chain itself remains exactly the same.
Best Practices
- Think of each Runnable as performing one specific task.
- Compose small Runnables instead of writing large monolithic workflows.
- Reuse prompts and parsers across different chains.
- Focus on the input and output of each component rather than its internal implementation.
Common Beginner Mistakes
- Assuming only language models are Runnables.
- Thinking
pipe()is a special feature of prompts. - Treating every LangChain component as unrelated instead of recognizing the shared interface.
- Trying to understand advanced Runnable classes before understanding the basic Runnable contract.
What's Next?
In Part 2, we'll explore the practical side of the Runnable Interface.
You'll learn:
- How
invoke(),batch(), andstream()fit into the Runnable contract - Why every Runnable supports the same execution methods
- How Runnable composition works
- Real-world Runnable workflows
- Production best practices
- Common design patterns
These concepts will prepare you for RunnableSequence, where you'll begin composing more advanced AI workflows.
Final Thoughts
The Runnable Interface is the foundation of LangChain's architecture. It provides a simple contract that every major component follows: receive input, perform work, and produce output.
This shared interface is what allows prompts, chat models, output parsers, and future components like retrievers and agents to work together seamlessly.
Understanding this design philosophy is far more valuable than memorizing APIs because it helps you reason about how LangChain applications are built. Once you see everything as a Runnable, the rest of the framework becomes much easier to learn.
Happy Coding! 🚀