tempestweb.tabular¶
sklearn-to-ONNX inference in the browser, the sibling of vision. The manifest — which features the model expects, and in what order — is what stops a silently wrong prediction. Modes A and B (Mode C refuses the import at build time). Training and exporting are a build step in a throwaway venv, not a dependency.
Guide with examples: Tabular inference in the browser.
tempestweb.tabular ¶
Tabular inference in the browser — the sibling of tempestweb.vision.
Vision runs a model on pixels. This runs one on a row of numbers, which is the commonest kind of ML in a business app: a risk score, a demand forecast, a lead classification. Without it, those had to call an endpoint — which breaks offline-first, one of the framework's promises.
Modules
* :mod:`manifest` — which features the model expects, and in what order.
* :mod:`predictor` — `TabularPredictor`, loaded lazily, addressed by name.
* :mod:`errors` — one named error per way a row can fail to match.
Example
The manifest is what keeps this from being silently wrong
An ONNX model is a function from an unlabelled vector of floats to a number:
the order carries all the meaning, and nothing in the runtime checks it.
An app that sends {"idade": 30} to a model trained on age does not fail —
it reads a zero where the age should be and answers a plausible, wrong score,
and nothing downstream can tell.
With a manifest that raises MissingFeatureError, naming the feature that is
missing and the one that was sent instead, because the two together are
usually one typo.
Training and export are a build step, not a dependency
Exporting sklearn to ONNX runs in a throwaway environment
(uvx --from skl2onnx …), documented in the recipe. Nothing here depends on
sklearn, skl2onnx or numpy at runtime: those bounds would propagate to every
tempestweb consumer for a step that happens once, on a developer's machine.
Modes A and B only
Mode C serves a fixed set of modules — tempest_core, tempestweb.components
and tempestweb.native — and refuses this import at build time.
Import everything from this package level rather than from submodules.
CompactModel
dataclass
¶
A parsed compact model: its header, and its arrays.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
str
|
|
task |
str
|
|
link |
str
|
How raw scores become probabilities ( |
classes |
tuple[str, ...]
|
Class labels in score-column order. Empty for a regressor. |
n_features |
int
|
Values expected per row. |
n_outputs |
int
|
Score columns per row. |
n_trees |
int
|
Trees in the ensemble; |
estimator |
str
|
Class name of the exported estimator, which names the model
in every :class: |
feature_names |
tuple[str, ...]
|
The column order the model was trained on, when the export recorded it. |
offset |
tuple[float, ...]
|
Per-feature offset of a folded scaler, empty when there is none. |
scale |
tuple[float, ...]
|
Per-feature scale of a folded scaler, empty when there is none. |
sections |
dict[str, Sequence[float]]
|
The decoded arrays, keyed by the name the header gave them. |
Source code in tempestweb/tabular/compact.py
manifest ¶
Build the manifest the file itself declares.
Returns:
| Name | Type | Description |
|---|---|---|
The |
FeatureManifest
|
class: |
FeatureManifest
|
attr: |
Raises:
| Type | Description |
|---|---|
ManifestError
|
If the export recorded no feature names — the file can still be scored positionally, but not addressed by name. |
Source code in tempestweb/tabular/compact.py
section ¶
Read one decoded section.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The section name the header gave it (e.g. |
required |
Returns:
| Type | Description |
|---|---|
Sequence[float]
|
Its values. |
Raises:
| Type | Description |
|---|---|
CompactFormatError
|
If the model carries no such section. |
Source code in tempestweb/tabular/compact.py
CompactPredictor ¶
A compact model, loaded lazily and addressed by feature name.
The file is downloaded on the first prediction, not in __init__: building
a predictor at module scope must not fetch anything, and an app that defines
three and uses one should pay for one.
Attributes:
| Name | Type | Description |
|---|---|---|
model_url |
str
|
Where the |
Source code in tempestweb/tabular/compact.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
load
async
¶
Download and parse the model, or return the one already parsed.
Returns:
| Name | Type | Description |
|---|---|---|
The |
CompactModel
|
class: |
Raises:
| Type | Description |
|---|---|
CompactFormatError
|
If the bytes are not a compact model this reader understands. |
NativeError
|
If the file cannot be downloaded ( |
Source code in tempestweb/tabular/compact.py
manifest
async
¶
Resolve the feature order, from the file or from the override.
Returns:
| Name | Type | Description |
|---|---|---|
The |
FeatureManifest
|
class: |
FeatureManifest
|
first resolution. |
Raises:
| Type | Description |
|---|---|
ManifestError
|
If neither the file nor the override declares features. |
NativeError
|
If a manifest URL cannot be fetched. |
Source code in tempestweb/tabular/compact.py
predict
async
¶
Score one row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
Mapping[str, object]
|
The feature values, in any order — the manifest imposes the one the model needs. |
required |
strict
|
bool
|
Whether a feature the model does not declare is an error. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
Prediction
|
class: |
Raises:
| Type | Description |
|---|---|
ManifestError
|
If the manifest orders a different number of features than the model expects. |
MissingFeatureError
|
If the row lacks a declared feature. |
UnknownFeatureError
|
If |
CompactFormatError
|
If the file is not a compact model this reader understands. |
NativeError
|
If the download fails. |
Source code in tempestweb/tabular/compact.py
predict_many
async
¶
Score several rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
Sequence[Mapping[str, object]]
|
The rows to score. |
required |
strict
|
bool
|
Whether a feature the model does not declare is an error. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
One |
list[Prediction]
|
class: |
list[Prediction]
|
empty |
|
list[Prediction]
|
scoring nothing is valid. |
Raises:
| Type | Description |
|---|---|
ManifestError
|
If the manifest orders a different number of features than the model expects — the mismatch the manifest exists to catch, arriving from the manifest's own side. |
MissingFeatureError
|
If a row lacks a declared feature. |
UnknownFeatureError
|
If |
CompactFormatError
|
If the file is not a compact model this reader understands. |
NativeError
|
If the download fails. |
Source code in tempestweb/tabular/compact.py
CompactFormatError ¶
Bases: TabularError
The bytes are not a compact model this reader understands.
Raised for wrong magic bytes, a layout version this reader does not
implement, a section the header promised and the file does not hold, or a
kind/link outside the format. Every one of them means the file was
written by something other than
tempest_fastapi_sdk.modelops.export_sklearn_to_compact at the version
this reader was built against — guessing past that would predict on garbage.
Source code in tempestweb/tabular/errors.py
ManifestError ¶
Bases: TabularError
The manifest itself is unusable.
Raised for a manifest with no features, with duplicates, or whose JSON is not the shape a manifest has. A broken manifest is a build-time mistake and is worth failing loudly on, because everything after it is built on the order it declares.
Source code in tempestweb/tabular/errors.py
MissingFeatureError ¶
Bases: TabularError
The row does not carry every feature the model was trained on.
Attributes:
| Name | Type | Description |
|---|---|---|
missing |
tuple[str, ...]
|
The declared features the row lacks. |
extra |
tuple[str, ...]
|
Features the row carries that the model does not know, listed
alongside because the pair is usually one typo — |
Source code in tempestweb/tabular/errors.py
PredictionError ¶
Bases: TabularError
The model ran but its output could not be read as a prediction.
TabularError ¶
UnknownFeatureError ¶
Bases: TabularError
The row carries a feature the model was not trained on.
Attributes:
| Name | Type | Description |
|---|---|---|
unknown |
tuple[str, ...]
|
The features the model does not declare. |
Source code in tempestweb/tabular/errors.py
FeatureManifest
dataclass
¶
What a model expects, declared rather than remembered.
Attributes:
| Name | Type | Description |
|---|---|---|
features |
tuple[str, ...]
|
The feature names, in the order the model was trained on. The order is the contract; the names are what makes it checkable. |
version |
str
|
The model version, for logs and cache busting. |
outputs |
tuple[str, ...]
|
The model's output names, in declaration order. |
classes |
tuple[str, ...]
|
The class labels a classifier answers, in index order. Empty for a regressor. |
Source code in tempestweb/tabular/manifest.py
vector ¶
Order one row into the vector the model expects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
Mapping[str, Any]
|
The feature values, in any order. |
required |
strict
|
bool
|
Whether a feature the model does not declare is an error. On by default: a stray key is almost always a typo, and dropping it silently is how a wrong prediction gets made. |
True
|
Returns:
| Type | Description |
|---|---|
list[float]
|
The values as floats, ordered by :attr: |
Raises:
| Type | Description |
|---|---|
MissingFeatureError
|
If the row lacks a declared feature. The message lists what is missing and what was sent instead, because the two together are usually one typo. |
UnknownFeatureError
|
If |
ValueError
|
If a value is not a number. |
Source code in tempestweb/tabular/manifest.py
label_of ¶
Name the class at an index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
The class index the model answered. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The declared label, or the index as text when the manifest declares |
str
|
no classes — a regressor has none, and a classifier shipped without |
str
|
them is still usable. |
Source code in tempestweb/tabular/manifest.py
Prediction
dataclass
¶
One row's answer.
Attributes:
| Name | Type | Description |
|---|---|---|
score |
float
|
The single number the model answered. For a regressor this is the value; for a classifier it is the probability of the predicted class, or the raw output when the model reports no probabilities. |
label |
str
|
The predicted class name, resolved through the manifest's
|
index |
int
|
The predicted class index, or |
probabilities |
dict[str, float]
|
Class name to probability, empty when the model does not report them. |
Source code in tempestweb/tabular/predictor.py
TabularPredictor ¶
A sklearn-to-ONNX model, loaded lazily and addressed by feature name.
The session is created on the first prediction, not in __init__: building
a predictor at module scope must not download a model, and an app that
defines three predictors and uses one should pay for one.
Attributes:
| Name | Type | Description |
|---|---|---|
model_url |
str
|
Where the |
providers |
list[str]
|
Execution providers, in preference order. |
Source code in tempestweb/tabular/predictor.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
manifest
async
¶
Resolve the manifest, fetching it if it was given as a URL.
Returns:
| Name | Type | Description |
|---|---|---|
The |
FeatureManifest
|
class: |
Raises:
| Type | Description |
|---|---|
ManifestError
|
If the fetched document is not a valid manifest. |
NativeError
|
If the manifest URL cannot be fetched. |
Source code in tempestweb/tabular/predictor.py
load
async
¶
Create the inference session, or return the one already created.
Returns:
| Name | Type | Description |
|---|---|---|
The |
OnnxModel
|
class: |
Raises:
| Type | Description |
|---|---|
NativeError
|
If the model fails to download or compile
( |
Source code in tempestweb/tabular/predictor.py
predict
async
¶
Score one row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
Mapping[str, object]
|
The feature values, in any order — the manifest imposes the one the model needs. |
required |
strict
|
bool
|
Whether a feature the model does not declare is an error. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
Prediction
|
class: |
Raises:
| Type | Description |
|---|---|
MissingFeatureError
|
If the row lacks a declared feature. |
UnknownFeatureError
|
If |
PredictionError
|
If the model answers nothing readable. |
NativeError
|
If loading or inference fails. |
Source code in tempestweb/tabular/predictor.py
predict_many
async
¶
Score several rows in a single inference run.
One run rather than one per row: crossing the bridge and entering the runtime dominate the cost for a model this size, so a hundred rows scored together are far cheaper than a hundred scored apart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
Sequence[Mapping[str, object]]
|
The rows to score. |
required |
strict
|
bool
|
Whether a feature the model does not declare is an error. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
One |
list[Prediction]
|
class: |
list[Prediction]
|
|
Raises:
| Type | Description |
|---|---|
MissingFeatureError
|
If a row lacks a declared feature. |
UnknownFeatureError
|
If |
PredictionError
|
If the model answers nothing readable. |
NativeError
|
If loading or inference fails. |
Source code in tempestweb/tabular/predictor.py
parse ¶
Read compact model bytes into a :class:CompactModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
The whole |
required |
Returns:
| Type | Description |
|---|---|
CompactModel
|
The parsed model, arrays included. |
Raises:
| Type | Description |
|---|---|
CompactFormatError
|
If the magic bytes, the layout version, the
|
Source code in tempestweb/tabular/compact.py
manifest_from_dict ¶
Read a manifest out of a decoded JSON object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
Mapping[str, Any]
|
The decoded manifest. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
The |
FeatureManifest
|
class: |
Raises:
| Type | Description |
|---|---|
ManifestError
|
If the payload is not a mapping, or its |
Source code in tempestweb/tabular/manifest.py
manifest_from_json ¶
Read a manifest out of JSON text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The manifest's JSON. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
The |
FeatureManifest
|
class: |
Raises:
| Type | Description |
|---|---|
ManifestError
|
If the text is not valid JSON, or not a valid manifest. |