# RunnableSequence

> **Class** in `langchain_core`

📖 [View in docs](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence)

Sequence of `Runnable` objects, where the output of one is the input of the next.

**`RunnableSequence`** is the most important composition operator in LangChain
as it is used in virtually every chain.

A `RunnableSequence` can be instantiated directly or more commonly by using the
`|` operator where either the left or right operands (or both) must be a
`Runnable`.

Any `RunnableSequence` automatically supports sync, async, batch.

The default implementations of `batch` and `abatch` utilize threadpools and
asyncio gather and will be faster than naive invocation of `invoke` or `ainvoke`
for IO bound `Runnable`s.

Batching is implemented by invoking the batch method on each component of the
`RunnableSequence` in order.

A `RunnableSequence` preserves the streaming properties of its components, so if
all components of the sequence implement a `transform` method -- which
is the method that implements the logic to map a streaming input to a streaming
output -- then the sequence will be able to stream input to output!

If any component of the sequence does not implement transform then the
streaming will only begin after this component is run. If there are
multiple blocking components, streaming begins after the last one.

!!! note
    `RunnableLambdas` do not support `transform` by default! So if you need to
    use a `RunnableLambdas` be careful about where you place them in a
    `RunnableSequence` (if you need to use the `stream`/`astream` methods).

    If you need arbitrary logic and need streaming, you can subclass
    Runnable, and implement `transform` for whatever logic you need.

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

    ```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

    runnable_1 = RunnableLambda(add_one)
    runnable_2 = RunnableLambda(mul_two)
    sequence = runnable_1 | runnable_2
    # Or equivalently:
    # sequence = RunnableSequence(first=runnable_1, last=runnable_2)
    sequence.invoke(1)
    await sequence.ainvoke(1)

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

Here's an example that uses streams JSON output generated by an LLM:

    ```python
    from langchain_core.output_parsers.json import SimpleJsonOutputParser
    from langchain_openai import ChatOpenAI

    prompt = PromptTemplate.from_template(
        "In JSON format, give me a list of {topic} and their "
        "corresponding names in French, Spanish and in a "
        "Cat Language."
    )

    model = ChatOpenAI()
    chain = prompt | model | SimpleJsonOutputParser()

    async for chunk in chain.astream({"topic": "colors"}):
        print("-")  # noqa: T201
        print(chunk, sep="", flush=True)  # noqa: T201
    ```

## Signature

```python
RunnableSequence(
    self,
    *steps: RunnableLike = (),
    name: str | None = None,
    first: Runnable[Any, Any] | None = None,
    middle: list[Runnable[Any, Any]] | None = None,
    last: Runnable[Any, Any] | None = None,
)
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `steps` | `RunnableLike` | No | The steps to include in the sequence. (default: `()`) |
| `name` | `str \| None` | No | The name of the `Runnable`. (default: `None`) |
| `first` | `Runnable[Any, Any] \| None` | No | The first `Runnable` in the sequence. (default: `None`) |
| `middle` | `list[Runnable[Any, Any]] \| None` | No | The middle `Runnable` objects in the sequence. (default: `None`) |
| `last` | `Runnable[Any, Any] \| None` | No | The last `Runnable` in the sequence. (default: `None`) |

## Extends

- `RunnableSerializable[Input, Output]`

## Constructors

```python
__init__(
    self,
    *steps: RunnableLike = (),
    name: str | None = None,
    first: Runnable[Any, Any] | None = None,
    middle: list[Runnable[Any, Any]] | None = None,
    last: Runnable[Any, Any] | None = None,
) -> None
```

| Name | Type |
|------|------|
| `name` | `str \| None` |
| `first` | `Runnable[Any, Any] \| None` |
| `middle` | `list[Runnable[Any, Any]] \| None` |
| `last` | `Runnable[Any, Any] \| None` |


## Properties

- `first`
- `middle`
- `last`
- `steps`
- `model_config`
- `InputType`
- `OutputType`
- `config_specs`

## Methods

- [`get_lc_namespace()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/get_lc_namespace)
- [`is_lc_serializable()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/is_lc_serializable)
- [`get_input_schema()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/get_input_schema)
- [`get_output_schema()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/get_output_schema)
- [`get_graph()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/get_graph)
- [`invoke()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/invoke)
- [`ainvoke()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/ainvoke)
- [`batch()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/batch)
- [`abatch()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/abatch)
- [`transform()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/transform)
- [`stream()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/stream)
- [`atransform()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/atransform)
- [`astream()`](https://reference.langchain.com/python/langchain-core/runnables/base/RunnableSequence/astream)

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/f0c5a28fa05adcda89aebcb449d897245ab21fa4/libs/core/langchain_core/runnables/base.py#L2817)