Skip to main content

Runnable

Trait Runnable 

Source
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§

Source

type Error: Error + Send + Sync + 'static

Error type.

Required Methods§

Source

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,

Transforms single input to output.

This is the primary method for single execution.

§Arguments
  • input - Input to process.
  • config - Optional execution configuration.
§Returns

Execution result.

Provided Methods§

Source

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.

Source

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}");
}
Source

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.

Source

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".

Implementations on Foreign Types§

Source§

impl Runnable<String, String> for Arc<dyn BaseTool>

Source§

type Error = LcelError

Source§

fn invoke<'life0, 'async_trait>( &'life0 self, input: String, _config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<String, LcelError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<E> Runnable<Vec<Message>, LLMResult> for Box<dyn BaseChatModel<Error = E> + Send + Sync>
where E: Error + Send + Sync + 'static,

Source§

type Error = E

Source§

fn invoke<'life0, 'async_trait>( &'life0 self, input: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn stream<'life0, 'async_trait>( &'life0 self, input: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<LLMResult, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Implementors§

Source§

impl Runnable<HashMap<String, Value>, HashMap<String, Value>> for RunnableAssign

Source§

impl Runnable<LLMResult, HashMap<String, String>> for StructuredOutputParser

Source§

impl Runnable<LLMResult, String> for StrOutputParser

Source§

impl Runnable<LLMResult, Value> for JsonOutputParser

Source§

impl Runnable<LLMResult, Vec<String>> for CommaSeparatedListOutputParser

Source§

impl Runnable<Vec<Message>, LLMResult> for RouterLLM

Source§

impl<I, O: Send + Sync + 'static> Runnable<I, O> for RunnableRetry<I, O>
where I: Clone + Send + Sync + 'static,

Source§

impl<I: Clone + Send + Sync + 'static, O: Send + Sync + 'static> Runnable<I, O> for RunnableBranch<I, O>

Source§

impl<I: Clone + Send + Sync + 'static, O: Send + Sync + 'static> Runnable<I, O> for RunnableWithFallbacks<I, O>

Source§

impl<I: Clone + Send + Sync + 'static> Runnable<I, HashMap<String, Value>> for RunnableParallel<I>

Source§

impl<I: Clone + Send + Sync + 'static> Runnable<I, I> for RunnablePassthrough<I>

Source§

impl<I: Send + Sync + 'static, O: Send + Sync + 'static> Runnable<I, O> for RunnableBinding<I, O>

Source§

impl<I: Send + Sync + 'static, O: Send + Sync + 'static> Runnable<I, O> for RunnableConfigurable<I, O>

Source§

impl<I: Send + Sync + 'static, O: Send + Sync + 'static> Runnable<I, O> for RunnableConfigurableFields<I, O>

Source§

impl<I: Send + Sync + 'static, O: Send + Sync + 'static> Runnable<I, O> for RunnableLambda<I, O>

Source§

impl<I: Send + Sync + 'static, O: Send + Sync + 'static> Runnable<I, O> for RunnableSequence<I, O>

Source§

impl<L> Runnable<Vec<Message>, LLMResult> for TokenTrackingLLM<L>
where L: BaseChatModel + Send + Sync,

Source§

impl<T: DeserializeOwned + Send + Sync + 'static> Runnable<LLMResult, T> for TypedOutputParser<T>