RunnableWithFallbacks

Struct RunnableWithFallbacks 

Source
pub struct RunnableWithFallbacks<I, O>
where I: Send + Sync + Clone + Debug + 'static, O: Send + Sync + Clone + Debug + 'static,
{ 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: bool

Whether 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>
where I: Send + Sync + Clone + Debug + 'static, O: Send + Sync + Clone + Debug + 'static,

Source

pub fn new<R>(runnable: R, fallbacks: Vec<DynRunnable<I, O>>) -> Self
where R: Runnable<Input = I, Output = O> + Send + Sync + 'static,

Create a new RunnableWithFallbacks.

§Arguments
  • runnable - The primary runnable to try first
  • fallbacks - A list of fallback runnables to try if the primary fails
Source

pub fn from_dyn( runnable: DynRunnable<I, O>, fallbacks: Vec<DynRunnable<I, O>>, ) -> Self

Create a new RunnableWithFallbacks from a DynRunnable.

Source

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

Source

pub fn with_name(self, name: impl Into<String>) -> Self

Set the name of this runnable.

Source

pub fn runnables(&self) -> impl Iterator<Item = &DynRunnable<I, O>>

Get an iterator over all runnables (primary + fallbacks).

Source

pub fn config_specs(&self) -> Result<Vec<ConfigurableFieldSpec>>

Get the config specs from all runnables.

Trait Implementations§

Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Source§

type Input = I

The input type for this Runnable.
Source§

type Output = O

The output type for this Runnable.
Source§

fn name(&self) -> Option<String>

Get the optional name of this Runnable.
Source§

fn invoke( &self, input: Self::Input, config: Option<RunnableConfig>, ) -> Result<Self::Output>

Transform a single input into an output. Read more
Source§

fn ainvoke<'life0, 'async_trait>( &'life0 self, input: Self::Input, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Self::Output>> + Send + 'async_trait>>
where Self: 'static + 'async_trait, 'life0: 'async_trait,

Transform a single input into an output asynchronously. Read more
Source§

fn batch( &self, inputs: Vec<Self::Input>, config: Option<ConfigOrList>, return_exceptions: bool, ) -> Vec<Result<Self::Output>>
where Self: 'static,

Transform multiple inputs into outputs in parallel. Read more
Source§

fn abatch<'life0, 'async_trait>( &'life0 self, inputs: Vec<Self::Input>, config: Option<ConfigOrList>, return_exceptions: bool, ) -> Pin<Box<dyn Future<Output = Vec<Result<Self::Output>>> + Send + 'async_trait>>
where Self: 'static + 'async_trait, 'life0: 'async_trait,

Transform multiple inputs into outputs asynchronously. Read more
Source§

fn stream( &self, input: Self::Input, config: Option<RunnableConfig>, ) -> BoxStream<'_, Result<Self::Output>>

Stream output from a single input. Read more
Source§

fn astream( &self, input: Self::Input, config: Option<RunnableConfig>, ) -> BoxStream<'_, Result<Self::Output>>
where Self: 'static,

Stream output from a single input asynchronously. Read more
Source§

fn get_name(&self, suffix: Option<&str>, name: Option<&str>) -> String

Get the name of this Runnable.
Source§

fn type_name(&self) -> &'static str

Get the type name of this Runnable.
Source§

fn transform<'a>( &'a self, input: BoxStream<'a, Self::Input>, config: Option<RunnableConfig>, ) -> BoxStream<'a, Result<Self::Output>>

Transform an input stream into an output stream. Read more
Source§

fn atransform<'a>( &'a self, input: BoxStream<'a, Self::Input>, config: Option<RunnableConfig>, ) -> BoxStream<'a, Result<Self::Output>>
where Self: 'static,

Transform an input stream into an output stream asynchronously. Read more
Source§

fn bind(self, kwargs: HashMap<String, Value>) -> RunnableBinding<Self>
where Self: Sized,

Bind arguments to this Runnable, returning a new Runnable.
Source§

fn with_config(self, config: RunnableConfig) -> RunnableBinding<Self>
where Self: Sized,

Bind config to this Runnable, returning a new Runnable.
Source§

fn with_retry( self, max_attempts: usize, wait_exponential_jitter: bool, ) -> RunnableRetry<Self>
where Self: Sized,

Create a new Runnable that retries on failure. Read more
Source§

fn map(self) -> RunnableEach<Self>
where Self: Sized,

Return a new Runnable that maps a list of inputs to a list of outputs.

Auto Trait Implementations§

§

impl<I, O> Freeze for RunnableWithFallbacks<I, O>

§

impl<I, O> !RefUnwindSafe for RunnableWithFallbacks<I, O>

§

impl<I, O> Send for RunnableWithFallbacks<I, O>

§

impl<I, O> Sync for RunnableWithFallbacks<I, O>

§

impl<I, O> Unpin for RunnableWithFallbacks<I, O>

§

impl<I, O> !UnwindSafe for RunnableWithFallbacks<I, O>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<R> ConfigurableRunnable for R
where R: Runnable,

Source§

fn configurable_fields( self, fields: HashMap<String, AnyConfigurableField>, ) -> RunnableConfigurableFields<Self::Input, Self::Output>
where Self: Send + Sync + 'static,

Create a configurable version of this runnable that allows configuring specific fields. Read more
Source§

fn configurable_alternatives( self, which: ConfigurableField, alternatives: HashMap<String, Alternative<Self::Input, Self::Output>>, default_key: impl Into<String>, prefix_keys: bool, ) -> RunnableConfigurableAlternatives<Self::Input, Self::Output>
where Self: Send + Sync + 'static,

Create a configurable version of this runnable that allows selecting between alternatives. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<R> RunnableRetryExt for R
where R: Runnable,

Source§

fn with_retry_config(self, config: RunnableRetryConfig) -> RunnableRetry<Self>
where Self: Sized,

Create a new Runnable that retries this runnable on failure with full config. Read more
Source§

impl<R> RunnableWithFallbacksExt for R
where R: Runnable,

Source§

fn with_fallbacks( self, fallbacks: Vec<DynRunnable<Self::Input, Self::Output>>, ) -> RunnableWithFallbacks<Self::Input, Self::Output>
where Self: Sized + Send + Sync + 'static,

Create a new Runnable that tries this runnable first, then falls back to others. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more