pub trait RunnableExt<Input: Send + Sync + 'static, Output: Send + Sync + 'static>: Runnable<Input, Output>{
// 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 { ... }
fn configurable_alternatives<K, R>(
self,
which: impl Into<String>,
default_key: impl Into<String>,
alternatives: Vec<(K, R)>,
) -> RunnableConfigurable<Input, Output>
where K: Into<String>,
R: Runnable<Input, Output> + Send + Sync + 'static,
R::Error: Into<LcelError> { ... }
fn configurable_fields(self) -> RunnableConfigurableFields<Input, Output> { ... }
}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§
Sourcefn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2>
fn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2>
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?;Sourcefn into_sequence(self) -> RunnableSequence<Input, Output>
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().
Sourcefn with_fallbacks<R>(
self,
fallbacks: Vec<R>,
) -> RunnableWithFallbacks<Input, Output>
fn with_fallbacks<R>( self, fallbacks: Vec<R>, ) -> RunnableWithFallbacks<Input, Output>
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);Sourcefn with_retry(self, retry_config: RetryConfig) -> RunnableRetry<Input, Output>where
Input: Clone,
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()
});Sourcefn configurable_alternatives<K, R>(
self,
which: impl Into<String>,
default_key: impl Into<String>,
alternatives: Vec<(K, R)>,
) -> RunnableConfigurable<Input, Output>
fn configurable_alternatives<K, R>( self, which: impl Into<String>, default_key: impl Into<String>, alternatives: Vec<(K, R)>, ) -> RunnableConfigurable<Input, Output>
Route between a default runnable and named alternatives at invoke time.
Rust counterpart of Python LCEL’s Runnable.configurable_alternatives.
The selector key which is read from config.configurable; the value
must name the default_key (→ this runnable) or one of the
alternatives. An unknown value falls back to this runnable.
§Example
let chain = llm.configurable_alternatives(
"provider", "default",
vec![("anthropic", anthropic_llm)],
);
// invoke(msgs, Some(RunnableConfig::new().with_configurable("provider", json!("anthropic"))))Sourcefn configurable_fields(self) -> RunnableConfigurableFields<Input, Output>
fn configurable_fields(self) -> RunnableConfigurableFields<Input, Output>
Override recognized config fields at invoke time from
config.configurable (Python’s Runnable.configurable_fields).
temperature / max_tokens in the configurable map are promoted to
the typed config fields the providers consume; other keys are merged
into config.metadata.
§Example
let llm = llm.configurable_fields();
let config = RunnableConfig::new().with_configurable("temperature", json!(0.5));
llm.invoke(msgs, Some(config)).await?; // 本次调用采样温度 0.5Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".