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)
cargo install stof-cli  # command line: stof run, stof test

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.4

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.4
File Size Uploaded Provenance Actions
stof-0.11.4.tar.gz 681.8 kB With provenance Details for stof-0.11.4.tar.gz

Built distributions (wheels)

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

Total release size: 43.1 MB

Release files / stof-0.11.4.tar.gz

Download URL stof-0.11.4.tar.gz
Size 681.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7893b357fa0aea5e03e8d790323bf561bb14b2fd994d9a70479e727342a2317f
BLAKE2b-256 checksum
How to use checksums
49499386c5a778694270a1783b4d0a07c44393abb6037496f401294b6eb61e07
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 16, 2026.

Transparency log

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

Download URL stof-0.11.4-cp38-abi3-win_amd64.whl
Size 8.5 MB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
ddef9a2fbb2fcc937295688a66b8a616a6e9f3e77753d9cd9e8416650f7123b0
BLAKE2b-256 checksum
How to use checksums
2ed865a956b76ea64636a104206e86cb07679dfa2a4bd86e0e9cb7bafa4cdad7
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 16, 2026.

Transparency log

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

Download URL stof-0.11.4-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
2443b18c176e941ab9068259cfeb68f30a7767be78476c7f6a12a3248748f1d1
BLAKE2b-256 checksum
How to use checksums
ff9e8b25ae35b623ca2b13bbbac3836b6a091f7805019081cd17d027d401b85c
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 16, 2026.

Transparency log

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

Download URL stof-0.11.4-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
569fb96fdc8afeb1a02463cfe94937dbb9471d90188f1e87aa976f3fc8d3a94d
BLAKE2b-256 checksum
How to use checksums
62e70a3039e596b1d6cfffa82ee89ac473ff93ea77b0ec0edb3dac428d6a42db
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 16, 2026.

Transparency log

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

Download URL stof-0.11.4-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
fce993aa68f672981f73790780081533e959fad8ba1502eb6cc360c4ce5ef443
BLAKE2b-256 checksum
How to use checksums
fe67cb308f9fa384643e9cc2fcdb168bf6058528043b33f4dab21bb8a5919b37
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 16, 2026.

Transparency log

Security for stof 0.11.4

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.4.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 16, 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.4 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