pub trait Runnable<Input: Send + Sync + 'static, Output: Send + Sync + 'static>: Send + Sync {
type Error: Error + Send + Sync + 'static;
// Required method
fn invoke<'life0, 'async_trait>(
&'life0 self,
input: Input,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Output, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided methods
fn batch<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Output>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn batch_as_completed<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<(usize, Output)>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn stream<'life0, 'async_trait>(
&'life0 self,
input: Input,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn transform<'life0, 'async_trait>(
&'life0 self,
input: Pin<Box<dyn Stream<Item = Result<Input, Self::Error>> + Send>>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send + '_>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
}Expand description
Base trait for all LangChain components.
This trait defines the core interface every component must implement:
- Single execution via
invoke - Batch processing via
batch - Streaming output via
stream - Stream-to-stream transformation via
transform
§Example
use lc_core::runnables::Runnable;
use lc_core::runnables::RunnableConfig;
use async_trait::async_trait;
// Define a simple Runnable: add one
struct AddOne;
#[async_trait]
impl Runnable<i32, i32> for AddOne {
type Error = std::convert::Infallible;
async fn invoke(&self, input: i32, _config: Option<RunnableConfig>) -> Result<i32, Self::Error> {
Ok(input + 1)
}
}Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn batch<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Output>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn batch<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Output>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Batch processing - transforms multiple inputs to outputs.
Default implementation processes inputs concurrently with a bounded
concurrency: config.max_concurrency items run at once (defaults to
all inputs), and results are returned in input order regardless of
completion order (buffered, not buffer_unordered). Override for
provider-level batch optimization.
§Arguments
inputs- Input vector.config- Optional batch configuration.
§Returns
Result vector, ordered as the inputs.
Sourcefn batch_as_completed<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<(usize, Output)>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn batch_as_completed<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<(usize, Output)>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Batch processing that returns results in completion order.
Rust counterpart of Python LCEL’s batch_as_completed: each input is
driven through the full chain via invoke independently, with
concurrency bounded by config.max_concurrency (defaults to all
inputs). The result is a Vec<(usize, Output)> ordered by completion
time, where the usize is the original index in inputs.
Short-circuits on the first error (like batch): if any input fails,
the error is returned immediately and the remaining results are
dropped.
§Example
let results = chain.batch_as_completed(inputs, None).await?;
// 最快完成的那项在 results[0],其下标标识它在 inputs 里的位置
for (index, output) in results {
println!("inputs[{index}] -> {output}");
}Sourcefn stream<'life0, 'async_trait>(
&'life0 self,
input: Input,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn stream<'life0, 'async_trait>(
&'life0 self,
input: Input,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Streaming output - for real-time responses (LLM, etc).
Enables real-time stream processing of output, suitable for chat models, token generation, etc.
§Arguments
input- Input to process.config- Optional configuration.
§Returns
Output stream.
§Default Implementation
Wraps invoke result as single-element stream. Types supporting true streaming should override.
Sourcefn transform<'life0, 'async_trait>(
&'life0 self,
input: Pin<Box<dyn Stream<Item = Result<Input, Self::Error>> + Send>>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send + '_>>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn transform<'life0, 'async_trait>(
&'life0 self,
input: Pin<Box<dyn Stream<Item = Result<Input, Self::Error>> + Send>>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send + '_>>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Stream-to-stream transformation - the core of LCEL streaming.
Takes an input stream and produces an output stream, enabling pipeline streaming without buffering intermediate results.
§Default Implementation
Drives each input item through stream lazily: as soon as an input
item arrives it is immediately run through stream and its output
yielded, before pulling the next input item. This is the LangChain
default transform semantics — downstream receives output incrementally
instead of waiting for the entire input stream to finish, and an
infinite/long-lived upstream never accumulates unboundedly in memory.
A step that overrides stream (e.g. an LLM) yields a real token stream
per item; a step using the default stream maps elementwise via
invoke. Components that want aggregation (e.g. incremental parsers)
should override this method.
§Arguments
input- Input stream to transform.config- Optional execution configuration.
§Returns
Output stream.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".