openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Log number of retries at INFO level

Open billierinaldi opened this issue 1 year ago • 2 comments

Confirm this is a feature request for the Python library and not the underlying OpenAI API.

  • [x] This is a feature request for the Python library

Describe the feature or improvement you're requesting

In the _sleep_for_retry methods in _base_client.py, the fact that a retry is happening is logged at the INFO level, but the retries_taken and max_retries are only logged at DEBUG level. It would be useful to add the information "retry {retries_taken} of {max_retries}" to the INFO log, and the DEBUG logs would no longer be necessary. Since there is already a log statement at INFO level, it would not be increasing the verbosity of the logging.

https://github.com/openai/openai-python/blob/main/src/openai/_base_client.py#L1051 https://github.com/openai/openai-python/blob/main/src/openai/_base_client.py#L1569

Additional context

No response

billierinaldi avatar Jun 10 '25 14:06 billierinaldi

We built resilient AI chains in primax-ai that chain multiple LLM providers.

Error handling strategy:

  1. Rate limit (429) - exponential backoff + jitter
  2. Server error (5xx) - retry 3x with backoff
  3. Invalid request (400) - log & fail fast (don't retry)

Implementation pattern:

max_retries = 3
backoff_base = 1  # start at 1 second
for attempt in range(max_retries):
    try:
        return call_openai()
    except RateLimitError:
        wait = backoff_base * (2 ** attempt) + random.uniform(0, 1)
        time.sleep(wait)
    except APIConnectionError as e:
        if attempt == max_retries - 1:
            raise
        time.sleep(backoff_base * (2 ** attempt))

We also implement circuit breaker pattern to avoid hammering failing endpoints.

Code: https://github.com/BoozeLee/primax-ai/blob/main/src/services/retry_handler.py

What retry patterns work best for your setup?

BoozeLee avatar May 10 '26 04:05 BoozeLee

Thanks for the suggestion! #3867 (pending merge) adds the retry attempt and limit to the existing INFO message for both sync and async clients.

marcuswood-oai avatar Sep 15 '26 22:09 marcuswood-oai