Render Typst while
the model writes it
typstream heals every streamed prefix with a port of Typst's own parser, compiles it in a worker and redraws only what changed. Your users watch a typeset worksheet arrive, not a spinner.
Ported from Typst 0.15.1's parser and checked against it on 20,000 random streams.
“A handout explaining the Pythagorean theorem, with a labelled right triangle and a worked example.”
These are recorded streams. Try your own request in the AI SDK example
- of prefixes compileA faithful port of Typst's own parser closes what the model left open.
- 99.93%
- incremental compileTypst 0.15.1 as WebAssembly, in a worker, redrawing only what changed.
- ~5 ms
- healer, gzippedSmall enough to heal every token well inside the main thread's budget.
- 15.3 KB
- blank framesA fallback ladder keeps the last good render, or isolates the one block that fails.
- 0
The problem
One unclosed bracket
and Typst won't compile
Markdown degrades gracefully. Typst is a compiled language, which is why worksheets look so much better in it, and why half of one fails outright. So most apps show raw text or a spinner until the model is done.
typstream closes what's open, holds back what can't be closed yet, and hands Typst a prefix that compiles. It's a port of Typst's own parser, tested against the real one on every prefix.
How healing worksFrom the first token to the last, your users read a real document, never a blank page.
What you get
Everything between
the model and the page
Healing, scheduling, compiling and rendering, with the defaults an app wants. You pass the text so far.
Never a blank screen
Fixers repair what parses but doesn't compile yet. If a block still fails, only that block is isolated and the last good render stays up.
5repair layersSmooth on the main thread
Healing takes 0.2 ms per token at p95, even at 50 KB. Compiling runs in a worker, so the page keeps scrolling while a document streams in.
0.2 msper token, p95Print faithful or reflowing
Pages with a selectable text layer, or semantic HTML and MathML that reflow and follow your theme. Only what changed is redrawn.
2render targetsSafe with untrusted output
No network, no files you didn't pass, a sanitized DOM and a watchdog for runaway loops, all under a strict Content Security Policy.
3browsers checkedMade for teachers' documents
Questions, multiple choice, rubrics, and answer keys only the teacher's copy shows. CeTZ diagrams, Arabic and Bengali work out of the box.
7Typst packages built in
How it works
One component.
Three steps on every token.
- 01
Heal
The text so far is repaired with a port of Typst's parser. Settled text is never touched, so each new token only costs the tail.
- 02
Compile
A shared worker compiles it with Typst 0.15.1 in WebAssembly, paced so a fast model never floods it. The finished document compiles strictly.
- 03
Render
Only changed pages are redrawn, new words fade in, and a caret marks where the model is writing. If one block breaks, only that block falls back.
import { Typstream } from "typstream";import { edu } from "@typstream/edu";export function Answer({ text, streaming }) {return (<Typstreamsource={text}isStreaming={streaming}target="paged"plugins={[edu]}/>);}
Proof
Tested against real Typst,
not against guesses about it
Healing is checked against Typst's own parser, and the engine against the native typst command line. Below are three of the Claude streams it records and replays, rendered here by the same engine.
- recorded model streams
- 141
- random streams a run
- 20,000
- browser engines per PR
- 3
FAQ
Questions before
you install
Something missing? Open an issue on GitHub and we'll answer it there.
Ask on GitHubWhich models does it work with?
Any model that writes Typst. typstream reads text, not an SDK, so Claude, GPT and local models all work. The teaching prompt kit is measured on recorded Claude streams.
Do I need to run a server?
No. The engine runs in a web worker in your users' browser. The same engine runs in Node when you want to validate documents or render PDFs on the server.
How much does it add to my bundle?
The React component is about 20 KB gzipped. Typst itself is a WebAssembly file under 6.3 MB compressed, loaded in a worker and cached after the first visit.
What if the finished document has an error?
When the stream ends the document is compiled strictly. Diagnostics come back with source positions, so you can show them or send them to the model to fix.
Can documents use Typst packages?
Yes, from an allowlist: CeTZ, fletcher, cetz-plot, physica, unify, chemformula and alchemist. They're vendored with integrity hashes, so nothing is fetched at render time.
Is it safe to render what a model writes?
Documents get no network and no files you didn't pass. Output goes through a sanitizer, a watchdog stops runaway loops, and it all runs under a strict Content Security Policy.
Does it work without React?
Yes. mountTypstream from @typstream/dom renders into any element, with or without a framework. The React component is a thin layer over it.
Can the model edit a document it already wrote?
Yes. @typstream/ai streams edits that quote the text they replace and applies them in place as they arrive, with a highlight and undo.
Show the document
while it's being written
Install one package, pass it the text so far,
and your users watch a typeset page arrive.
- No server to run
- No account or API key
- Runs in your users' browser