Skip to main content

Error Handling

Learn how to handle errors from the Structurify API in your applications.

Error Types

ErrorHTTP StatusDescription
AuthenticationError401Invalid or missing API key
NotFoundError404Resource not found
ValidationError400Invalid request data
InsufficientCreditsError402Not enough credits
RateLimitError429Rate limit exceeded
ServerError500Internal server error

SDK Error Handling

from structurify import (
Structurify,
StructurifyError,
AuthenticationError,
NotFoundError,
ValidationError,
InsufficientCreditsError,
RateLimitError,
ServerError,
)

client = Structurify(api_key="sk_live_xxx")

try:
project = client.projects.get("proj_invalid")
except AuthenticationError:
print("Invalid API key. Check your credentials.")
except NotFoundError:
print("Project not found. Check the ID.")
except ValidationError as e:
print(f"Invalid request: {e.message}")
except InsufficientCreditsError:
print("Not enough credits. Please top up.")
except RateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after} seconds.")
except ServerError:
print("Server error. Please try again later.")
except StructurifyError as e:
print(f"API error: {e}")

API Error Response

All API errors return this JSON format:

{
"error": "ErrorCode",
"message": "Human-readable description"
}

Retry Strategies

Rate Limiting

Implement exponential backoff for rate limits:

import time
from structurify import Structurify, RateLimitError

def with_retry(func, max_retries=3):
for attempt in range(max_retries):
try:
return func()
except RateLimitError as e:
if attempt < max_retries - 1:
wait = e.retry_after or (2 ** attempt)
print(f"Rate limited. Waiting {wait}s...")
time.sleep(wait)
else:
raise

# Usage
result = with_retry(lambda: client.extraction.run(project_id="proj_xxx"))

Server Errors

For transient server errors (5xx), retry with backoff:

import time
from structurify import ServerError

def with_server_retry(func, max_retries=3):
for attempt in range(max_retries):
try:
return func()
except ServerError:
if attempt < max_retries - 1:
wait = 2 ** attempt
print(f"Server error. Retrying in {wait}s...")
time.sleep(wait)
else:
raise

Best Practices

  1. Always catch errors - Don't let unhandled exceptions crash your app
  2. Log error details - Include error codes and messages for debugging
  3. Use specific exceptions - Handle different errors appropriately
  4. Implement retries - Retry transient errors with backoff
  5. Monitor errors - Track error rates in your application

Common Issues

Invalid API Key

AuthenticationError: Invalid or missing API key

Solution: Verify your API key is correct and hasn't been revoked.

Project Not Found

NotFoundError: Project not found

Solution: Check the project ID. It may have been deleted.

Rate Limited

RateLimitError: Rate limit exceeded. Retry after 30s

Solution: Implement retry logic with the retry_after value.

Insufficient Credits

InsufficientCreditsError: Not enough credits

Solution: Top up credits in your dashboard.