pub struct RunnableWithFallbacks<I, O>{
pub runnable: DynRunnable<I, O>,
pub fallbacks: Vec<DynRunnable<I, O>>,
pub handle_all_errors: bool,
/* private fields */
}Expand description
A Runnable that can fallback to other Runnables if it fails.
External APIs (e.g., APIs for a language model) may at times experience degraded performance or even downtime.
In these cases, it can be useful to have a fallback Runnable that can be
used in place of the original Runnable (e.g., fallback to another LLM provider).
Fallbacks can be defined at the level of a single Runnable, or at the level
of a chain of Runnables. Fallbacks are tried in order until one succeeds or
all fail.
While you can instantiate a RunnableWithFallbacks directly, it is usually
more convenient to use the with_fallbacks method on a Runnable.
§Example
use agent_chain_core::runnables::{RunnableLambda, RunnableWithFallbacks};
// Create a primary runnable that might fail
let primary = RunnableLambda::new(|x: i32| {
if x > 5 { Err(Error::other("too large")) }
else { Ok(x * 2) }
});
// Create a fallback runnable
let fallback = RunnableLambda::new(|x: i32| Ok(x));
// Combine them with fallbacks
let with_fallbacks = RunnableWithFallbacks::new(primary, vec![fallback]);
// Will use primary for x <= 5, fallback for x > 5
assert_eq!(with_fallbacks.invoke(3, None).unwrap(), 6);
assert_eq!(with_fallbacks.invoke(10, None).unwrap(), 10);Fields§
§runnable: DynRunnable<I, O>The Runnable to run first.
fallbacks: Vec<DynRunnable<I, O>>A sequence of fallbacks to try.
handle_all_errors: boolWhether to handle all errors (true) or specific error types.
In Rust, we simplify the Python’s exceptions_to_handle to a boolean flag
since we don’t have the same exception hierarchy.
Implementations§
Source§impl<I, O> RunnableWithFallbacks<I, O>
impl<I, O> RunnableWithFallbacks<I, O>
Sourcepub fn new<R>(runnable: R, fallbacks: Vec<DynRunnable<I, O>>) -> Self
pub fn new<R>(runnable: R, fallbacks: Vec<DynRunnable<I, O>>) -> Self
Create a new RunnableWithFallbacks.
§Arguments
runnable- The primary runnable to try firstfallbacks- A list of fallback runnables to try if the primary fails
Sourcepub fn from_dyn(
runnable: DynRunnable<I, O>,
fallbacks: Vec<DynRunnable<I, O>>,
) -> Self
pub fn from_dyn( runnable: DynRunnable<I, O>, fallbacks: Vec<DynRunnable<I, O>>, ) -> Self
Create a new RunnableWithFallbacks from a DynRunnable.
Sourcepub fn with_handle_all_errors(self, handle_all_errors: bool) -> Self
pub fn with_handle_all_errors(self, handle_all_errors: bool) -> Self
Set whether to handle all errors.
If true, any error will trigger fallback. If false, only certain errors will trigger fallback (default: true).
Sourcepub fn runnables(&self) -> impl Iterator<Item = &DynRunnable<I, O>>
pub fn runnables(&self) -> impl Iterator<Item = &DynRunnable<I, O>>
Get an iterator over all runnables (primary + fallbacks).
Sourcepub fn config_specs(&self) -> Result<Vec<ConfigurableFieldSpec>>
pub fn config_specs(&self) -> Result<Vec<ConfigurableFieldSpec>>
Get the config specs from all runnables.