Module 4 Lesson 5: Chain Composition and Aggregation
Parallel Reasoning. How to run multiple chains at the same time and combine their results into a final synthesized answer.
Chain Composition: Parallel Power
So far we have looked at Sequential chains (A then B). But what if you want to perform three different tasks at once?
- Task 1: Check grammar.
- Task 2: Check factuality.
- Task 3: Check tone.
Running these one after the other would take 3x as long. RunnableParallel allows you to run them all at the exact same time.
1. Using RunnableParallel
The dictionary syntax in LCEL ({ "key": ... }) is shorthand for RunnableParallel.
from langchain_core.runnables import RunnableParallel
# 1. Define individual specialist chains
joke_chain = ChatPromptTemplate.from_template("Joke about {topic}") | model
poem_chain = ChatPromptTemplate.from_template("Poem about {topic}") | model
# 2. RUN IN PARALLEL
map_chain = RunnableParallel(joke=joke_chain, poem=poem_chain)
# Result will be a dictionary with both results
result = map_chain.invoke({"topic": "Programming"})
print(result["joke"])
print(result["poem"])
2. Aggregating Results
Often, you want to run parallel checks and then have a Final Model summarize the results.
graph LR
Input --> C1[Summarizer]
Input --> C2[Sentiment Analysis]
C1 --> Combine[Final Report Agent]
C2 --> Combine
Combine --> Output
3. Why Composition Wins
- Speed: Parallel processing is significantly faster for the user.
- Modularity: You can test the 'Joke Chain' in isolation before adding it to the 'Parallel Map'.
- Accuracy: Specialization (Lesson 4) combined with Parallelism leads to the most comprehensive AI outputs.
4. The itemgetter Pattern
When composing complex chains, you might only need "One" piece of the dictionary output for the next step. We use Python's itemgetter to pluck that data.
from operator import itemgetter
final_chain = (
{"data": map_chain}
| ChatPromptTemplate.from_template("Summarize this: {data}")
| model
)
5. Engineering Tip: Resource Management
Be careful! If you run 20 chains in parallel, you are sending 20 separate API requests at once. ensure your Rate Limits (Module 13) can handle the burst.
Key Takeaways
RunnableParallel(dictionary syntax) enables concurrent execution of chains.- Parallelism is the best way to reduce latency in complex workflows.
- Results are collected into a Dictionary for easy processing.
- Composition allows you to build a "Team" of agents that work together.