Skip to main content

Error Handling

Use this page to identify common SDK and API errors, understand what they mean, and decide how your application should handle them.

Error Codes

Code or errorSDK exceptionWhat it usually meansWhat to check
401AuthenticationErrorThe API key is missing, invalid, expired, or not available to the running process.Check CUSTODIAN_SDK_API_KEY, rotate the key if needed, and restart your application.
404NotFoundErrorThe assistant, app, or resource does not exist or is not accessible with the current key.Check the assistant or app ID and confirm the key belongs to the correct account or environment.
422ValidationErrorThe request payload is invalid.Check required fields such as model, prompt, examples, file path, or message content.
5xxServerErrorThe API returned a server-side error after retries.Retry later or check service status.
Network errorAPIConnectionErrorThe SDK could not reach the API.Check network access, firewall settings, and CUSTODIAN_SDK_BASE_URL.
Other 4xxAPIResponseErrorThe API returned an unclassified client-side error.Inspect the error payload for details.

Error Details

Every SDK error may include:

  • message
  • status_code
  • endpoint
  • payload

These fields are useful for logging and debugging:

except AISDKError as error:
print(error.status_code)
print(error.endpoint)
print(error.payload)

SDK Exception Types

All SDK exceptions inherit from AISDKError.

ExceptionWhen it is raised
AuthenticationErrorThe API key is missing or invalid. Maps to 401 responses or missing local configuration.
NotFoundErrorThe requested assistant, app, or resource was not found. Maps to 404.
ValidationErrorThe request is invalid. Maps to 422.
ServerErrorThe API returned a server-side error. Maps to 5xx responses after retries.
APIConnectionErrorThe SDK could not reach the API after retries.
APIResponseErrorThe API returned another unclassified error response.
AISDKErrorBase class for all SDK exceptions.

Catch Errors

from custodian_labs import create_assistant
from custodian_labs import (
AISDKError,
APIConnectionError,
AuthenticationError,
NotFoundError,
ServerError,
ValidationError,
)

try:
app = create_assistant(
model="gpt-4o",
prompt="You are a helpful assistant.",
)

response = app.chat("Hello")
print(response.response)

except AuthenticationError:
print("Check your API key configuration.")

except NotFoundError:
print("The requested assistant or app was not found.")

except ValidationError as error:
print(f"Check the request body: {error}")

except ServerError:
print("The API returned a server error. Try again later.")

except APIConnectionError:
print("Could not reach the API. Check your network or base URL.")

except AISDKError as error:
print(f"Unexpected SDK error: {error}")

Retry Behavior

The SDK automatically retries:

  • Network request errors.
  • 5xx server errors.

The SDK does not retry:

  • 401 authentication errors.
  • 404 not found errors.
  • 422 validation errors.
  • Other 4xx request errors.

By default, the SDK uses:

SettingDefault
timeout30.0 seconds
max_retries2
retry_backoff0.25 seconds

With max_retries=2, the SDK makes up to three attempts total: the first request plus two retries.

Configure Timeout and Retries

You can pass retry and timeout settings to create_assistant() or Custodian:

from custodian_labs import create_assistant

app = create_assistant(
model="gpt-4o",
prompt="You are a helpful assistant.",
timeout=60.0,
max_retries=4,
)

To disable retries:

app = create_assistant(
model="gpt-4o",
prompt="You are a helpful assistant.",
max_retries=0,
)

Common Issues

Missing API key

Set CUSTODIAN_SDK_API_KEY in the same terminal or runtime where your application starts.

Invalid API URL

Check CUSTODIAN_SDK_BASE_URL if the SDK cannot reach the API. Restart your application after changing the value.

Import error

If Python cannot import custodian_labs, confirm that your virtual environment is active and reinstall the package:

python -m pip install --upgrade custodian-labs

Next Steps