PerslyPersly API

Error Handling

Understand API error responses and implement robust error handling.

When an API request fails, Persly returns a JSON error response with a consistent structure.

Error Response Format

{
  "error": {
    "message": "Human-readable error description",
    "type": "invalid_request_error",
    "code": "model_not_found",
    "param": "model"
  }
}
FieldTypeDescription
messagestringHuman-readable explanation of what went wrong
typestringError category (see table below)
codestringMachine-readable error code
paramstring | nullThe parameter that caused the error, if applicable

Error Codes

400 — Bad Request

CodeTypeDescription
model_not_foundinvalid_request_errorThe specified model does not exist
missing_required_fieldinvalid_request_errorA required field is missing from the request
invalid_requestinvalid_request_errorThe request body is malformed or contains invalid values
batch_size_exceededinvalid_request_errorEmbedding batch size exceeds 100 items
documents_limit_exceededinvalid_request_errorRerank document count exceeds 1,000

401 — Unauthorized

CodeTypeDescription
authentication_errorauthentication_errorThe API key is missing, invalid, or revoked

403 — Forbidden

CodeTypeDescription
organization_suspendedpermission_errorYour organization has been suspended

429 — Too Many Requests

CodeTypeDescription
quota_exceededinsufficient_quotaFree tier credits are exhausted
spending_limit_exceededrate_limit_errorMonthly spending limit reached

500 — Server Error

CodeTypeDescription
internal_errorserver_errorAn unexpected error occurred on our servers

Error Handling Examples

import requests

response = requests.post(
    "https://api.persly.ai/v1/chat/completions",
    headers={"Authorization": "Bearer YOUR_API_KEY"},
    json={
        "model": "persly-chat-v1",
        "messages": [{"role": "user", "content": "Hello"}],
    },
)

if response.status_code != 200:
    error = response.json()["error"]
    print(f"Error [{error['code']}]: {error['message']}")
else:
    data = response.json()
    print(data["choices"][0]["message"]["content"])
const response = await fetch("https://api.persly.ai/v1/chat/completions", {
  method: "POST",
  headers: {
    Authorization: "Bearer YOUR_API_KEY",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "persly-chat-v1",
    messages: [{ role: "user", content: "Hello" }],
  }),
});

if (!response.ok) {
  const { error } = await response.json();
  console.error(`Error [${error.code}]: ${error.message}`);
} else {
  const data = await response.json();
  console.log(data.choices[0].message.content);
}

Retry Strategy

For transient errors (5xx), implement exponential backoff:

import time
import requests

def call_api(payload, max_retries=3):
    for attempt in range(max_retries):
        response = requests.post(
            "https://api.persly.ai/v1/chat/completions",
            headers={"Authorization": "Bearer YOUR_API_KEY"},
            json=payload,
        )
        if response.status_code < 500:
            return response

        wait = 2 ** attempt  # 1s, 2s, 4s
        time.sleep(wait)

    return response  # Return last response after all retries
Error TypeRetry?Action
400 Bad RequestNoFix the request parameters
401 UnauthorizedNoCheck your API key
403 ForbiddenNoContact support
429 Quota ExceededNoUpgrade plan or wait for reset
429 Spending LimitNoIncrease spending limit in dashboard
500 Server ErrorYesRetry with exponential backoff

On this page