Error Handling

All SDK errors extend TouAIError and are importable from touai. Each HTTP status code maps to a more specific exception class.

Common Exceptions

from touai import (
    TouAI,
    TouAIError,
    AuthenticationError,
    InsufficientCreditsError,
    PermissionDeniedError,
    NotFoundError,
    ConflictError,
    ValidationError,
    RateLimitError,
    ServerError,
    TimeoutError,
    ConnectionError,
)
 
try:
    results = client.connectors.search("test")
except AuthenticationError:
    print("Invalid or expired API key")
except InsufficientCreditsError as e:
    print(f"Not enough credits (reason: {e.reason})")
except RateLimitError as e:
    print(f"Retry after {e.retry_after}s")
except NotFoundError:
    print("Resource not found")
except ValidationError as e:
    print(e.message)
except ServerError:
    print("Server error")
except TouAIError as e:
    print(e.status_code, e.message, e.request_id, e.body)

Exception Classes

ClassRaised For
AuthenticationError401 — invalid or expired API key
PermissionDeniedError403 — insufficient permissions
ApiKeyEntitlementRequired403 — the organization has no API key entitlement (top up or subscribe in the console); subclass of PermissionDeniedError
InsufficientCreditsError402 — not enough credits; carries requires_topup, reason, max_allowed_credits
NotFoundErrorthe resource does not exist
ValidationError400 / 422 — request validation failed
ConflictError409 — conflicting state
ConnectorSetupRequired409 — the connector must be configured in the web console first; carries setup_url; subclass of ConflictError
PlaygroundQuotaExceeded409 — console playground quota is full (never raised for SDK/API-key traffic); subclass of ConflictError
GoneError410 — the endpoint or resource was retired; the message names the replacement
RateLimitError429 — too many requests; carries retry_after
ServerError5xx — server-side error
BillingFrozenError503 — billing is temporarily frozen during an incident; subclass of ServerError
TimeoutErrorrequest timed out; also subclasses the builtin TimeoutError
ConnectionErrornetwork connection failed

Error Properties

All TouAIError subclasses include:

PropertyTypeDescription
messagestrHuman-readable error description
status_code`int \None`
bodyAnyRaw response body
request_id`str \None`
error_code`str \None`

Automatic Retries

The SDK retries 429 responses for every request, and 500, 502, 503, 504 for requests that are safe to replay (GET, or writes carrying an Idempotency-Key), with exponential backoff. It respects the Retry-After header when present and uses max_retries to cap attempts.

Retries help with transient failures, but they will not rescue validation errors or invalid credentials. Keep your error handling explicit around auth and request payloads.

AuthType Reference