Skip to main content

Stof

JSON that runs.

Stof is JSON with functions. Put the logic next to the data, and your app runs the document in a sandbox that can only touch what you hand it. In Rust, JavaScript, and Python.

Try it in your browser · Docs · Discord

JSON, plus a function

// acme.stof: one customer's pricing
{
    "customer": "acme",
    "plan": { "rate": 0.02, "included": 1000 },

    fn price(units: int) -> float {
        const billable = units - self.plan.included;
        if (billable <= 0) return 0;
        billable * self.plan.rate
    }
}
import { StofDoc } from '@formata/stof';

const doc = await StofDoc.new(source);   // acme.stof
await doc.call('price', 5000);           // 80
from pystof import Doc

doc = Doc()
doc.parse(source)                         # acme.stof
doc.call('price', 5000)                   # 80.0

Any JSON is already a Stof document. Add a function where the logic belongs, and your app calls it the way it reads a field. The document runs inside your process: no server, no container, and it can't reach anything you didn't give it.

What else a document can hold

// server.stof
ms timeout: 5min + 30s        // 330000
MiB memory: 2GB               // about 1907.35

#[type]
Server: {
    #[schema((v: int): bool => v > 0 && v < 65536)]
    int port: 8080
}

config: { port: 70000 }

fn valid() -> bool { <Server>.schemafy(self.config) }

#[test]
fn config_is_valid() { assert(self.valid()); }
  • Units are part of the number, and math between them converts. No more "timeout": "5m" for someone to parse later.
  • Types carry rules for their fields. Point one at any object to check it: this config fails, because 70000 isn't a port.
  • Tests live next to what they check. stof test server.stof fails here until the port is fixed.

There's more: destructuring, async, attributes, and imports and exports for JSON, YAML, TOML, and compact binary.

It can only touch what you hand it

const doc = await StofDoc.new(`
    fn greet(name: str) -> str { Host::greet(name) }
    fn leak() -> str { fs::read('/etc/passwd') }
`);

doc.lib('Host', 'greet', (name) => `hello, ${name}`);

await doc.call('greet', 'world');   // 'hello, world'
await doc.call('leak');
// throws: 'fs::read' isn't available: this host hasn't given documents file system access

The same in Python:

doc.lib('Host', 'greet', lambda name: f'hello, {name}')

doc.call('greet', 'world')   # 'hello, world'
doc.call('leak')             # raises ValueError: 'fs::read' isn't available: ...

A document sees itself and the functions your app registers. There's no file system, network, or environment unless you turn them on, and network access can be limited to the hosts you list (doc.allowHttp(['api.example.com']) in JavaScript, doc.allow_http_hosts(['api.example.com']) in Python). Every call has a time and memory limit. You can also seal the parts you trust, so nothing can change them, and confine code from somewhere else so it sees only what you list. See Security and the Sandbox.

What it's for

  • Logic someone else writes. A model, a user, or a customer gives you rules, filters, transforms, scoring, or agent tools. Check them, test them, and run them in-process on every request.
  • Behavior per customer. One document per customer holds their rules, limits, and state. An agent can edit it, a person reviews a one-line diff, and your app behaves differently for that customer with no deploy.
  • Config that checks itself. Validate and compute values in the config, and still export plain JSON, YAML, or TOML.
  • The rules engine or DSL you were about to build. Describe your domain in Stof instead.
  • Logic that has to run everywhere. Send a document, functions included, as text or compact binary, and get the same answer in the browser, on a server, or in a worker.

Each one has a runnable example in the playground. Use something else for your own application code, heavy compute, or long-running services: write those in your main language and give them to documents as functions.

Stof is the engine behind Limitr, where every customer's pricing is a Stof document, evaluated in-process on every call.

Install

npm i @formata/stof     # JavaScript / TypeScript
cargo add stof          # Rust
pip install stof        # Python (import pystof)

The stof command line (stof run, stof test, and the language server) is prebuilt for macOS, Linux, and Windows. See stof-cli, or on macOS and Linux:

curl --proto '=https' --tlsv1.2 -LsSf https://github.com/dev-formata-io/stof-cli/releases/latest/download/stof-cli-installer.sh | sh

TypeScript

import { stofAsync } from '@formata/stof';

const doc = await stofAsync`
    "name": "Stof"
    fn hello() -> str { \`Hello, \${self.name}!\` }
`;
console.log(await doc.call('hello')); // Hello, Stof!

Python

from pystof import Doc

doc = Doc()
doc.parse('rate: 2\nfn total(units: int) -> float { units * self.rate }')
print(doc.call('total', 21))  # 42.0

Rust

use stof::model::Graph;
use stof::runtime::{Runtime, Val};

let mut graph = Graph::default();
graph.parse_stof_src("rate: 2\nfn total(units: int) -> float { units * self.rate }", None, Default::default())?;
let total = Runtime::call(&mut graph, "total", vec![Val::from(21i64)])?; // 42

Teaching a model Stof: give it content/SKILL.md, a short guide to the language, with the library references in content/stof.zip. The npm package ships the same skill in node_modules/@formata/stof/skill/stof.

