AiFail
Python Library for retrying openai/gcp API calls
Installation
pip install aifail
Usage
To get started, simply wrap your functions with @retry_if, specifying the condition to retry on.
Custom logic
You can use this with anything that needs retrying, e.g. google sheets -
def sheets_api_should_retry(e: Exception) -> bool:
return isinstance(e, HttpError) and (
e.resp.status in (408, 429) or e.resp.status > 500
)
@retry_if(sheets_api_should_retry)
def update_cell(spreadsheet_id: str, row: int, col: int, value: str):
get_spreadsheet_service().values().update(
spreadsheetId=spreadsheet_id,
range=f"{col_i2a(col)}{row}:{col_i2a(col)}{row}",
body={"values": [[value]]},
valueInputOption="RAW",
).execute()
Advanced Usage
This library is used by GooeyAI in production to handle thousands of API calls every day. To save costs and handle rate limits, you can intelligently specify quick fallbacks (eg azure openai) -
Traceable Errors
AiFail comes with a built in logger, and outputs complete stack traces tracing the error back to the original call site.
# python examples/azure_openai_fallback.py
2023-11-18 04:36:01.364 | WARNING | aifail.aifail:try_all:63 - [2/2] tyring next fn, prev_exc=NotFoundError("Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}")
2023-11-18 04:36:01.778 | WARNING | aifail.aifail:wrapper:98 - [1/1] captured error, retry_delay=0.4117675457681431s, exc=NotFoundError("Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}")
2023-11-18 04:36:02.483 | WARNING | aifail.aifail:try_all:63 - [2/2] tyring next fn, prev_exc=NotFoundError("Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}")
2023-11-18 04:36:04.093 | WARNING | aifail.aifail:wrapper:98 - [2/1] captured error, retry_delay=0.9974197744911488s, exc=NotFoundError("Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}")
Traceback (most recent call last):
File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 65, in try_all
return fn()
File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 28, in <lambda>
lambda: azure_client.chat.completions.create(
...
openai.NotFoundError: Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 86, in wrapper
return fn(*args, **kwargs)
File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 26, in chad_gpt4
response = try_all(
File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 69, in try_all
raise prev_exc
File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 65, in try_all
return fn()
File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 34, in <lambda>
lambda: openai_client.chat.completions.create(
...
openai.NotFoundError: Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 65, in try_all
return fn()
File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 28, in <lambda>
lambda: azure_client.chat.completions.create(
...
openai.NotFoundError: Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 43, in <module>
chad_gpt4(
File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 102, in wrapper
raise prev_exc
File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 86, in wrapper
return fn(*args, **kwargs)
File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 26, in chad_gpt4
response = try_all(
File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 69, in try_all
raise prev_exc
File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 65, in try_all
return fn()
File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 34, in <lambda>
lambda: openai_client.chat.completions.create(
...
openai.NotFoundError: Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}
Process finished with exit code 1
Sentry too, will capture the entire retry loop
Metadata
Release files for aifail 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| aifail-0.3.1.tar.gz | 8.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aifail-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.8 kB
Release files / aifail-0.3.1.tar.gz
| Download URL | aifail-0.3.1.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
24f51f193315efd35d867971651e2152a939da0b1a42cb22091dbb3d1ef3d262
|
|
BLAKE2b-256 checksum How to use checksums |
ef304532b7c9760c992dadfccc555b9549f5c5505d623bf733ff6969ebd80a6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.2.1 CPython/3.10.5 Darwin/24.6.0
|
Release files / aifail-0.3.1-py3-none-any.whl
| Download URL | aifail-0.3.1-py3-none-any.whl |
|---|---|
| Size | 9.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7e2ab9569c2ca17546cc4634797cba484469d8ee0ccd6a7bf2e54458ea2ebe38
|
|
BLAKE2b-256 checksum How to use checksums |
57420beba3ff5343b278ac059013b3aa4968d513e78173cb92668988cb64907b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.2.1 CPython/3.10.5 Darwin/24.6.0
|
Security for aifail 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.