LangChain Expression Language (LCEL) enables declarative, composable logic for LLM workflows. A standout feature is RunnableParallel, which allows you to run multiple chains concurrently, perfect for performance-sensitive applications that need multiple LLM outputs in parallel.
In this blog, we’ll build a pipeline that runs title generation, summary generation, and tag suggestion concurrently for a given input article all using LCEL’s RunnableParallel pattern.
Tools & Stack
- LangChain
- LCEL (Expression Language)
- OpenAI / Groq / Gemini as LLM provider
- FastAPI (optional for deployment)
Use Case: Multi-Task LLM Processing Pipeline
For a given article input, we want to generate:
- A catchy title
- A 1-line summary
- A list of semantic tags
Instead of calling these in sequence (slow), we’ll parallelize the chains.
Step 1: Install Dependencies
pip install langchain openai
Step 2: Define Individual Chains
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
llm = ChatOpenAI(model_name="gpt-3.5-turbo")
# Prompts
title_prompt = PromptTemplate.from_template("Generate a short title for: {input}")
summary_prompt = PromptTemplate.from_template("Summarize in one line: {input}")
tags_prompt = PromptTemplate.from_template("List 5 tags for: {input}")
# Chains
title_chain = LLMChain(llm=llm, prompt=title_prompt)
summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
tags_chain = LLMChain(llm=llm, prompt=tags_prompt)
Step 3: Compose with RunnableParallel
from langchain_core.runnables import RunnableParallel
parallel_chain = RunnableParallel(
title=title_chain,
summary=summary_chain,
tags=tags_chain
)
response = parallel_chain.invoke({"input": article_text})
Output:
{
"title": "Revolutionizing AI with Open Tools",
"summary": "This article explores open-source momentum in AI models and tooling.",
"tags": "['AI', 'open-source', 'LLMs', 'research', 'deployment']"
}
Handling Race Conditions
While LLMs are non-blocking in most hosted setups (e.g., OpenAI/Groq), race conditions can happen when dealing with:
- External API limits
- Rate throttling
- Downstream dependencies (e.g., DB writes)
from langchain_core.runnables import RunnableLambda
merge_and_log = RunnableLambda(lambda result: {
"title": result['title'].strip(),
"summary": result['summary'].strip(),
"tags": result['tags'].lower().split(',')
})
pipeline = parallel_chain | merge_and_log
FastAPI Deployment (Optional)
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class ArticleInput(BaseModel):
input: str
@app.post("/generate")
async def generate(article: ArticleInput):
result = await pipeline.ainvoke({"input": article.input})
return result
Benefits of RunnableParallel
- Massive speed gains: Chains run simultaneously, reducing latency
- Cleaner code: Parallel tasks are grouped declaratively
- Composable: Easy to plug into larger LCEL workflows
Conclusion
LangChain’s RunnableParallel is a powerful utility that turns your LLM workflows from sequential to concurrent, without compromising modularity. For production apps — especially those doing summarization, tagging, classification, and enrichment — this pattern delivers major performance and design wins.