FAQ

Is it safe to run a document a model wrote? That's what it's built for. A document can only see itself and the functions your app gives it. File system, environment, file imports, and network are off by default in every language, and network access never reaches localhost or your private network unless you list those hosts. Each call has a time limit (2 minutes by default) and a memory limit (256 MB by default), so a runaway document fails its call instead of your app. For code from somewhere else, parse it as untrusted (nothing runs while it's parsed) and seal it confined to what you list. The stof CLI turns access on, since it runs your own files.

Can models write Stof? Yes. It reads like JSON and JavaScript, which every model already knows, and errors give the exact line and a plain explanation, so a model can fix its own mistakes. The skill helps.

Why not have the model write Python and run it in a container? You can, but that's infrastructure, startup time, and a lot of access to lock down, and what a person reviews is a program. Stof runs inside your process, starts instantly, has no access by default, and a change is a diff to a document.

Why not Lua? Lua is a great language for scripting your own app. Stof is for documents that carry logic. With Lua, the data lives somewhere else, each host language needs its own Lua binding, and schemas, units, tests, and the sandbox are yours to build. A Stof document holds the data and its logic together, runs the same in Rust, JavaScript, and Python, and starts with no access. Any JSON is already a Stof document, so there's nothing to migrate.

Why not CEL or JSONLogic? Expression languages are safe because they're small: one expression, no functions of your own, no state. That's enough for a single condition, and it runs out quickly for pricing, scoring, or an agent's tools. Stof has functions, types, and tests, inside the same kind of sandbox.

Why not WebAssembly? WebAssembly needs a compiler toolchain, and its binaries are large to send. Stof is readable, small, and travels with the data it works on. (The Stof runtime itself is compiled to WebAssembly for browsers.)

How fast is it? A small document parses and runs in under a millisecond, and calls after that take microseconds.

Do I have to replace JSON, YAML, or TOML? No. Stof imports and exports them, so you can add logic to the data you already have.

Learn more

License

Apache 2.0. See LICENSE.

Metadata

Release files for stof 0.11.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stof 0.11.6
File Size Uploaded Provenance Actions
stof-0.11.6.tar.gz 700.5 kB With provenance Details for stof-0.11.6.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for stof 0.11.6
File Actions
stof-0.11.6-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details for stof-0.11.6-cp38-abi3-win_amd64.whl
stof-0.11.6-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.8 abi3 Linux glibc 2.17+ x86-64 Details for stof-0.11.6-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
stof-0.11.6-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.8 abi3 Linux glibc 2.17+ ARM64 Details for stof-0.11.6-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
stof-0.11.6-cp38-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl CPython 3.8 abi3 macOS 10.12+ x86-64, macOS 10.12+ universal2 (ARM64, x86-64), macOS 11.0+ ARM64 Details for stof-0.11.6-cp38-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl

Total release size: 43.3 MB

Release files / stof-0.11.6.tar.gz

Download URL stof-0.11.6.tar.gz
Size 700.5 kB
Tags Source
SHA-256 checksum
How to use checksums
514f8e0f9b58ab937267a9b0cf83e26b2d5f2258f5be46ddbe7316073b77b50c
BLAKE2b-256 checksum
How to use checksums
6656c22ae20cd5df1fd431abe0b88244d506a2da1dbf6eddac34ec33a2daba64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

Release files / stof-0.11.6-cp38-abi3-win_amd64.whl

Download URL stof-0.11.6-cp38-abi3-win_amd64.whl
Size 8.5 MB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
879e8a0a4ee9b7d136b2a29d8a1f9b9c26bdb06649f830fe71cf7139e324d797
BLAKE2b-256 checksum
How to use checksums
bf1a9b33e8d6226791bab0be1ad35267d3be0c0982b142fcaff6c75a6e3b0d02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

Release files / stof-0.11.6-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL stof-0.11.6-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 8.9 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
79322323c5ecafe0e896c2abf88ad62c26c0defcdbc6d86658718d5e2210076f
BLAKE2b-256 checksum
How to use checksums
169699e8050f1ecbc9467431b4ad4ab9e9098607f505ec00a685f1b391c48ddb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

Release files / stof-0.11.6-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL stof-0.11.6-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 8.6 MB
Tags CPython 3.8 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
79e84554d63db7b9d3141f0bcfd30520b02eff1f069ad451e2b4725641d281cf
BLAKE2b-256 checksum
How to use checksums
4a7b7681fc5111c64d37dc995a0dd5e61fd4c7e200de6f169dc5e9296d7ed2e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

Release files / stof-0.11.6-cp38-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl

Download URL stof-0.11.6-cp38-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl
Size 16.5 MB
Tags CPython 3.8 abi3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
3c0c377bdc3aa65ca9f7e2baad678720ea3c54e38df913977c394994eeba6d81
BLAKE2b-256 checksum
How to use checksums
96e133532d0c275f31a911716102788a0e732af048b79df128ecb48dc63f8038
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

Security for stof 0.11.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 5 files in this release have been uploaded with provenance metadata.

Select from 5 files

Attestation for stof-0.11.6.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.11.6 This release

5 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page