pub struct FallbackChain { /* private fields */ }Expand description
Fault-recovery primitives for resilient agent execution. An ordered chain of models for sequential fallback on failure.
When a model request fails, the chain allows easy iteration to the next available model in priority order. This is useful for implementing automatic failover strategies.
§Example
use oxi_ai::fallback_chain::FallbackChain;
// From string IDs
let chain = FallbackChain::from_ids(&["openai/gpt-4o", "google/gemini-2.0-flash"])?;
// Direct construction
let models = vec![model1, model2];
let chain = FallbackChain::new(models);
// Find next model after failure
if let Some(next) = chain.next("openai/gpt-4o") {
// Use next model...
}Implementations§
Source§impl FallbackChain
impl FallbackChain
Sourcepub fn new(models: Vec<&'static ModelEntry>) -> FallbackChain
pub fn new(models: Vec<&'static ModelEntry>) -> FallbackChain
Creates a new fallback chain from an ordered list of models.
§Arguments
models- A vector of model entries in priority order (first = highest priority)
§Example
use oxi_ai::model_db::get_model_entry;
let models = vec![
get_model_entry("openai", "gpt-4o").unwrap(),
get_model_entry("anthropic", "claude-sonnet-4-20250514").unwrap(),
];
let chain = FallbackChain::new(models);Sourcepub fn from_ids(ids: &[&str]) -> Result<FallbackChain, FallbackChainError>
pub fn from_ids(ids: &[&str]) -> Result<FallbackChain, FallbackChainError>
Creates a fallback chain from “provider/model” ID strings.
Each string must be in the format "provider/model-id", for example:
"anthropic/claude-sonnet-4-20250514""openai/gpt-4o""google/gemini-2.0-flash"
§Arguments
ids- Slice of strings in"provider/model"format
§Errors
Returns a FallbackChainError if any model ID cannot be found in the database.
§Example
let chain = FallbackChain::from_ids(&[
"anthropic/claude-sonnet-4-20250514",
"openai/gpt-4o",
])?;Sourcepub fn next(&self, current: &str) -> Option<&'static ModelEntry>
pub fn next(&self, current: &str) -> Option<&'static ModelEntry>
Returns the next model in the chain after the current one.
§Arguments
current- The current model ID in"provider/model"format
§Returns
Some(&ModelEntry)- The next model in the chainNone- If the current model is not in the chain, or it’s the last model
§Example
let chain = FallbackChain::from_ids(&["a", "b", "c"])?;
assert_eq!(chain.next("a").map(|m| m.id), Some("b"));
assert_eq!(chain.next("b").map(|m| m.id), Some("c"));
assert_eq!(chain.next("c"), None); // Last in chain
assert_eq!(chain.next("unknown"), None); // Not in chainSourcepub fn index_of(&self, model_id: &str) -> Option<usize>
pub fn index_of(&self, model_id: &str) -> Option<usize>
Returns the index of a model in the chain.
§Arguments
model_id- The model ID in"provider/model"format
§Returns
Some(usize)- The zero-based position in the chainNone- If the model is not in the chain
§Example
let chain = FallbackChain::from_ids(&["a", "b", "c"])?;
assert_eq!(chain.index_of("a"), Some(0));
assert_eq!(chain.index_of("b"), Some(1));
assert_eq!(chain.index_of("c"), Some(2));
assert_eq!(chain.index_of("unknown"), None);Sourcepub fn iter(&self) -> impl Iterator<Item = &'static ModelEntry>
pub fn iter(&self) -> impl Iterator<Item = &'static ModelEntry>
Sourcepub fn models(&self) -> &[&'static ModelEntry]
pub fn models(&self) -> &[&'static ModelEntry]
Sourcepub fn first(&self) -> Option<&'static ModelEntry>
pub fn first(&self) -> Option<&'static ModelEntry>
Sourcepub fn last(&self) -> Option<&'static ModelEntry>
pub fn last(&self) -> Option<&'static ModelEntry>
Sourcepub fn from_inclusive(&self, model_id: &str) -> Option<FallbackChain>
pub fn from_inclusive(&self, model_id: &str) -> Option<FallbackChain>
Creates a new chain with models after (and including) the given model.
This is useful for continuing fallback after a model succeeds but you want to track the remaining options.
§Arguments
model_id- The model ID to start from (inclusive)
§Returns
Some(FallbackChain)- The remaining models from the starting pointNone- If the model is not in the chain
§Example
let chain = FallbackChain::from_ids(&["a", "b", "c"])?;
let remaining = chain.from_inclusive("b")?;
assert_eq!(remaining.names(), &["b", "c"]);Sourcepub fn from_after(&self, model_id: &str) -> Option<FallbackChain>
pub fn from_after(&self, model_id: &str) -> Option<FallbackChain>
Creates a new chain with models after (excluding) the given model.
§Arguments
model_id- The model ID to skip
§Returns
Some(FallbackChain)- The remaining models after the given modelNone- If the model is not in the chain or is the last model
§Example
let chain = FallbackChain::from_ids(&["a", "b", "c"])?;
let remaining = chain.from_after("b")?;
assert_eq!(remaining.names(), &["c"]);Trait Implementations§
Source§impl Clone for FallbackChain
impl Clone for FallbackChain
Source§fn clone(&self) -> FallbackChain
fn clone(&self) -> FallbackChain
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for FallbackChain
impl Debug for FallbackChain
Source§impl Default for FallbackChain
impl Default for FallbackChain
Source§fn default() -> FallbackChain
fn default() -> FallbackChain
Creates a default fallback chain with cheap, reliable models.
The default chain includes models from multiple providers to ensure redundancy and cost efficiency. These are selected based on:
- Low input cost
- Wide context window
- Vision support for versatility
Source§impl PartialEq for FallbackChain
impl PartialEq for FallbackChain
Source§fn eq(&self, other: &FallbackChain) -> bool
fn eq(&self, other: &FallbackChain) -> bool
self and other values to be equal, and is used by ==.impl StructuralPartialEq for FallbackChain
Auto Trait Implementations§
impl Freeze for FallbackChain
impl RefUnwindSafe for FallbackChain
impl Send for FallbackChain
impl Sync for FallbackChain
impl Unpin for FallbackChain
impl UnsafeUnpin for FallbackChain
impl UnwindSafe for FallbackChain
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreimpl<T> MaybeSendSync for T
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.