Language Bindings
Litsea is a Rust library, but it is also usable from other languages. The bindings live in this repository as workspace members, so they are versioned and released together with litsea itself.
Crates
| Crate | Target runtime | FFI stack | Status |
|---|---|---|---|
litsea-binding-core | (shared, no FFI) | — | Available |
litsea-python | Python 3.10+ | PyO3 + maturin | Available |
litsea-nodejs | Node.js 20+ | napi-rs | Available |
litsea-php | PHP 8.1+ | ext-php-rs | Available |
litsea-ruby | Ruby 3.1+ | magnus + rb-sys | Available |
litsea-wasm | Browser / Deno | wasm-bindgen | Available |
Capabilities
Every binding segments, tags and takes a user dictionary, but not every host allows the rest:
| Crate | Model input | Training | Stopping a training run in progress |
|---|---|---|---|
litsea-python | Bytes, path, file://, http(s):// | Yes | Yes: the GIL is released |
litsea-nodejs | Bytes, path, file://, http(s):// | Yes, returning a Promise | Yes: training runs on the libuv threadpool |
litsea-php | Bytes, path, file://, http(s):// | Yes, blocking | No: a token takes effect only if cancelled before train() |
litsea-ruby | Bytes, path, file://, http(s):// | Yes | Yes: the GVL is released |
litsea-wasm | Bytes only | No | — |
Design principles
These apply to every binding.
Models are never embedded
A binding package ships code only. The caller supplies a model as raw bytes, a filesystem path, a file:// path, or an http(s):// URL; litsea-wasm takes raw bytes only, and the page fetches the model. The bundled models range from 86 KB to 8.3 MB each, so embedding all four languages would push a wheel or npm package past 20 MB; keeping models external also means a model can be updated without republishing the binding.
See Pre-trained Models for where to get a model.
The model kind is detected, not declared
The CLI needs --pos to know whether it is loading a segmentation model or a two-stage POS model. Bindings do not: they read the model bytes once and dispatch on the detected kind, so has_pos is a property of the loaded model rather than something the caller has to get right.
Cancellation is explicit
Training can run for a long time, and litsea’s trainers stop early when their running flag is cleared. The CLI drives that flag from a Ctrl-C handler, but a library must not install one: signal handling is process-global and the host language usually owns it already. The bindings that train therefore expose a cancellation token that the caller triggers; litsea-wasm has no training and so no token, and in PHP the token takes effect only when it is cancelled before train().
Shared logic lives in one crate
Everything that is not FFI-specific — language-name parsing, model loading and kind detection, token offsets, trainer orchestration, error categories — lives in litsea-binding-core. Each binding only maps that surface onto its host language’s types and exception model.