Spavik

Python SDK

Evaluate, train, predict and forecast with the synchronous and asynchronous Python SDK.

pip install spavik            # with pandas: pip install "spavik[pandas]"

The SDK requires Python 3.11 or later.

from spavik import Spavik

spavik = Spavik()                                   # reads SPAVIK_API_KEY
model  = spavik.train("churn.csv", target="churn")  # free
print(model.predict({"plan": "pro", "seats": 12, "tickets_90d": 3}))

Two things to know before the first call: your key, and the name of the column to predict.

The operations

The ones that carry a model identifier live on the Model object, so nobody has to retype a mdl_... by hand.

CallCost
spavik.evaluate(data, target=...)free
spavik.train(data, target=...)free
spavik.backtest(data, target=..., horizon=...)free
spavik.forecast(data, target=..., horizon=...)billed per horizon step
spavik.models()free
spavik.usage()free
model.predict(rows)billed per row
model.submit_outcomes(outcomes)free
model.performance(days=30)free
model.refresh()free
model.archive(), spavik.archive(model_id)free
model.restore(), spavik.restore(model_id)free

models() is an iterator: pagination happens on its own. Its limit sets the page size, capped at 200, and the iterator walks every page whatever its value. A model exposes its engine and features, the columns predict expects. The raw API fields are in model.data, for example model.data.get("target").

Engine selection

Training accepts engine_family; omitting it lets the API choose its configured default family. Evaluation takes no family. Read GET /v1/engine-families for available families. The model's family cannot change. Predictions accept engine_version to select an exact version or latest alias within that family. Forecast and backtest accept both options. Available versions and prices come from GET /v1/engines. Keep the exact engine from the prediction result with its model version.

Check before you train, or before you pay

evaluate() returns structural validation, a trial and the API's assessment. backtest() validates a series and replays its history against the required references, without returning forecast values. Both are free. evaluate(..., explain=True) also ranks every feature column in report["trial"]["feature_importance"], to choose which ones to keep before training. It runs one extra prediction per column: on a wide table, give the client a longer timeout (see Default timeouts). See Prepare your data.

report = spavik.evaluate("churn.csv", target="churn")
print(report["assessment"]["status"])
print(report["assessment"]["metric"], report["assessment"]["message"])
print(report.get("hint"))

check = spavik.backtest(
    "sales.csv", target="sales", horizon=7, timestamp="date",
    point_estimate="median", quantiles=[0.1, 0.5, 0.9],
)
print(check["assessment"])
print(check.get("backtest"))

Read the evaluation guide before interpreting a score. The tabular trial uses a random holdout, with no temporal or group split controls. A better comparison on the sample does not establish future reliability.

Configuration

import os
from spavik import Spavik

spavik = Spavik(
    api_key=os.environ["SPAVIK_API_KEY"],
)

The key is sent in X-Spavik-Api-Key. Omitting api_key reads SPAVIK_API_KEY; an explicitly empty string disables the environment fallback.

The other options are keyword arguments:

OptionEffect
timeoutseconds per attempt, replacing every operation's default (see Default timeouts)
max_retries, backoffthe retry policy (see Retries)
user_agentthe User-Agent sent with each call
clientyour own httpx.Client (httpx.AsyncClient for AsyncSpavik), which the SDK then does not close

spavik.close() releases the HTTP connections the SDK opened. with Spavik() as spavik: closes them for you, as does async with AsyncSpavik().

Forecast options and results

forecast = spavik.forecast(
    "sales.csv", target="sales", horizon=7, timestamp="date",
    point_estimate="median", quantiles=[0.1, 0.5, 0.9],
)
print(forecast.points)  # parsed rows; no method call in Python
print(forecast.csv)
print(forecast.get("point_estimate"), forecast.predictions_remaining)

forecast.predictions_remaining copies the X-Spavik-Predictions-Remaining header, or is None when the API does not send it. forecast.cost is None: the API does not return the amount charged. See Costs and credits.

item_id names the series-ID column for multiple series. Match the backtest's history, horizon, column roles, point estimate and quantiles. The API default point estimate is mean. See series preparation for calendar constraints.

Observe performance

report = model.performance(days=30)
observed = report.get("observed")
if observed is not None:
    print(observed["assessment"], observed["metrics"], observed["baselines"])
    print(observed["metric_context"], report.get("evaluation"))

Call this after reporting outcomes. observed: null means no client-reported predictions are available for comparison; API failures are raised. The SDK preserves API scores, baselines, undefined reasons and assessment without recalculating them.

Archive and restore

Plans cap the number of active models. archive() stops a model from serving and frees its slot; everything is kept, and model.restore() brings it back. When you only have the identifier, use spavik.restore(model_id): an archived model cannot be read with spavik.model(), the API answers MODEL_ARCHIVED.

model.archive()
for m in spavik.models(status="archived"):
    print(m.id, m.name)
model.restore()

Other methods

CallReturns
spavik.model(model_id)the Model with that identifier
model.reload()the same Model, with its fields read again from the API
spavik.engines()the engine catalogue: versions, prices, statuses
spavik.engine_families()the engine families and the default one

engines() and engine_families() work without an API key. A Result, such as the return value of predict or forecast, carries the response body in result.data and reads like a dict. It also has result.request_id, from the X-Spavik-Request-Id header, result.predictions_remaining and result.engine; result.cost is described in Costs and credits.

Not in the library

These API routes have no SDK method. Call them over HTTP with the same key:

  • PATCH /v1/models/{id}: rename a model or change auto_update.
  • GET /v1/models/{id}/versions: list a model's versions.
  • DELETE /v1/models/{id}: delete a model permanently.
  • GET /v1/plans: read the plan catalogue.
  • GET /v1/requests: read the request log.

