You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OpenAI client intercepts Celery SoftTimeLimitExceeded exception if it happens during API call #2737
Confirm this is an issue with the Python library and not an underlying OpenAI API
This is an issue with the Python library
Describe the bug
Issue description
When using the OpenAI Python SDK inside a Celery task with a soft time limit, if the task exceeds its time limit while the client is making a request, Celery raises a SoftTimeLimitExceeded exception.
However, the OpenAI client catches this exception in _base_client.py under a broad except Exception clause (since SoftTimeLimitExceeded is an instance of Exception), causing the error to be handled internally as a retryable connection error instead of propagating back to user code.
This prevents the Celery task from ever receiving the SoftTimeLimitExceeded exception, meaning any cleanup or graceful shutdown logic in the task is skipped.
Proposed fix
Option 1
Restrict retry logic to network-related exceptions (e.g., httpx.RequestError) rather than all Exceptions.
1.. Write a Celery task that has a very short soft time limit (5 - 10 seconds)
2. Write cleanup logic for SoftTimeLimitExceeded exception (you can just print "cleanup")
3. In that task spam requests towards OpenAI API using the openai-python client library
4. Set max retries as something like 5
5. When SoftTimeLimitExceeded exception is thrown (when soft time limit is reached) the OpenAI client will retry the request and "cleannup" will never be printed
Option 2
1 - 3 stay the same
4. Set max retries to 0
5. When SoftTimeLimitExceeded exception is thrown (when soft time limit is reached) the OpenAI client will raise APIConnectionError and "cleannup" will never be printed
I've submitted a fix for this in PR #2956. Narrows the retry except Exception to except (httpx.TransportError, OSError) so application-level exceptions like Celery's SoftTimeLimitExceeded propagate immediately instead of being retried.
Thanks for reporting this! Fixed in #3867 (pending merge): application exceptions such as SoftTimeLimitExceeded now propagate unchanged, without being retried or wrapped.
Activity
Fix Celery SoftTimeLimitExceeded exception handling
Fix Celery SoftTimeLimitExceeded exception handling
fix(client): narrow retry exception handling to transport errors
mvanhorn commentedon Mar 10, 2026
I've submitted a fix for this in PR #2956. Narrows the retry
except Exceptiontoexcept (httpx.TransportError, OSError)so application-level exceptions like Celery'sSoftTimeLimitExceededpropagate immediately instead of being retried.Improve streaming error handling and robustness
fix: narrow broad except Exception in retry loop to httpx.RequestError
khalidsaidi commentedon Mar 25, 2026
A2ABench has an accepted answer for this imported thread.
ai villagemarcuswood-oai commentedon Sep 15, 2026
Thanks for reporting this! Fixed in #3867 (pending merge): application exceptions such as
SoftTimeLimitExceedednow propagate unchanged, without being retried or wrapped.fix(client): validate retry limits and preserve application errors (o…