0
0
LangchainHow-ToBeginner ยท 4 min read

How to Use RunnableParallel in Langchain: Syntax and Example

In Langchain, RunnableParallel lets you run multiple runnables at the same time to speed up processing. You create it by passing a list of runnables, then call invoke to get all results concurrently.
๐Ÿ“

Syntax

The RunnableParallel class takes a list of runnable objects and runs them concurrently. You create it by passing a list of runnables to its constructor. Then, you call invoke(input) with the input to run all tasks in parallel and get their results as a list.

  • RunnableParallel(runnables: List[Runnable]): Constructor that accepts multiple runnable instances.
  • invoke(input: Any) -> List[Any]: Runs all runnables with the same input concurrently and returns their outputs as a list.
python
from langchain.schema.runnable import RunnableParallel

# Create RunnableParallel with multiple runnables
parallel = RunnableParallel([runnable1, runnable2, runnable3])

# Run all runnables concurrently with the same input
results = parallel.invoke(input_data)
๐Ÿ’ป

Example

This example shows how to run two simple runnables in parallel: one that uppercases text and another that reverses it. The RunnableParallel runs both at the same time and returns their results as a list.

python
from langchain.schema.runnable import Runnable, RunnableParallel

class UppercaseRunnable(Runnable):
    def invoke(self, input: str) -> str:
        return input.upper()

class ReverseRunnable(Runnable):
    def invoke(self, input: str) -> str:
        return input[::-1]

# Create instances of runnables
uppercase = UppercaseRunnable()
reverse = ReverseRunnable()

# Create RunnableParallel with both runnables
parallel = RunnableParallel([uppercase, reverse])

# Run both with the same input
results = parallel.invoke("hello")
print(results)
Output
['HELLO', 'olleh']
โš ๏ธ

Common Pitfalls

Common mistakes when using RunnableParallel include:

  • Passing non-runnable objects instead of runnable instances.
  • Expecting different inputs per runnable; RunnableParallel uses the same input for all.
  • Not handling exceptions inside individual runnables, which can cause the whole parallel run to fail.

Always ensure each runnable can handle the input and errors gracefully.

python
from langchain.schema.runnable import Runnable, RunnableParallel

# Wrong: passing non-runnable objects
# parallel = RunnableParallel(["not a runnable", 123])  # This will raise an error

# Right: pass runnable instances
class EchoRunnable(Runnable):
    def invoke(self, input: str) -> str:
        return input

parallel = RunnableParallel([EchoRunnable()])
results = parallel.invoke("test")
print(results)  # ['test']
Output
['test']
๐Ÿ“Š

Quick Reference

MethodDescription
RunnableParallel(runnables: List[Runnable])Create a parallel runnable with multiple runnables.
invoke(input: Any) -> List[Any]Run all runnables concurrently with the same input and get list of outputs.
Each runnable.invoke(input) -> outputIndividual runnable must implement invoke method to process input.
โœ…

Key Takeaways

RunnableParallel runs multiple runnables concurrently with the same input.
Pass only runnable instances to RunnableParallel constructor.
invoke returns a list of results matching the order of runnables.
Handle errors inside each runnable to avoid breaking parallel execution.
RunnableParallel is useful to speed up independent tasks in Langchain.