Python setup
macOS 15+ · signed Apple silicon app
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.
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.
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
On macOS a runtime license is bound to a signed app: its bundle ID and
Keychain group. A plain python3 command is not a signed app, so with a
customer build of libslab.dylib the license is refused and models do not
open. Use the bindings inside a signed macOS app that embeds Python, or with
a development build of the library.