Expand description
Multi-provider failover for LLM calls.
Wrap an ordered list of (name, callable) provider pairs. Each call tries
them in order. If a provider returns Err, the chain decides whether to fall
back or re-raise, then moves on. You get back a ChainResult with the
return value, the winning provider name, and a trace of every failed attempt.
use llm_fallback_chain::{FallbackChain, DynError};
let chain = FallbackChain::<&str, String>::new(vec![
("anthropic", Box::new(|_p: &&str| -> Result<String, DynError> {
Err("rate limited".into())
}) as _),
("openai", Box::new(|p: &&str| -> Result<String, DynError> {
Ok(format!("o:{}", p))
}) as _),
]).unwrap();
let result = chain.call(&"hi").unwrap();
assert_eq!(result.value, "o:hi");
assert_eq!(result.provider, "openai");
assert_eq!(result.attempts.len(), 1);Pluggable predicate to whitelist only certain errors:
use llm_fallback_chain::{FallbackChain, DynError};
let chain = FallbackChain::<(), i32>::new(vec![
("a", Box::new(|_: &()| -> Result<i32, DynError> {
Err("validation error".into())
}) as _),
("b", Box::new(|_: &()| -> Result<i32, DynError> { Ok(1) }) as _),
])
.unwrap()
.with_should_fall_back(|err| err.to_string().contains("rate"));
// validation error is not "rate", so we do not fall back; the chain re-raises.
assert!(chain.call(&()).is_err());Structs§
- AllProviders
Failed - Raised when every provider in the chain failed.
- Attempt
- One provider attempt within a chain call.
- Chain
Result - Outcome of a successful
FallbackChain::call. - Fallback
Chain - Ordered list of LLM providers to try in sequence.
Type Aliases§
- DynError
- Boxed dynamic error used throughout the chain. Providers return this so every provider in the chain can fail in its own way.
- OnFallback
- Audit callback fired after a provider fails, before the next is tried.
(failed_name, error, next_name). - Should
Fall Back - Predicate deciding whether a given error should cause fallback. Default: any error causes fallback.
- Sync
Provider - Sync provider callable:
(input) -> Result<O, DynError>.