Skip to content
imaged

Python setup

macOS 15+ · Apple silicon · Python 3

Python bindings for the Slab runtime, using ctypes on the shared library (libslab.dylib). No compiled extension and no required dependencies; NumPy is used when installed. Tested with Python 3.11 on Apple silicon, macOS 15 or later.

Install

The macOS release contains the library and the Python package side by side:

macos/
  libslab.dylib
  slab.h
  python/slab/

Add macos/python to PYTHONPATH (or copy python/slab into your project). The package finds libslab.dylib two folders up. Elsewhere, point it at the library with slab.load("/path/to/libslab.dylib") or the SLAB_LIBRARY environment variable.

If macOS blocks the downloaded library, follow the macOS download instructions.

Use

import slab

cfg = slab.Config(models_dir="models", license=open("license.txt").read())
session = slab.Session(cfg, "classifier.slab")

out = session.run(slab.Input(frame=slab.Frame.open("photo.jpg")))
for c in out.top_k(5):
    print(c.label or c.class_id, c.score)

Streaming text:

inp = slab.Input(prompt="Describe this image.")
for piece in session.generate(inp):
    print(piece, end="")

Examples: classify, faces, tensors, chat, camera (live detection with OpenCV).

API

The objects are the same as in Slab's C and C+ APIs: Config, Session, Frame, Input, Output. In Python:

Python Behavior
Errors Failed calls raise slab.SlabError (code, message). License problems raise slab.LicenseError with a detail reason code.
Session(cfg, path) Creates and opens in one step. Keyword options: backend, warmup, context_length.
session.run(inp) Returns an Output (or fills the one passed).
session.generate(inp, out) Iterator over text pieces; out holds the totals afterwards. session.cancel() works from any thread.
input.set_tensor(index_or_name, data, dtype) data: NumPy array, array.array, raw bytes or a list. Always copied.
out.array(i), out.numpy(i), out.shape(i) Copies of output tensors.
out.top_k(k), out.boxes(...) Lists of Classification and Box (with landmarks).
frame.crop(x, y, w, h) A view sharing the parent's pixels; the parent is kept alive.
Objects Freed by garbage collection, or at once with close() / with.

Licensing on macOS

The host process needs a bundle identifier listed in the runtime license. Use the bindings inside a macOS app that embeds Python, or an executable that embeds an Info.plist with the licensed CFBundleIdentifier. See the C setup guide for embedding an identifier in a command-line executable.

A plain python3 process may not have your app's identity. Setting PYTHONPATH or SLAB_LIBRARY only locates code; it does not supply a licensed app ID. An identity error must be fixed in the host executable. New app-ID licenses do not require a Keychain group.

For model preparation and benchmarking from the terminal, use slab-load with the same license file. See Prepare a model.

License details

Slab 0.2.0 exposes the attached license on Config and Session. Use it to display expiry and send renewal reminders to whoever maintains the license.

Config member Value
license_seconds_left Seconds remaining
license_expires_at Expiry, Unix seconds
license_not_before Start of validity, Unix seconds
license_issued_at Issue time, Unix seconds
license_kind License kind
license_features Enabled feature flags
license_id License ID

Seconds remaining becomes negative after expiry and is 0 without a license. Kind values are 0 none, 1 production, 2 trial and 3 pilot. Feature bits are 1 core, 2 graph models, 4 generative, 8 GPU, 16 NPU and 32 protected models.

The session object exposes the same license members.

For protected models, generate a license with your model key in the console. Attach that license before opening the pack. See licensing for model protection and renewals.