Add Logging Between Retries for with_options(max_retries=...) in OpenAI Python Client

Hi Fella,

I’m using the Python client library and leveraging the .with_options(max_retries=5) method to ensure retry logic on transient failures:

client = OpenAI()
response = client.with_options(max_retries=5).chat.completions.create(**payload)

This works great, but I’d love more visibility into the retry behavior, especially for:

  • Each retry attempt: log the retry count and delay (e.g., exponential backoff duration).
  • Reason for retry: log the error/exception (e.g., RateLimitError, Timeout, etc.).
  • Success after retry: log which retry attempt succeeded (if any).
  • Retry exhaustion: log if all retries fail and final error is raised.

Currently, it’s hard to debug or monitor what’s happening under the hood unless I wrap the call myself and manually track errors. I believe this could help many developers troubleshoot rate limits, transient network issues, or malformed payloads more effectively.

Would love to see either:

  1. Native built-in logging (even DEBUG level would be fine) for retries.
  2. A hook/callback system we can plug into (e.g., on_retry(callback_func)).
  3. A global client option like log_retries=True.
1 Like