Error Handling & Retries

Error hierarchy (BlockedError, AuthenticationError, RateLimitError, APIError, APIConnectionError), automatic retry policy, and timeout configuration for the Highflame SDK.

The Highflame SDK provides a structured error hierarchy so you can handle each failure mode distinctly. Transient network and server errors are retried automatically.

Error Hierarchy

All SDK errors extend a single base class:

HighflameError
├── BlockedError          — Shield decision was "deny"
└── APIError              — HTTP error from the service
    ├── AuthenticationError  — 401 Unauthorized
    └── RateLimitError       — 429 Too Many Requests
APIConnectionError        — Network failure or timeout (extends HighflameError)

Python

from highflame import (
    Highflame,
    GuardRequest,
    HighflameError,
    APIError,
    AuthenticationError,
    RateLimitError,
    APIConnectionError,
    BlockedError,
)

client = Highflame(api_key="hf_sk_...")

try:
    resp = client.guard.evaluate(GuardRequest(
        content=user_input,
        content_type="prompt",
        action="process_prompt",
    ))

except BlockedError as e:
    # Raised only by Shield decorators (@shield.prompt, @shield.tool, etc.)
    # Direct client.guard.evaluate() calls return GuardResponse and never raise on deny.
    print(f"Blocked: {e.response.policy_reason}")
    print(f"Signals: {[s.name for s in e.response.signals]}")

except AuthenticationError as e:
    # 401 — invalid or expired API key
    print(f"Auth failed: {e.status} {e.title}{e.detail}")

except RateLimitError as e:
    # 429 — request quota exceeded
    print(f"Rate limited: {e.status} {e.title}{e.detail}")

except APIError as e:
    # Any other non-2xx HTTP response
    print(f"API error {e.status}: {e.title}{e.detail}")

except APIConnectionError as e:
    # Network failure, timeout, DNS error
    print(f"Connection error: {e}")

except HighflameError as e:
    # Catch-all for any other SDK error
    print(f"SDK error: {e}")

APIError Attributes

Attribute
Type
Description

status

int

HTTP status code

title

str

Short machine-readable error title

detail

str

Human-readable description of the error

BlockedError Attributes

Attribute
Type
Description

response

GuardResponse

The full evaluation response from Shield

TypeScript

Catching BlockedError from Shield wrappers

BlockedError is only raised by Shield decorators (Python) and Shield wrappers (TypeScript). When you call client.guard.evaluate() directly, the call always resolves — check resp.denied (Python) or resp.decision === "deny" (TypeScript) yourself.

Automatic Retries

The SDK automatically retries failed requests on transient errors. Retries use exponential backoff with jitter.

Retried status codes: 429, 500, 502, 503, 504

Default retry count: 2 (3 total attempts)

AuthenticationError (401) is not retried — a 401 indicates a credential problem that retrying will not fix.

Timeouts

The default per-request timeout is 30 seconds (30,000ms in TypeScript).

Handling Rate Limits Manually

If you want to implement your own backoff on top of the SDK's automatic retries:

Self-Hosted Deployments

For self-hosted deployments, APIConnectionError indicates the service is unreachable. Check that base_url and token_url are configured correctly and that the service is healthy.

Last updated