The data format is yours

spavik.train(df, target="churn")                  # pandas DataFrame
spavik.train("data/churn.csv", target="churn")    # file path
spavik.train([{"plan": "pro", "churn": 0}], ...)  # list of dicts

CSV, JSON, semicolons or commas, quoted or not: the library sorts it out, and retypes numbers along the way. A DataFrame can hold date and timestamp columns, NaN and Decimal values: the library converts them before sending. See how CSV values are read.

What is refused before being sent

The library has your data in hand, the API does not yet. These refusals happen locally, with no network call and no spend. Among them:

Spavik(api_key="").usage()
# SpavikAuthError: no API key: set SPAVIK_API_KEY or pass api_key. Nothing was sent.

spavik.train(eighty_rows, target="churn")
# SpavikDataError: training needs at least 100 rows, 80 provided. Nothing was sent.

spavik.train(df, target="churn_90d")
# SpavikDataError: the target column 'churn_90d' is missing from the data.
#                  Columns found: plan, seats, tickets_90d, churn. Did you mean 'churn'?

spavik.forecast(three_points, target="sales", horizon=14)
# SpavikDataError: the series has 3 point(s) for a horizon of 14. A series shorter
#                  than its horizon yields nothing usable: provide at least 14 points,
#                  preferably several times more.

model.submit_outcomes([{"request_id": "...", "outcome": 1}])
# SpavikDataError: 1 outcome(s) out of 1 have no 'actual' field, the value actually
#                  observed. Fields found on the first: request_id, outcome.

The messages are the same, word for word, in the JavaScript library.

Errors

One class per family, all subclasses of SpavikError, each carrying the machine code, the detail returned by the API, the status and the request_id.

from spavik import SpavikCreditsExhausted

try:
    model.predict(rows)
except SpavikCreditsExhausted as exc:
    print(exc.code, exc.detail, exc.request_id)
    print(spavik.usage()["predictions_remaining"])
ClassWhen
SpavikDataErrorlocal refusal, nothing was sent
SpavikAuthErrorkey missing, invalid, or insufficient
SpavikCreditsExhaustedbalance too low; read usage() for what is left
SpavikQuotaExceededa plan limit is reached: active models, rows
SpavikValidationErrorthe API refuses the request as formed
SpavikNotFoundthe resource does not exist in this workspace
SpavikConflictreplay, work in progress, nothing to integrate
SpavikRateLimiteda 429 response
SpavikUnavailabledependency missing or asleep
SpavikServerErrorunexpected API failure
SpavikTransportErrornetwork, or non-JSON response
SpavikSignatureErrora webhook delivery fails verify

Normal states, not errors

model.refresh() returns None only when there are no pending outcomes. Other API failures are raised. model.performance() preserves the API report; observed: null means there are no measured outcomes to compare. An API failure is raised rather than converted into an empty performance report.

Async

Same names, same refusals, same results.

from spavik import AsyncSpavik

async with AsyncSpavik() as spavik:
    model = await spavik.train("churn.csv", target="churn")
    print(await model.predict(rows))

Retries

The SDK follows the API retry policy for network failures, eligible 429/5xx responses and REQUEST_IN_PROGRESS. It keeps the same idempotency key during an invocation's internal retries. Replays are protected while the API record is retained (24 hours by default for completed responses).

Keep exc.idempotency_key and exc.request_id after an uncertain failure. Calling the SDK operation again generates a new key and may execute or charge again, unless you pass the original key: train, predict, forecast, submit_outcomes and refresh accept idempotency_key=. With the same key and input, the API replays the stored response instead of running it again.

from spavik import SpavikTransportError

try:
    result = model.predict(rows)
except SpavikTransportError as exc:
    result = model.predict(rows, idempotency_key=exc.idempotency_key)

REQUEST_INTERRUPTED and IDEMPOTENCY_RESPONSE_UNAVAILABLE stop automatic retries: see uncertain outcomes.

max_retries counts every attempt, the first included: the default is 3, and max_retries=1 disables retries. Without Retry-After, the default backoff is exponential with jitter, from 0.5 s up to 8 s.

Spavik(max_retries=1)                    # a single attempt, no retry
Spavik(backoff=lambda n: n * 1.0)        # linear backoff, n is the attempt number

Webhooks

verify checks the Spavik-Signature header and returns the decoded event. It raises SpavikSignatureError when the signature, the timestamp or the header is invalid, and when the secret is empty (missing webhook secret). The timestamp may be 300 seconds old at most; tolerance= changes that window, in seconds. With Flask:

import os

from flask import Flask, request
from spavik import SpavikSignatureError, verify

app = Flask(__name__)

@app.post("/spavik/webhook")
def spavik_webhook():
    try:
        event = verify(request.get_data(), request.headers, os.environ["SPAVIK_WEBHOOK_SECRET"])
    except SpavikSignatureError:
        return "", 400
    # Store event["id"] and the event durably, then acknowledge.
    return "", 204

The body must be the bytes as received: a parsed and re-serialised body does not produce the same signature. See Webhooks for the delivery contract.

Debugging

SPAVIK_LOG=debug python my_script.py
spavik POST /v1/models  idem=a3f1b2c4  23.81s  201  req_atZusk...  key=sk-spv-api-bm7...Tr9x

The key is masked.

Default timeouts

OperationTimeout
train, evaluate, refresh120 s
forecast, backtest60 s
predict, submit_outcomes, performance, archive, restore30 s
models, model, usage, engines, engine_families15 s

The engine takes about ten seconds to answer when it wakes from idle, which is why the timeouts are generous. The client's timeout, in seconds, replaces every one of them, per attempt; leave it out to keep these defaults.

How is this guide?

Last updated on

On this page