Runnable that runs a mapping of Runnables in parallel, and returns a mapping of their outputs. RunnableParallel is one of the two main composition primitives for the LCEL, alongside RunnableSequence. It invokes Runnables concurrently, providing the same input to each. A Runnabl
| 2910 | |
| 2911 | |
| 2912 | class RunnableParallel(RunnableSerializable[Input, Dict[str, Any]]): |
| 2913 | """Runnable that runs a mapping of Runnables in parallel, and returns a mapping |
| 2914 | of their outputs. |
| 2915 | |
| 2916 | RunnableParallel is one of the two main composition primitives for the LCEL, |
| 2917 | alongside RunnableSequence. It invokes Runnables concurrently, providing the same |
| 2918 | input to each. |
| 2919 | |
| 2920 | A RunnableParallel can be instantiated directly or by using a dict literal within a |
| 2921 | sequence. |
| 2922 | |
| 2923 | Here is a simple example that uses functions to illustrate the use of |
| 2924 | RunnableParallel: |
| 2925 | |
| 2926 | .. code-block:: python |
| 2927 | |
| 2928 | from langchain_core.runnables import RunnableLambda |
| 2929 | |
| 2930 | def add_one(x: int) -> int: |
| 2931 | return x + 1 |
| 2932 | |
| 2933 | def mul_two(x: int) -> int: |
| 2934 | return x * 2 |
| 2935 | |
| 2936 | def mul_three(x: int) -> int: |
| 2937 | return x * 3 |
| 2938 | |
| 2939 | runnable_1 = RunnableLambda(add_one) |
| 2940 | runnable_2 = RunnableLambda(mul_two) |
| 2941 | runnable_3 = RunnableLambda(mul_three) |
| 2942 | |
| 2943 | sequence = runnable_1 | { # this dict is coerced to a RunnableParallel |
| 2944 | "mul_two": runnable_2, |
| 2945 | "mul_three": runnable_3, |
| 2946 | } |
| 2947 | # Or equivalently: |
| 2948 | # sequence = runnable_1 | RunnableParallel( |
| 2949 | # {"mul_two": runnable_2, "mul_three": runnable_3} |
| 2950 | # ) |
| 2951 | # Also equivalently: |
| 2952 | # sequence = runnable_1 | RunnableParallel( |
| 2953 | # mul_two=runnable_2, |
| 2954 | # mul_three=runnable_3, |
| 2955 | # ) |
| 2956 | |
| 2957 | sequence.invoke(1) |
| 2958 | await sequence.ainvoke(1) |
| 2959 | |
| 2960 | sequence.batch([1, 2, 3]) |
| 2961 | await sequence.abatch([1, 2, 3]) |
| 2962 | |
| 2963 | RunnableParallel makes it easy to run Runnables in parallel. In the below example, |
| 2964 | we simultaneously stream output from two different Runnables: |
| 2965 | |
| 2966 | .. code-block:: python |
| 2967 | |
| 2968 | from langchain_core.prompts import ChatPromptTemplate |
| 2969 | from langchain_core.runnables import RunnableParallel |
no outgoing calls