Smritea

Handle errors and retries

Catch each Smritea error type, know which ones the SDK retries, and decide what your code does for the rest.

Ask AI about this page

The SDK retries a rate limit for you and raises a typed error for every failure. Your code decides what to do with each type. Catch the specific types first, then the base error class.

What gets retried

The SDK retries only HTTP 429, the rate limit error. It never retries any other status.

The table describes what the SDK does on its own when a call gets a 429. You do not write this logic yourself.

BehaviorWhat the SDK does
Number of retriesIt retries up to 2 times. Set max_retries to change this number, or set it to 0 to turn retries off.
Wait before a retryWhen the server sends a Retry-After header, the SDK waits 90 percent of that time. Otherwise it waits 1 second before the first retry and doubles the wait for each later retry. It adds or removes up to 25 percent of the wait at random.
Longest waitThe SDK never waits more than 30 seconds.
After the last retryThe SDK raises SmriteaTooManyRequestsError.

Choose an action for each error

The table below is advice for your code. For each error type it says what your code should do after the SDK raises that error.

ErrorHTTP statusWhat to do
SmriteaBadRequestError400Fix the request. Retrying the same request fails again.
SmriteaUnauthorizedError401Check the API key and the host for your region.
SmriteaPaymentRequiredError402Your plan quota has run out. Wait for the next period or change your plan. Do not retry.
SmriteaForbiddenError403Check that the app ID matches the API key's app.
SmriteaNotFoundError404Check the memory ID.
SmriteaConflictError409Read the message and adjust the request.
SmriteaUnprocessableError422Remove the unsupported part, such as $contains.
SmriteaTooManyRequestsError429You hit a rate limit and the SDK retries ran out. Slow down, then try again.
SmriteaError5xx, otherThe failure is on the server side. Retry later with your own backoff.

A rate limit (429) and a quota (402) are different. A rate limit clears in seconds. A quota clears when the period ends or you change plans. See Limits, quotas and credits.

Catch the errors

import time

from smritea import (
    SmriteaError,
    SmriteaPaymentRequiredError,
    SmriteaTooManyRequestsError,
)

def search_safely(query, scope):
    try:
        return client.search(query, scope=scope)
    except SmriteaPaymentRequiredError:
        print("Quota used up. Not retrying.")
    except SmriteaTooManyRequestsError as e:
        time.sleep(e.retry_after or 5)
        return client.search(query, scope=scope)
    except SmriteaError as e:
        print(e.http_status, e.code, e.request_id)
    return []

In Go, a typed error does not match *smritea.SmriteaError in errors.As. Check the typed errors first, as the code does.

Names differ by SDK. Python and TypeScript use max_retries and maxRetries. Go uses MaxRetries, a pointer set with smritea.Int(n). Java takes maxRetries as a constructor argument. C# uses the maxRetries parameter. In C#, the error classes end in Exception and the base class is SmriteaException. C# wraps only HTTP errors, so a network failure surfaces as a .NET exception.

Report a failure

Every error carries a request_id. Include it when you contact support, so the team can find the failed call.

Last updated on