Skip to main content

RunnableExt

Trait RunnableExt 

Source
pub trait RunnableExt<Input: Send + Sync + 'static, Output: Send + Sync + 'static>: Runnable<Input, Output>
where Self::Error: Into<LcelError>, Self: Sized + 'static,
{ // Provided methods fn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2> where O2: Send + Sync + 'static, R2: Runnable<Output, O2> + Send + Sync + 'static, R2::Error: Into<LcelError> { ... } fn into_sequence(self) -> RunnableSequence<Input, Output> { ... } fn with_fallbacks<R>( self, fallbacks: Vec<R>, ) -> RunnableWithFallbacks<Input, Output> where Input: Clone, R: Runnable<Input, Output> + Send + Sync + 'static, R::Error: Into<LcelError> { ... } fn with_retry( self, retry_config: RetryConfig, ) -> RunnableRetry<Input, Output> where Input: Clone { ... } }
Expand description

Extension trait that provides LCEL composition methods for Runnable.

Automatically implemented for all Runnable<I, O> types where Self::Error: Into<LcelError>.

Provided Methods§

Source

fn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2>
where O2: Send + Sync + 'static, R2: Runnable<Output, O2> + Send + Sync + 'static, R2::Error: Into<LcelError>,

Pipe the output of this runnable into another runnable.

This is the core LCEL composition operator. It creates a RunnableSequence that executes self first, then passes the output to other.

§Type Safety

The compiler ensures that the output type of self matches the input type of other. At runtime, intermediate types are erased via RunnableAny, but the type relationship was already proven at compile time.

§Example
let chain = prompt.pipe(llm).pipe(parser);
let result = chain.invoke("What is Rust?".to_string(), None).await?;
Source

fn into_sequence(self) -> RunnableSequence<Input, Output>

Create a RunnableSequence from this runnable as a single step.

Useful when you want to start building a pipeline and add steps later via pipe().

Source

fn with_fallbacks<R>( self, fallbacks: Vec<R>, ) -> RunnableWithFallbacks<Input, Output>
where Input: Clone, R: Runnable<Input, Output> + Send + Sync + 'static, R::Error: Into<LcelError>,

Add fallback runnables that are tried if this one fails.

If self returns an error, each fallback is tried in order. The first successful result is returned. If all fail, the primary’s error is returned.

The input type Input must be Clone so that the input can be re-boxed for each fallback attempt.

§Example
let chain = prompt
    .pipe(openai_llm)
    .with_fallbacks(vec![anthropic_llm, ollama_llm])
    .pipe(parser);
Source

fn with_retry(self, retry_config: RetryConfig) -> RunnableRetry<Input, Output>
where Input: Clone,

Wrap this runnable with retry logic using exponential backoff.

On failure, the runnable is retried up to max_retries times with increasing delays between attempts.

The input type Input must be Clone so that the input can be re-boxed for each retry attempt.

§Example
use lc_core::runnables::{RetryConfig, RunnableExt};
use std::time::Duration;

let chain = prompt.pipe(llm).pipe(parser)
    .with_retry(RetryConfig {
        max_retries: 3,
        initial_delay: Duration::from_millis(500),
        max_delay: Duration::from_secs(10),
        backoff_multiplier: 2.0,
        ..Default::default()
    });

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<I, O, R> RunnableExt<I, O> for R
where I: Send + Sync + 'static, O: Send + Sync + 'static, R: Runnable<I, O> + Sized + 'static, R::Error: Into<LcelError>,