Consider two simple runnables that return strings. They are run in parallel using RunnableParallel. What will be the combined output?
from langchain.schema.runnable import Runnable from langchain.schema.runnable.parallel import RunnableParallel class RunnableA(Runnable): def invoke(self, input): return f"A received {input}" class RunnableB(Runnable): def invoke(self, input): return f"B received {input}" parallel = RunnableParallel(A=RunnableA(), B=RunnableB()) result = parallel.invoke("test") print(result)
RunnableParallel returns a dict of results keyed by the branch names.
RunnableParallel runs each named runnable in parallel and collects their outputs in a dict with branch names as keys. Each runnable returns a string with the input included, so the result is a dict of those strings.
When you run RunnableParallel.invoke() with multiple runnables, what type of data structure do you get back?
from langchain.schema.runnable.parallel import RunnableParallel parallel = RunnableParallel(a=runnable1, b=runnable2) result = parallel.invoke("input") print(type(result))
Think about how multiple results are collected from parallel runnables.
RunnableParallel.invoke() returns a dict containing the results from each runnable keyed by branch names.
You want to run three runnables in parallel. Which code snippet correctly creates a RunnableParallel instance with them?
Check the constructor signature for RunnableParallel.
RunnableParallel expects runnables passed as keyword arguments with names, e.g. RunnableParallel(r1=r1, r2=r2, r3=r3). Passing as positional arguments, sets, or unknown kwargs is invalid.
Given the code below, why does it raise a TypeError when invoking?
from langchain.schema.runnable import Runnable from langchain.schema.runnable.parallel import RunnableParallel class RunnableX(Runnable): def invoke(self, input): return input * 2 parallel = RunnableParallel(x=RunnableX(), bad="not a runnable") result = parallel.invoke(5)
Check the types of elements passed to RunnableParallel.
RunnableParallel expects all branches to be Runnable instances. Passing a string causes a TypeError when trying to call invoke on it.
If one runnable in a RunnableParallel raises an exception during invoke, what happens to the overall RunnableParallel invocation?
Think about error handling in parallel execution.
RunnableParallel does not catch exceptions internally. If any runnable raises an exception, it propagates and causes the entire invocation to fail.