std.overview
The Sema standard library is a small set of neurosymbolic building blocks —
calibrated confidence, memoization, structured reports, agentic loops, usage
accounting, provenance, and collection helpers. What makes it unusual is what it
is made of: the standard library is written in Sema. Every module you see
here has a .sema source file that you can read, and the logic in it — a Beta
prior, a token price, a render layout — is expressed in the language, not buried
in the runtime.
How it works
Section titled “How it works”Each module lives in stdlib/sema/<module>.sema and ships embedded in the
compiler (injected via crates/sema-runtime/src/stdlib.rs). There is nothing
to install. Any project can bring a module in with an explicit import:
from std.belief import Belief, priorfrom std.cache import memoizefrom std.document import Report, renderfrom std.agent_loop import loop_untilSema draws a clean line between two kinds of dependency (spec §5.35). Effect
capabilities — fs, net, model, memory, … — are authorized by the
!{...} effect row on a function and stay ambient. Standard-library modules
are APIs you call, so they must be named in an explicit import, exactly as in
Python. Using a std module without importing it is a NameError with an
actionable hint, never a silent fallback.
Why it exists
Section titled “Why it exists”Each module replaces code that AI applications otherwise hand-roll again and
again — a bespoke confidence tracker, a pickle-path cache, regex repair of
freeform model markdown, a 300-line while loop, (result, usage) tuple
threading, citation-id bookkeeping. Standardizing these as small, verified,
readable Sema components makes the neurosymbolic parts of a program legible and
reusable instead of copy-pasted.
The seven modules
Section titled “The seven modules”| Module | What it gives you | Narrative | Generated API |
|---|---|---|---|
belief | Beta-Bernoulli calibrated confidence | std.belief | API |
cache | Memoization decorators (in-run + on-disk) | std.cache | API |
collections | Small pure list/string helpers | std.collections | API |
document | Typed report IR + deterministic render | std.document | API |
agent_loop | The agentic loop as a combinator | std.agent_loop | API |
usage | Model usage accounting + pricing | std.usage | API |
provenance | Citation-id mapping + rewrite | std.provenance | API |
Narrative pages vs. the API reference
Section titled “Narrative pages vs. the API reference”Every module has two pages. The narrative page (this section, /stdlib/…)
motivates the module, shows the key types and functions with usage, and gives you
the import line. The generated API reference (/reference/stdlib-api/…) is
emitted by sema doc directly from the .sema source — it is the exact,
never-drifting signature list (params, return types, effect rows, struct fields).
Read the narrative to learn the module; consult the API for the precise surface.