Runnable Class — langchain Architecture
Architecture documentation for the Runnable class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 4022a4a7_2f12_918e_95f7_87d452c5dc9e["Runnable"] 7370835a_e974_def1_6538_c09e500f69bc["RunnableConfig"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|extends| 7370835a_e974_def1_6538_c09e500f69bc dddce6a3_2420_c71d_01fe_f214c3eb2503["BasePromptTemplate"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|extends| dddce6a3_2420_c71d_01fe_f214c3eb2503 9c9ba0ab_539f_aaa1_399f_ec00f912f41f["_StreamingCallbackHandler"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|extends| 9c9ba0ab_539f_aaa1_399f_ec00f912f41f 5f3c1f1c_6f8a_e293_7cb5_97c21b4bf214["base.py"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|defined in| 5f3c1f1c_6f8a_e293_7cb5_97c21b4bf214 1dc6ac7a_dcbe_0b0a_c9c3_ebaf3a38b9cb["get_name()"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|method| 1dc6ac7a_dcbe_0b0a_c9c3_ebaf3a38b9cb 368f6e6a_f895_2871_89fe_8bf6473a74e2["InputType()"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|method| 368f6e6a_f895_2871_89fe_8bf6473a74e2 5190b3fa_f80f_a987_1443_a76b0f2ced9f["OutputType()"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|method| 5190b3fa_f80f_a987_1443_a76b0f2ced9f 475b10d3_af92_48a6_bf07_fa1cd288556c["input_schema()"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|method| 475b10d3_af92_48a6_bf07_fa1cd288556c 125c96c1_bd40_4c0a_e008_1b2d7d545256["get_input_schema()"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|method| 125c96c1_bd40_4c0a_e008_1b2d7d545256 be1f80bc_a2b2_5a4b_62c1_9ec9265410d2["get_input_jsonschema()"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|method| be1f80bc_a2b2_5a4b_62c1_9ec9265410d2 3c7d26c1_3ee4_dc2e_04ec_04ad4d31ab5c["output_schema()"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|method| 3c7d26c1_3ee4_dc2e_04ec_04ad4d31ab5c 8763aeb7_5982_a349_d09d_56e770883eae["get_output_schema()"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|method| 8763aeb7_5982_a349_d09d_56e770883eae efd65a04_3de0_0a60_de3f_4e9430453720["get_output_jsonschema()"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|method| efd65a04_3de0_0a60_de3f_4e9430453720 f2386feb_545a_6ab4_d828_4b0541f03b83["config_specs()"] 4022a4a7_2f12_918e_95f7_87d452c5dc9e -->|method| f2386feb_545a_6ab4_d828_4b0541f03b83
Relationship Graph
Source Code
libs/core/langchain_core/runnables/base.py lines 124–2583
class Runnable(ABC, Generic[Input, Output]):
"""A unit of work that can be invoked, batched, streamed, transformed and composed.
Key Methods
===========
- `invoke`/`ainvoke`: Transforms a single input into an output.
- `batch`/`abatch`: Efficiently transforms multiple inputs into outputs.
- `stream`/`astream`: Streams output from a single input as it's produced.
- `astream_log`: Streams output and selected intermediate results from an
input.
Built-in optimizations:
- **Batch**: By default, batch runs invoke() in parallel using a thread pool
executor. Override to optimize batching.
- **Async**: Methods with `'a'` prefix are asynchronous. By default, they execute
the sync counterpart using asyncio's thread pool.
Override for native async.
All methods accept an optional config argument, which can be used to configure
execution, add tags and metadata for tracing and debugging etc.
Runnables expose schematic information about their input, output and config via
the `input_schema` property, the `output_schema` property and `config_schema`
method.
Composition
===========
Runnable objects can be composed together to create chains in a declarative way.
Any chain constructed this way will automatically have sync, async, batch, and
streaming support.
The main composition primitives are `RunnableSequence` and `RunnableParallel`.
**`RunnableSequence`** invokes a series of runnables sequentially, with
one Runnable's output serving as the next's input. Construct using
the `|` operator or by passing a list of runnables to `RunnableSequence`.
**`RunnableParallel`** invokes runnables concurrently, providing the same input
to each. Construct it using a dict literal within a sequence or by passing a
dict to `RunnableParallel`.
For example,
```python
from langchain_core.runnables import RunnableLambda
# A RunnableSequence constructed using the `|` operator
sequence = RunnableLambda(lambda x: x + 1) | RunnableLambda(lambda x: x * 2)
sequence.invoke(1) # 4
sequence.batch([1, 2, 3]) # [4, 6, 8]
# A sequence that contains a RunnableParallel constructed using a dict literal
sequence = RunnableLambda(lambda x: x + 1) | {
"mul_2": RunnableLambda(lambda x: x * 2),
"mul_5": RunnableLambda(lambda x: x * 5),
}
sequence.invoke(1) # {'mul_2': 4, 'mul_5': 10}
```
Standard Methods
================
All `Runnable`s expose additional methods that can be used to modify their
behavior (e.g., add a retry policy, add lifecycle listeners, make them
configurable, etc.).
These methods will work on any `Runnable`, including `Runnable` chains
constructed by composing other `Runnable`s.
See the individual methods for details.
For example,
```python
from langchain_core.runnables import RunnableLambda
Defined In
Source
Frequently Asked Questions
What is the Runnable class?
Runnable is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is Runnable defined?
Runnable is defined in libs/core/langchain_core/runnables/base.py at line 124.
What does Runnable extend?
Runnable extends RunnableConfig, BasePromptTemplate, _StreamingCallbackHandler.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free