Skip to content

[Question] How to track/log the current retry attempt when using request retries? #242

@leoplct

Description

@leoplct

Hi there, thanks for the great gem!

I am using openai (v0.42.0) with Rails 8. I have configured the client to handle retries automatically, but I am struggling to find a way to "observe" the retry process in real-time.

The Context I am running a background job that extracts data from invoices. I want to be able to log (or update a database record) every time a retry occurs (e.g., "Attempt 2/5 failed, retrying..."), specifically when hitting 429 Rate Limits.

Currently, the .chat or .completions call is blocking, and I only get the result after all retries are exhausted or successful. I have no visibility into how many attempts were made during the execution.

client = OpenAI::Client.new(
  access_token: "...",
  request: { timeout: 20 } 
  # I understand retries are handled via Faraday middleware under the hood
)

# How can I hook into the retry mechanism here?
@response ||= $openai.responses.create(
      model: MODEL,
      service_tier: service_tier,
      store: false,
      instructions: "Extract details from the provided document.",
      reasoning: { effort: "medium" },
      input: [
        { "role": "user", "content": inputs }
      ],
      text: {
        format: {
          "type": "json_schema",
          "name": "extraction",
          "schema": Openai::Schema.schema(@input)
        }
      }
    )

The Question Is there a supported way to pass an on_retry callback or a block to the underlying Faraday connection through the OpenAI::Client?

I would like to achieve something similar to Faraday's retry block:

retry_options = {
  max: 5,
  retry_block: -> (env, options, retries, exception) { 
    puts "Retrying... Attempt #{retries}" 
    # MyCustomLogger.log(retries)
  }
}

Is there a configuration option I missed to expose this?

Thanks for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions