Introduction
In the previous article, we learned about RunnableSequence.
A RunnableSequence executes multiple steps one after another:
Input
↓
Prompt
↓
Model
↓
Parser
↓
Output
For example:
const chain = prompt
.pipe(model)
.pipe(parser);
This works perfectly when one step depends on the output of the previous step.
But what if we have multiple independent tasks?
For example, suppose a user asks:
"Explain React."
We might want to generate:
-
A beginner explanation
-
An interview explanation
-
A short summary
These tasks don't depend on each other.
There is no reason to execute them one after another.
Instead, we can run them concurrently.
This is where RunnableParallel becomes useful.
In this article, you'll learn:
-
What
RunnableParallelis -
Why parallel execution is useful
-
RunnableSequencevsRunnableParallel -
How to create a parallel workflow
-
Running multiple prompts concurrently
-
Running OpenAI and Gemini in parallel
-
Combining results
-
Real-world use cases
-
Error handling
-
Best practices
-
Common mistakes
Let's get started.
What is RunnableParallel?
RunnableParallel allows multiple Runnables to execute concurrently using the same input.
Think of it like this:
┌──→ Runnable A ──→ Output A
│
Input ───────────┼──→ Runnable B ──→ Output B
│
└──→ Runnable C ──→ Output C
All three workflows can work independently.
The final result contains the output from each Runnable.
Why Do We Need RunnableParallel?
Suppose you want to generate three different pieces of content about React:
React Explanation
React Interview Questions
React Summary
Using a sequence would look like:
Input
↓
Explanation
↓
Interview Questions
↓
Summary
This isn't ideal because the tasks are independent.
With RunnableParallel:
┌──→ Explanation
│
Input ──────────────┼──→ Interview Questions
│
└──→ Summary
All three can run concurrently.
This can make independent workflows more efficient.
RunnableSequence vs RunnableParallel
This is one of the most important differences to understand.
RunnableSequence
Use it when steps depend on each other.
Input
↓
Step 1
↓
Step 2
↓
Step 3
↓
Output
Example:
const chain = prompt
.pipe(model)
.pipe(parser);
The output of one step becomes the input of the next.
RunnableParallel
Use it when tasks are independent.
┌──→ Step A
│
Input ───────┼──→ Step B
│
└──→ Step C
The same input is sent to multiple Runnables.
Project Setup
Let's create a small project.
mkdir runnable-parallel
cd runnable-parallel
npm init -y
Install the required packages:
npm install langchain @langchain/openai @langchain/google-genai dotenv
Project Structure
runnable-parallel/
│
├── index.js
├── .env
└── package.json
Configure API Keys
Create a .env file:
OPENAI_API_KEY=your_openai_api_key
GOOGLE_API_KEY=your_google_api_key
Never commit .env to Git.
Add it to .gitignore:
node_modules/
.env
Import Required Modules
import "dotenv/config";
import { ChatOpenAI } from "@langchain/openai";
import {
ChatGoogleGenerativeAI,
} from "@langchain/google-genai";
import {
PromptTemplate,
} from "@langchain/core/prompts";
import {
StringOutputParser,
} from "@langchain/core/output_parsers";
import {
RunnableParallel,
} from "@langchain/core/runnables";
Initialize OpenAI
const openAIModel = new ChatOpenAI({
model: "gpt-4.1-mini",
});
Initialize Gemini
const geminiModel =
new ChatGoogleGenerativeAI({
model: "gemini-2.5-flash",
});
Your First RunnableParallel
Let's start with a simple example.
We'll create two prompts:
const explanationPrompt =
PromptTemplate.fromTemplate(
"Explain {topic} in simple words."
);
const summaryPrompt =
PromptTemplate.fromTemplate(
"Give a short summary of {topic}."
);
Now create two chains:
const explanationChain =
explanationPrompt.pipe(openAIModel);
const summaryChain =
summaryPrompt.pipe(openAIModel);
Now combine them using RunnableParallel:
const parallelChain =
RunnableParallel.from({
explanation: explanationChain,
summary: summaryChain,
});
Invoke the parallel workflow:
const result =
await parallelChain.invoke({
topic: "Node.js",
});
console.log(result);
The result will have this general shape:
{
explanation: "...",
summary: "..."
}
Understanding the Execution Flow
The workflow looks like this:
┌──→ Explanation Chain
│
{ topic: "Node.js" }
│
└──→ Summary Chain
Both chains receive:
{
topic: "Node.js"
}
The result is combined into one object:
{
explanation: "...",
summary: "..."
}
Adding a Third Runnable
We can easily add another independent task.
const interviewPrompt =
PromptTemplate.fromTemplate(
"Generate 3 interview questions about {topic}."
);
const interviewChain =
interviewPrompt.pipe(openAIModel);
Now:
const parallelChain =
RunnableParallel.from({
explanation: explanationChain,
summary: summaryChain,
interviewQuestions: interviewChain,
});
Invoke:
const result =
await parallelChain.invoke({
topic: "React",
});
console.log(result);
The output will look like:
{
explanation: "...",
summary: "...",
interviewQuestions: "..."
}
Using StringOutputParser
The model normally returns an AIMessage.
If we want plain strings, we can add StringOutputParser.
const parser =
new StringOutputParser();
Now create the chains:
const explanationChain =
explanationPrompt
.pipe(openAIModel)
.pipe(parser);
const summaryChain =
summaryPrompt
.pipe(openAIModel)
.pipe(parser);
const interviewChain =
interviewPrompt
.pipe(openAIModel)
.pipe(parser);
Create the parallel workflow:
const parallelChain =
RunnableParallel.from({
explanation: explanationChain,
summary: summaryChain,
interviewQuestions: interviewChain,
});
Invoke:
const result =
await parallelChain.invoke({
topic: "JavaScript",
});
console.log(result);
Now every property contains a plain string.
Running OpenAI and Gemini in Parallel
This is where RunnableParallel becomes particularly interesting.
Suppose we want to ask the same question to both OpenAI and Gemini.
┌──→ OpenAI
│
User Question ──────┤
│
└──→ Gemini
Let's create two chains.
const openAIChain =
explanationPrompt
.pipe(openAIModel)
.pipe(parser);
const geminiChain =
explanationPrompt
.pipe(geminiModel)
.pipe(parser);
Create the parallel workflow:
const parallelChain =
RunnableParallel.from({
openAI: openAIChain,
gemini: geminiChain,
});
Invoke:
const result =
await parallelChain.invoke({
topic: "Node.js",
});
console.log(result);
Output:
{
openAI: "...",
gemini: "..."
}
Both models receive the same input.
Why Would We Run Multiple Models?
There are several practical reasons.
1. Compare responses
You can compare answers from different models.
OpenAI
↓
Answer A
Gemini
↓
Answer B
Your application can then choose the better response.
2. Use different models for different tasks
For example:
Question
│
├──→ Fast Model → Summary
│
└──→ Advanced Model → Detailed Analysis
3. Build fallback or evaluation systems
You can send the same prompt to multiple models and evaluate their responses.
RunnableParallel with Different Prompts
Parallel execution doesn't require the same prompt.
For example:
┌──→ Explain Topic
│
Topic ─────────────┼──→ Generate Questions
│
└──→ Generate Summary
Code:
const explanationChain =
PromptTemplate.fromTemplate(
"Explain {topic} in simple words."
)
.pipe(openAIModel)
.pipe(parser);
const questionsChain =
PromptTemplate.fromTemplate(
"Generate 5 interview questions about {topic}."
)
.pipe(geminiModel)
.pipe(parser);
const summaryChain =
PromptTemplate.fromTemplate(
"Summarize {topic} in 3 bullet points."
)
.pipe(openAIModel)
.pipe(parser);
Now combine them:
const parallelChain =
RunnableParallel.from({
explanation: explanationChain,
questions: questionsChain,
summary: summaryChain,
});
Invoke:
const result =
await parallelChain.invoke({
topic: "React",
});
console.log(result);
Output Structure
The result might look like:
{
explanation: "React is a JavaScript library...",
questions: `
1. What is JSX?
2. What are hooks?
3. What is reconciliation?
4. What is Virtual DOM?
5. What is state?
`,
summary: `
- React is component based.
- React uses JSX.
- React manages UI state.
`
}
One input produced multiple independent outputs.
RunnableParallel Can Also Work Inside a Sequence
This is an important concept.
RunnableParallel itself is a Runnable.
That means you can put it inside a RunnableSequence.
For example:
Input
↓
Prompt
↓
Parallel
├──→ OpenAI
└──→ Gemini
↓
Combined Result
Conceptually:
const chain =
prompt
.pipe(parallelChain);
This is possible because both RunnableSequence and RunnableParallel follow the Runnable interface.
This is a powerful example of LangChain's composability.
Combining Sequential and Parallel Workflows
Now let's build something more realistic.
Suppose we want to:
-
Receive a topic.
-
Generate a basic explanation.
-
Send that explanation to two independent evaluators.
-
Return both evaluations.
The workflow becomes:
Topic
↓
Explanation
↓
┌───────────────┐
│ │
▼ ▼
OpenAI Gemini
│ │
▼ ▼
Evaluation A Evaluation B
This combines:
-
RunnableSequence -
RunnableParallel
This is a common pattern in more advanced AI workflows.
Real-World Example: AI Content Analyzer
Imagine you're building a content analysis application.
A user submits an article.
You want to calculate:
-
Summary
-
Sentiment
-
Keywords
-
SEO suggestions
These tasks are independent.
So instead of:
Article
↓
Summary
↓
Sentiment
↓
Keywords
↓
SEO
you can build:
┌──→ Summary
│
Article ────────────┼──→ Sentiment
│
├──→ Keywords
│
└──→ SEO
Each task can run independently.
Real-World Example: AI Interview Assistant
Suppose the user enters:
"Explain React hooks."
Your application could simultaneously generate:
┌──→ Explanation
│
Question ───────────┼──→ Follow-up Question
│
└──→ Interview Tips
The UI can then display all three results.
Real-World Example: Multi-Model AI Evaluation
This is another interesting use case.
Send the same prompt to multiple models:
┌──→ OpenAI
│
Question ───────────┼──→ Gemini
│
└──→ Another Model
Then collect:
{
openAI: "...",
gemini: "...",
otherModel: "..."
}
You can then pass those responses to another step for comparison.
RunnableParallel vs Promise.all()
If you're familiar with JavaScript, you may be wondering:
"Isn't this similar to
Promise.all()?"
Conceptually, yes.
Both are useful when independent operations can execute concurrently.
For example:
const results = await Promise.all([
fetchDataA(),
fetchDataB(),
]);
With LangChain:
const chain =
RunnableParallel.from({
resultA: chainA,
resultB: chainB,
});
The important difference is that RunnableParallel works within LangChain's Runnable composition model.
That means it can be combined with:
-
RunnableSequence -
pipe() -
Models
-
Prompt templates
-
Output parsers
-
Retrievers
-
Other Runnables
So you can build larger workflows without leaving the Runnable architecture.
Important: Parallel Does Not Mean "Faster" in Every Case
This is an important point.
Parallel execution can reduce overall waiting time when tasks are independent, but actual performance depends on:
-
Model response time
-
Provider rate limits
-
Network latency
-
Concurrency limits
-
Number of requests
-
API quotas
For example, if three independent requests each take around 5 seconds:
Sequential:
5 + 5 + 5 = ~15 seconds
Parallel execution may be closer to:
Parallel:
max(5, 5, 5) = ~5 seconds
But this is an idealized example. Real applications can behave differently due to provider limits and network conditions.
Error Handling
Parallel workflows need careful error handling.
Suppose you have:
OpenAI
Gemini
Summary
and Gemini fails.
Your application needs to decide whether:
-
The entire operation should fail.
-
The other results should still be used.
-
A fallback should be used.
A basic example:
try {
const result =
await parallelChain.invoke({
topic: "Node.js",
});
console.log(result);
} catch (error) {
console.error(
"Parallel execution failed:",
error
);
}
For more advanced fallback behavior, LangChain provides additional Runnable features that we'll cover in a later article.
Important: Independent vs Dependent Tasks
This is the most important rule when deciding between RunnableSequence and RunnableParallel.
Use RunnableSequence when:
The next step needs the previous step's output.
Step A
↓
Step B
↓
Step C
Example:
Generate Summary
↓
Translate Summary
↓
Format Translation
Use RunnableParallel when:
The tasks don't depend on each other.
┌──→ Task A
│
Input ──┼──→ Task B
│
└──→ Task C
Example:
Article
├──→ Summary
├──→ Keywords
└──→ Sentiment
RunnableSequence + RunnableParallel
The real power comes when you combine both.
For example:
┌──→ Summary
│
Input
↓ ├──→ Keywords
Prompt
↓ │
Model ───────────┼──→ Sentiment
│
└──→ SEO
This gives you a flexible workflow where some operations happen sequentially and others happen concurrently.
Best Practices
1. Use parallel execution only for independent tasks
Don't parallelize operations that depend on each other.
2. Keep each branch focused
Each branch should ideally perform one clear task.
Summary
Keywords
Sentiment
is easier to maintain than one branch that does everything.
3. Use meaningful result keys
Prefer:
{
summary: summaryChain,
keywords: keywordChain,
sentiment: sentimentChain,
}
instead of:
{
a: chain1,
b: chain2,
c: chain3,
}
Meaningful names make the output easier to understand.
4. Consider provider limits
Running many AI requests concurrently can trigger rate limits.
Don't assume you can create unlimited parallel requests.
5. Handle failures
Production applications should have a strategy for failed branches.
Later in the series, we'll explore fallback mechanisms.
Common Beginner Mistakes
Mistake 1: Using RunnableParallel for dependent operations
Incorrect:
Generate Summary
↓
Translate Summary
The translation depends on the summary.
Use a sequence instead.
Mistake 2: Creating too many parallel requests
Running hundreds of AI requests simultaneously can cause:
-
Rate limits
-
Higher costs
-
Increased resource usage
-
Provider errors
Use concurrency carefully.
Mistake 3: Assuming parallel execution always improves performance
Parallel execution helps when tasks are independent, but network latency, model speed, rate limits, and provider behavior still affect the final performance.
Mistake 4: Ignoring output structure
A parallel workflow returns an object containing the results from each branch.
For example:
{
summary: "...",
keywords: "...",
sentiment: "..."
}
Design your application around this predictable structure.
RunnableSequence vs RunnableParallel
Here's the simplest way to remember the difference:
RunnableSequence
A
↓
B
↓
C
Use it when:
A must finish before B can start.
And:
RunnableParallel
┌→ A
Input├→ B
└→ C
Use it when:
A, B, and C can work independently.
What's Next?
So far, we've learned how to compose workflows using:
-
RunnableSequence -
RunnableParallel
But sometimes you need to add your own JavaScript logic into a LangChain workflow.
For example:
Input
↓
AI Model
↓
Custom JavaScript Function
↓
Next Step
Maybe you want to:
-
Transform data
-
Calculate a value
-
Call your own API
-
Filter results
-
Format an object
-
Apply custom business logic
This is where RunnableLambda becomes useful.
In the next article, we'll learn:
Mastering RunnableLambda in LangChain.js: Add Custom JavaScript Logic to AI Workflows
Final Thoughts
RunnableParallel is an important building block for AI applications that need to perform multiple independent operations.
Instead of processing every task sequentially:
Input
↓
Task A
↓
Task B
↓
Task C
you can execute independent tasks concurrently:
┌──→ Task A
│
Input ─┼──→ Task B
│
└──→ Task C
The real power comes from combining RunnableParallel with other Runnable components.
You can build workflows such as:
Prompt
↓
Model
↓
Parallel
├──→ Summary
├──→ Keywords
└──→ Sentiment
or even combine sequential and parallel execution:
Input
↓
Prompt
↓
Model
↓
Parallel
├──→ OpenAI
└──→ Gemini
↓
Combined Result
Once you understand this pattern, you can start designing much more sophisticated AI workflows.
The key rule to remember is simple:
Use
RunnableSequencewhen tasks depend on each other. UseRunnableParallelwhen tasks are independent.
Happy Coding! 🚀