class RubyLLM::Fallback
A Fallback is a fallback model target configured with Chat#with_fallbacks. When the active model fails with a matching error, the chat retries the request with each fallback in order. Instances are yielded to Chat#before_fallback and Chat#after_fallback callbacks, enriched with the details of that attempt. With drop_unsupported: true, a retry of the same model without a parameter it rejected is an attempt too: its from and to are that model, and dropped_parameter names what it left out.
chat = RubyLLM.chat(model: "gpt-4.1") .with_fallbacks("gpt-4.1-mini", "claude-haiku-4-5") chat.before_fallback do |fallback| puts "Falling back from #{fallback.from.id} to #{fallback.to.id}" end chat.after_fallback do |fallback| puts "Fallback #{fallback.succeeded? ? 'succeeded' : 'failed'}" end
Constants
- DEFAULT_ERRORS
-
The error classes that trigger a fallback when
Chat#with_fallbacksis called withouton:.
Attributes
The fallback attempt number, starting at 1.
The parameter a retry on the same model left out, as UnsupportedParameterError#parameter names it, or nil for a fallback to another model. See Chat#with_fallbacks.
The error that triggered the fallback.
The error raised by the fallback attempt itself, or nil if it succeeded.
The model id of the fallback target, or of the model a retry without a rejected parameter runs on.
The Model object the fallback was configured with, or nil when it was configured with a model id.
The provider slug of the fallback target as a String, or nil.
The response Message from a successful fallback attempt, or nil.
Public Instance Methods
Source
# File lib/ruby_llm/fallback.rb, line 145 def chunks_yielded? @chunks_yielded end
Returns whether the failed model already yielded stream chunks before the fallback.
Source
# File lib/ruby_llm/fallback.rb, line 151 def failed? !fallback_error.nil? end
Returns true if the fallback attempt raised an error, false otherwise.
Source
# File lib/ruby_llm/fallback.rb, line 139 def streaming? streaming end
Returns whether the fallback happened during a streaming request.
Source
# File lib/ruby_llm/fallback.rb, line 157 def succeeded? !response.nil? && !failed? end
Returns true if the fallback attempt produced a response without error, false otherwise.