Skip to content
  • Auto
  • Light
  • Dark

Handling errors

When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of do_gradientai.APIConnectionError is raised.

When the API returns a non-success status code (that is, 4xx or 5xx response), a subclass of do_gradientai.APIStatusError is raised, containing status_code and response properties.

All errors inherit from do_gradientai.APIError.

Python
import do_gradientai
from do_gradientai import GradientAI
client = GradientAI()
try:
client.chat.completions.create(
messages=[
{
"role": "user",
"content": "What is the capital of France?",
}
],
model="llama3.3-70b-instruct",
)
except do_gradientai.APIConnectionError as e:
print("The server could not be reached")
print(e.__cause__) # an underlying Exception, likely raised within httpx.
except do_gradientai.RateLimitError as e:
print("A 429 status code was received; we should back off a bit.")
except do_gradientai.APIStatusError as e:
print("Another non-200-range status code was received")
print(e.status_code)
print(e.response)

Error codes are as follows:

Status CodeError Type
400BadRequestError
401AuthenticationError
403PermissionDeniedError
404NotFoundError
422UnprocessableEntityError
429RateLimitError
>=500InternalServerError
N/AAPIConnectionError

Certain errors are automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors are all retried by default.

You can use the max_retries option to configure or disable retry settings:

Python
from do_gradientai import GradientAI
# Configure the default for all requests:
client = GradientAI(
# default is 2
max_retries=0,
)
# Or, configure per-request:
client.with_options(max_retries=5).chat.completions.create(
messages=[
{
"role": "user",
"content": "What is the capital of France?",
}
],
model="llama3.3-70b-instruct",
)

By default requests time out after 1 minute. You can configure this with a timeout option, which accepts a float or an httpx.Timeout object:

Python
from do_gradientai import GradientAI
# Configure the default for all requests:
client = GradientAI(
# 20 seconds (default is 1 minute)
timeout=20.0,
)
# More granular control:
client = GradientAI(
timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0),
)
# Override per-request:
client.with_options(timeout=5.0).chat.completions.create(
messages=[
{
"role": "user",
"content": "What is the capital of France?",
}
],
model="llama3.3-70b-instruct",
)

On timeout, an APITimeoutError is thrown.

Note that requests that time out are retried twice by default.