Home / Class/ RunnableParallel Class — langchain Architecture

RunnableParallel Class — langchain Architecture

Architecture documentation for the RunnableParallel class in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  ab1f5091_3b84_0155_e2c8_371b1e7e9630["RunnableParallel"]
  5f3c1f1c_6f8a_e293_7cb5_97c21b4bf214["base.py"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|defined in| 5f3c1f1c_6f8a_e293_7cb5_97c21b4bf214
  04c68bd7_a799_fa32_24ea_66ec86d1d211["__init__()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| 04c68bd7_a799_fa32_24ea_66ec86d1d211
  5735cc2d_ef07_dd87_797b_d2ca1252eebf["is_lc_serializable()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| 5735cc2d_ef07_dd87_797b_d2ca1252eebf
  29bc2552_48fd_f782_ee8c_e34ada4aec0d["get_lc_namespace()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| 29bc2552_48fd_f782_ee8c_e34ada4aec0d
  e9469d43_b057_bd44_72a5_6fdf965c9bca["get_name()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| e9469d43_b057_bd44_72a5_6fdf965c9bca
  5de4d70f_5c51_6771_f0db_c5701dfe645e["InputType()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| 5de4d70f_5c51_6771_f0db_c5701dfe645e
  81b31591_9ce2_e4ba_7dbe_ff586a97133a["get_input_schema()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| 81b31591_9ce2_e4ba_7dbe_ff586a97133a
  0155f98c_e79b_94c6_9a87_110abe53315f["get_output_schema()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| 0155f98c_e79b_94c6_9a87_110abe53315f
  a8558920_4284_3da6_482d_fff99363cb2c["config_specs()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| a8558920_4284_3da6_482d_fff99363cb2c
  85e4d484_2be0_32c5_d09a_3bcf0efabda7["get_graph()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| 85e4d484_2be0_32c5_d09a_3bcf0efabda7
  3b90e0d4_aa2e_594e_014c_b096830f0a35["__repr__()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| 3b90e0d4_aa2e_594e_014c_b096830f0a35
  f1e0a6f7_e5a9_f2ac_fde5_aa75bc2b4540["invoke()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| f1e0a6f7_e5a9_f2ac_fde5_aa75bc2b4540
  6e14738a_0775_7c6b_a8a4_5439435199ce["ainvoke()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| 6e14738a_0775_7c6b_a8a4_5439435199ce
  20459eea_49c1_8b12_50d4_f12ef79e2989["_transform()"]
  ab1f5091_3b84_0155_e2c8_371b1e7e9630 -->|method| 20459eea_49c1_8b12_50d4_f12ef79e2989

Relationship Graph

Source Code

libs/core/langchain_core/runnables/base.py lines 3565–4089

class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
    """Runnable that runs a mapping of `Runnable`s in parallel.

    Returns a mapping of their outputs.

    `RunnableParallel` is one of the two main composition primitives,
    alongside `RunnableSequence`. It invokes `Runnable`s concurrently, providing the
    same input to each.

    A `RunnableParallel` can be instantiated directly or by using a dict literal
    within a sequence.

    Here is a simple example that uses functions to illustrate the use of
    `RunnableParallel`:

        ```python
        from langchain_core.runnables import RunnableLambda


        def add_one(x: int) -> int:
            return x + 1


        def mul_two(x: int) -> int:
            return x * 2


        def mul_three(x: int) -> int:
            return x * 3


        runnable_1 = RunnableLambda(add_one)
        runnable_2 = RunnableLambda(mul_two)
        runnable_3 = RunnableLambda(mul_three)

        sequence = runnable_1 | {  # this dict is coerced to a RunnableParallel
            "mul_two": runnable_2,
            "mul_three": runnable_3,
        }
        # Or equivalently:
        # sequence = runnable_1 | RunnableParallel(
        #     {"mul_two": runnable_2, "mul_three": runnable_3}
        # )
        # Also equivalently:
        # sequence = runnable_1 | RunnableParallel(
        #     mul_two=runnable_2,
        #     mul_three=runnable_3,
        # )

        sequence.invoke(1)
        await sequence.ainvoke(1)

        sequence.batch([1, 2, 3])
        await sequence.abatch([1, 2, 3])
        ```

    `RunnableParallel` makes it easy to run `Runnable`s in parallel. In the below
    example, we simultaneously stream output from two different `Runnable` objects:

        ```python
        from langchain_core.prompts import ChatPromptTemplate
        from langchain_core.runnables import RunnableParallel
        from langchain_openai import ChatOpenAI

        model = ChatOpenAI()
        joke_chain = (
            ChatPromptTemplate.from_template("tell me a joke about {topic}") | model
        )
        poem_chain = (
            ChatPromptTemplate.from_template("write a 2-line poem about {topic}")
            | model
        )

        runnable = RunnableParallel(joke=joke_chain, poem=poem_chain)

        # Display stream
        output = {key: "" for key, _ in runnable.output_schema()}
        for chunk in runnable.stream({"topic": "bear"}):
            for key in chunk:
                output[key] = output[key] + chunk[key].content
            print(output)  # noqa: T201

Frequently Asked Questions

What is the RunnableParallel class?
RunnableParallel is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is RunnableParallel defined?
RunnableParallel is defined in libs/core/langchain_core/runnables/base.py at line 3565.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free