Class: RubyLLM::Contract::Step::RetryPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/contract/step/retry_policy.rb

Constant Summary collapse

DEFAULT_RETRY_ON =

Breaking (0.7.0): :adapter_error removed from defaults. ruby_llm’s Faraday middleware already retries transport errors (RateLimitError, ServerError, ServiceUnavailableError, OverloadedError, timeouts). Retrying on :adapter_error against the same model re-runs what transport already did. Opt in explicitly with ‘retry_on :adapter_error` — only meaningful paired with `escalate “model_a”, “model_b”` (a different model may bypass what transport retry could not).

%i[validation_failed parse_error].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models: nil, attempts: nil, retry_on: nil, &block) ⇒ RetryPolicy

Returns a new instance of RetryPolicy.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 18

def initialize(models: nil, attempts: nil, retry_on: nil, &block)
  @configs = []
  @retryable_statuses = DEFAULT_RETRY_ON.dup

  if block
    @max_attempts = 1
    instance_eval(&block)
    warn_no_retry! if @max_attempts == 1 && @configs.empty?
  else
    apply_keywords(models: models, attempts: attempts, retry_on: retry_on)
  end

  validate_max_attempts!
end

Instance Attribute Details

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



7
8
9
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 7

def max_attempts
  @max_attempts
end

#retryable_statusesObject (readonly)

Returns the value of attribute retryable_statuses.



7
8
9
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 7

def retryable_statuses
  @retryable_statuses
end

Instance Method Details

#attempts(count) ⇒ Object



33
34
35
36
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 33

def attempts(count)
  @max_attempts = count
  validate_max_attempts!
end

#config_for_attempt(attempt, default_config) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 60

def config_for_attempt(attempt, default_config)
  if @configs.any?
    @configs[attempt] || @configs.last
  else
    default_config
  end
end

#config_listObject



48
49
50
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 48

def config_list
  @configs
end

#escalate(*config_list) ⇒ Object Also known as: models



38
39
40
41
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 38

def escalate(*config_list)
  @configs = config_list.flatten.map { |c| normalize_config(c).freeze }.freeze
  @max_attempts = @configs.length if @max_attempts < @configs.length
end

#model_for_attempt(attempt, default_model) ⇒ Object



68
69
70
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 68

def model_for_attempt(attempt, default_model)
  config_for_attempt(attempt, { model: default_model })[:model]
end

#model_listObject



44
45
46
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 44

def model_list
  @configs.map { |c| c[:model] }.freeze
end

#retry_on(*statuses) ⇒ Object



52
53
54
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 52

def retry_on(*statuses)
  @retryable_statuses = statuses.flatten
end

#retryable?(result) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 56

def retryable?(result)
  retryable_statuses.include?(result.status)
end