0
0
LangChainframework~10 mins

Parallel execution with RunnableParallel in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parallel execution with RunnableParallel
Start RunnableParallel
Receive multiple runnables
Start all runnables at once
Each runnable runs independently
Wait for all runnables to finish
Collect all outputs
Return combined results
End
RunnableParallel runs multiple tasks at the same time, waits for all to finish, then returns all results together.
Execution Sample
LangChain
from langchain.schema.runnables import RunnableParallel, RunnableLambda

r1 = RunnableLambda(lambda x: x + 1)
r2 = RunnableLambda(lambda x: x * 2)
parallel = RunnableParallel(r1=r1, r2=r2)
result = parallel.invoke(3)
This code runs two simple functions in parallel on input 3 and collects their results.
Execution Table
StepActionRunnableInputOutputNotes
1Start RunnableParallel with two runnablesr1, r23Prepare to run both functions in parallel
2Invoke r1r134r1 adds 1 to input
3Invoke r2r236r2 multiplies input by 2
4Wait for both runnables to finishr1, r2Parallel execution completes
5Collect outputsr1, r2{'r1': 4, 'r2': 6}Results combined in dict
6Return combined resultsRunnableParallel3{'r1': 4, 'r2': 6}Final output returned
7ExitAll runnables completed and results returned
💡 All runnables finished, combined results returned as dict
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
input_value33333
r1_outputNone4444
r2_outputNoneNone666
combined_resultNoneNoneNone{'r1': 4, 'r2': 6}{'r1': 4, 'r2': 6}
Key Moments - 3 Insights
Why do both runnables receive the same input value?
RunnableParallel sends the same input to each runnable independently, as shown in steps 2 and 3 where both get input 3.
How does RunnableParallel know when all runnables have finished?
It waits for all runnables to complete their invoke calls before moving to step 4, ensuring all outputs are ready.
Why are the outputs combined into a dict?
RunnableParallel collects each runnable's output in order and returns them as a dict, as seen in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of runnable r1 at step 2?
A6
B4
C3
DNone
💡 Hint
Check the 'Output' column in row for step 2 in execution_table
At which step does RunnableParallel collect all outputs?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the row mentioning 'Collect outputs' in execution_table
If the input changed to 5, what would be the output of r2 at step 3?
A10
B5
C6
D11
💡 Hint
r2 multiplies input by 2, see step 3 output calculation
Concept Snapshot
RunnableParallel runs multiple tasks at the same time.
Each runnable gets the same input independently.
All runnables run in parallel.
RunnableParallel waits for all to finish.
It returns a dict of all outputs in order.
Full Transcript
RunnableParallel is a tool in langchain that lets you run several tasks at once. You give it several runnables, and it sends the same input to each one. All runnables start running at the same time. RunnableParallel waits until every runnable finishes its work. Then it collects all their outputs into a dict and returns it. For example, if you have two runnables, one adding 1 and one multiplying by 2, and you input 3, r1 outputs 4 and r2 outputs 6. RunnableParallel returns {'r1': 4, 'r2': 6}. This helps you do many things at once and get all results together.