LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-corerunnablesbaseRunnableParallel
    Class●Since v0.1

    RunnableParallel

    Copy
    RunnableParallel(
      self,
    steps__
    :
    Mapping
    [
    str
    ,
    Runnable
    [
    Input
    ,
    Any
    ]
    |
    Callable
    [
    [
    Input
    ]
    ,
    Any
    ]
    |
    Mapping
    [
    str
    ,
    Runnable
    [
    Input
    ,
    Any
    ]
    |
    Callable
    [
    [
    Input
    ]
    ,
    Any
    ]
    ]
    ]
    |
    None
    =
    None
    ,
    **
    kwargs
    :
    Runnable
    [
    Input
    ,
    Any
    ]
    |
    Callable
    [
    [
    Input
    ]
    ,
    Any
    ]
    |
    Mapping
    [
    str
    ,
    Runnable
    [
    Input
    ,
    Any
    ]
    |
    Callable
    [
    [
    Input
    ]
    ,
    Any
    ]
    ]
    =
    {
    }
    )

    Bases

    RunnableSerializable[Input, dict[str, Any]]

    Used in Docs

    • Google cloud Vertex AI reranker integration

    Constructors

    constructor
    __init__
    NameType
    steps__Mapping[str, Runnable[Input, Any] | Callable[[Input], Any

    Attributes

    attribute
    steps__: Mapping[str, Runnable[Input, Any]]
    attribute
    model_config
    attribute
    InputType: Any
    attribute
    config_specs: list[ConfigurableFieldSpec]

    Methods

    method
    is_lc_serializable
    method
    get_lc_namespace
    method
    get_name
    method
    get_input_schema
    method
    get_output_schema
    method
    get_graph
    method
    invoke
    method
    ainvoke
    method
    transform
    method
    stream
    method
    atransform
    method
    astream

    Inherited fromRunnableSerializable

    Attributes

    Aname: str | None
    —

    The name of the Runnable.

    Methods

    Mto_json
    —

    Serialize the Runnable to JSON.

    Mconfigurable_fields
    —

    Configure particular Runnable fields at runtime.

    Mconfigurable_alternatives
    —

    Configure alternatives for Runnable objects that can be set at runtime.

    Inherited fromSerializable

    Attributes

    Alc_secrets: dict[str, str]
    —

    A map of constructor argument names to secret ids.

    Alc_attributes: dict
    —

    List of attribute names that should be included in the serialized kwargs.

    Methods

    Mlc_id
    —

    Return a unique identifier for this class for serialization purposes.

    Inherited fromRunnable

    Attributes

    Aname: str | None
    —

    The name of the Runnable. Used for debugging and tracing.

    AOutputType: type[Output]
    —

    Output Type.

    Ainput_schema: type[BaseModel]
    —

    The type of input this Runnable accepts specified as a Pydantic model.

    View source on GitHub

    Runnable that runs a mapping of Runnables in parallel.

    Returns a mapping of their outputs.

    RunnableParallel is one of the two main composition primitives, alongside RunnableSequence. It invokes Runnables 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:

    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 Runnables in parallel. In the below example, we simultaneously stream output from two different Runnable objects:

    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

    Parameters

    NameTypeDescription
    steps__Mapping[str, Runnable[Input, Any] | Callable[[Input], Any] | Mapping
    M
    to_json
    —

    Serialize the object to JSON.

    Mto_json_not_implemented
    —

    Serialize a "not implemented" object.

    A
    output_schema
    : type[BaseModel]
    —

    Output schema.

    Methods

    Mget_input_jsonschema
    —

    Get a JSON schema that represents the input to the Runnable.

    Mget_output_jsonschema
    —

    Get a JSON schema that represents the output of the Runnable.

    Mconfig_schema
    —

    The type of config this Runnable accepts specified as a Pydantic model.

    Mget_config_jsonschema
    —

    Get a JSON schema that represents the config of the Runnable.

    Mget_prompts
    —

    Return a list of prompts used by this Runnable.

    Mpipe
    —

    Pipe Runnable objects.

    Mpick
    —

    Pick keys from the output dict of this Runnable.

    Massign
    —

    Assigns new fields to the dict output of this Runnable.

    Mbatch
    —

    Default implementation runs invoke in parallel using a thread pool executor.

    Mbatch_as_completed
    —

    Run invoke in parallel on a list of inputs.

    Mabatch
    —

    Default implementation runs ainvoke in parallel using asyncio.gather.

    Mabatch_as_completed
    —

    Run ainvoke in parallel on a list of inputs.

    Mastream_log
    —

    Stream all output from a Runnable, as reported to the callback system.

    Mastream_events
    —

    Generate a stream of events.

    Mstream_events
    —

    Generate a stream of events synchronously.

    Mbind
    —

    Bind arguments to a Runnable, returning a new Runnable.

    Mwith_config
    —

    Bind config to a Runnable, returning a new Runnable.

    Mwith_listeners
    —

    Bind lifecycle listeners to a Runnable, returning a new Runnable.

    Mwith_alisteners
    —

    Bind async lifecycle listeners to a Runnable.

    Mwith_types
    —

    Bind input and output types to a Runnable, returning a new Runnable.

    Mwith_retry
    —

    Create a new Runnable that retries the original Runnable on exceptions.

    Mmap
    —

    Return a new Runnable that maps a list of inputs to a list of outputs.

    Mwith_fallbacks
    —

    Add fallbacks to a Runnable, returning a new Runnable.

    Mas_tool
    —

    Create a BaseTool from a Runnable.

    [
    str
    ,
    Runnable
    [
    Input
    ,
    Any
    ] |
    Callable
    [[
    Input
    ],
    Any
    ]]] |
    None
    Default:None

    The steps to include.

    **kwargsRunnable[Input, Any] | Callable[[Input], Any] | Mapping[str, Runnable[Input, Any] | Callable[[Input], Any]]
    Default:{}
    ] |
    Mapping
    [
    str
    ,
    Runnable
    [
    Input
    ,
    Any
    ] |
    Callable
    [[
    Input
    ],
    Any
    ]]] |
    None

    The type of the input to the Runnable.

    Get the config specs of the Runnable.

    Return True as this class is serializable.

    Get the namespace of the LangChain object.

    Get the name of the Runnable.

    Get the input schema of the Runnable.

    Get the output schema of the Runnable.

    Get the graph representation of the Runnable.

    Additional steps to include.