RunnableSequence Class — langchain Architecture
Architecture documentation for the RunnableSequence class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 65091a88_74f0_3f4b_bf31_1d54a0bca200["RunnableSequence"] 65091a88_74f0_3f4b_bf31_1d54a0bca200["RunnableSequence"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|extends| 65091a88_74f0_3f4b_bf31_1d54a0bca200 5f3c1f1c_6f8a_e293_7cb5_97c21b4bf214["base.py"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|defined in| 5f3c1f1c_6f8a_e293_7cb5_97c21b4bf214 5537c44a_82ac_f065_29d0_a67f9339ea97["__init__()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 5537c44a_82ac_f065_29d0_a67f9339ea97 6e9b0668_f2be_2262_659a_a68ca324a406["get_lc_namespace()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 6e9b0668_f2be_2262_659a_a68ca324a406 6dde49b0_09b2_d1ac_fdda_497e8b98e521["steps()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 6dde49b0_09b2_d1ac_fdda_497e8b98e521 4a9835f6_bddf_6df1_f5dd_17d0dd5176b8["is_lc_serializable()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 4a9835f6_bddf_6df1_f5dd_17d0dd5176b8 3b78c7d6_379e_b2d5_1d91_ba027d8c8375["InputType()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 3b78c7d6_379e_b2d5_1d91_ba027d8c8375 8c8e75e5_b75c_0c99_e7d8_c689d654a85b["OutputType()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 8c8e75e5_b75c_0c99_e7d8_c689d654a85b 46e37b5b_f6c1_727b_5e9b_8951d46e5dba["get_input_schema()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 46e37b5b_f6c1_727b_5e9b_8951d46e5dba 53ea2a4a_9e29_88d1_5137_c8832858939e["get_output_schema()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 53ea2a4a_9e29_88d1_5137_c8832858939e 7e292c91_c407_fd4d_7366_c519c4def658["config_specs()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 7e292c91_c407_fd4d_7366_c519c4def658 83b04009_6c96_a208_4f7a_8ced84cb70e8["get_graph()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 83b04009_6c96_a208_4f7a_8ced84cb70e8 82a8ec27_37a1_d8ec_a5fa_73e329b328ec["__repr__()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 82a8ec27_37a1_d8ec_a5fa_73e329b328ec 9d86dd04_96aa_b6dc_117b_e4ec34cb6dfb["__or__()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 9d86dd04_96aa_b6dc_117b_e4ec34cb6dfb 522ab7ce_4289_77ed_49e2_9ac1dd8ca4da["__ror__()"] 65091a88_74f0_3f4b_bf31_1d54a0bca200 -->|method| 522ab7ce_4289_77ed_49e2_9ac1dd8ca4da
Relationship Graph
Source Code
libs/core/langchain_core/runnables/base.py lines 2817–3562
class RunnableSequence(RunnableSerializable[Input, Output]):
"""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
Defined In
Extends
Source
Frequently Asked Questions
What is the RunnableSequence class?
RunnableSequence is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is RunnableSequence defined?
RunnableSequence is defined in libs/core/langchain_core/runnables/base.py at line 2817.
What does RunnableSequence extend?
RunnableSequence extends RunnableSequence.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free