Transformers.js v4.3 Released: Structured Output in the Browser — A Deep Dive

Transformers.js v4.3 brings constrained decoding to the browser with zero-overhead pure JS structured output.
Transformers.js v4.3's headline feature is **structured output**, enabling browser-side language models to strictly follow developer-defined JSON schemas or regex patterns, eliminating the parsing risks caused by unpredictable model output formats. Built on **constrained decoding**, it uses a Logits Processor hook to remove invalid token candidates before each sampling step. The team started with a WebAssembly wrapper around the Rust library LLGuidance, then rewrote it entirely in pure JavaScript, eliminating a 1MB WASM dependency while maintaining near-zero performance overhead. Architecturally, the feature ships as an independent plugin package, pioneering a new extensibility paradigm for Transformers.js with minimal changes to the core library.
Hugging Face's Transformers.js has landed its v4.3 update, arriving some time after the previous 4.2 release. The headline feature this time is Structured Output — a capability that lets browser-side language models generate content that strictly conforms to a predefined format. The team also completely rewrote the documentation generation system and merged a large number of community-contributed features and fixes.
Why Structured Output Matters
Without constraints, getting a language model to reliably produce JSON is inherently unpredictable. Take a common scenario: extracting personal information from user input. Given a system prompt and a user prompt, the model can indeed produce a Markdown code block containing JSON, and a developer can strip the backticks and parse out the object.
The problem is you can't guarantee it will work every time. The model might change the JSON structure, switch to XML or plain Markdown, or produce something entirely unexpected. For use cases where model output feeds directly into application logic, this unpredictability is a dealbreaker. Structured output exists specifically to solve this problem — it lets developers force the model to follow a precise schema.
Constrained Decoding: The Technical Core of Structured Output
The technical term behind structured output is Constrained Decoding. To understand it, you first need to understand how language models generate text: LLMs produce text one token at a time, and at each step they return a list of candidate tokens along with a probability score for each one being the next output.

Take the query "when was Danny Boyle born" — the next token candidates might include born, birth, date, birthday, given, and so on. The core idea of constrained decoding is: instead of letting the model freely pick from all candidates, invalid tokens are eliminated in advance based on the constraints. In the diagram, born, birth, and birthday are marked red (disallowed), so the model can only select date; on the next step, of is permitted and generation continues. Through this mechanism, the model's output is firmly anchored within the expected structure.
From an implementation perspective, constrained decoding relies on a mathematical structure called a Finite State Machine (FSM) or pushdown automaton to represent the complete set of valid outputs. For a JSON schema, the system pre-compiles the schema into a state transition graph: the currently generated string occupies some state, each candidate token attempts to trigger a state transition, and if the transition leads to a valid state the token is kept — otherwise its logit score is set to negative infinity (effectively removing it from the candidate list). This means the computational cost of constraint validation scales with the vocabulary size (typically tens of thousands to hundreds of thousands of tokens) and must repeat for every token generated, which is precisely why performance optimization is so critical.
A New Plugin-Based Package Architecture
The way structured output was implemented this time is worth paying attention to. The team reorganized the GitHub repository with the v4 release, adding a packages directory. For a long time, this directory contained only the main Transformers package — but now it also hosts a Transformers Structured Output package.

The significance of this restructuring is that the team can now release new features as independent plugins or add-ons. The structured output package is the first such example. Even more elegant is the fact that implementing this feature required almost no changes to the Transformers.js core — because it already supported a Logits Processor mechanism.
A Logits Processor is an interface that hooks into the final step of token generation (i.e., the decoding step). At decode time, all possible next tokens are present, and developers can intervene to remove unwanted tokens. The usage is straightforward: import the JSON processor from the Structured Output package, pass in the tokenizer and the desired JSON schema, then hand it to the Logits Processor.
Running the same example, the model no longer generates Markdown — it starts directly with a curly brace and outputs pure JSON, using full name instead of name because the schema explicitly requires that property name. In addition to JSON schema, the package also supports regular expression (regex) constraints, which can force the model to produce specific formats like person=Bob Johnson, age=25.
Logits are the raw score vectors output by the model before softmax normalization, with a dimension equal to the vocabulary size. A Logits Processor is a hook that intervenes on this vector: it receives the current sequence of generated tokens and the raw logits vector, returns a modified logits vector, and the decoder then performs softmax sampling. Because the intervention happens before sampling, any token set to negative infinity will approach zero probability after normalization, achieving hard exclusion. This mechanism isn't unique to Transformers.js — the HuggingFace Python Transformers library and several inference frameworks provide similar interfaces, with structured output being just one of many applications.
A Real-World Use Case: Reliable Recipe Generation
The author demonstrates a recipe generator demo built on structured output. Upon clicking generate, the model strictly outputs according to the preset JSON structure, including fields like title, description, and ingredients.

With this reliable JSON in hand, the application can parse it directly and populate it into the UI layout. The entire workflow is feasible precisely because developers know in advance and can trust the exact structure the model will output — something ordinary generation cannot guarantee.
From WebAssembly to Pure JS: The Performance Journey
The technical challenge of structured output lies in needing a mechanism that continuously validates whether the generated string conforms to the constraints. At every generation step, all candidate tokens must be validated against those constraints.
The community already had a library called LLGuidance that had long handled this role — vLLM, llama.cpp, and the browser-side Prompt API all build on it. The team's first prototype was a WebAssembly wrapper around LLGuidance's Rust implementation, with an adapter to hook into Transformers.js's generation pipeline. This approach worked, but was slow and required downloading a 1MB WebAssembly file — far from ideal.

The next step: with extensive help from coding agents, the team completely reimplemented LLGuidance in pure JavaScript, entirely eliminating that 1MB WASM file. The team also invested heavily in performance — because each new token requires regenerating a mask of allowed next tokens and comparing it against the model's generated token.
After multiple iterations, the author tested across different models — Gemma, Granite, LFM 2.5 — and different tokenizers. Looking at the tokens-per-second comparisons, enabling constrained decoding introduces almost no additional overhead, with only occasional slight slowdowns of negligible magnitude. The end result is a structured output package that is extremely fast and compatible with any tokenizer.
WebAssembly (WASM) is a binary instruction format that can run in browsers at near-native speeds, commonly used to port high-performance libraries written in C/C++/Rust to the web environment. Despite fast execution, WASM modules have notable drawbacks: they require additional network downloads (1MB in this case), data transfer between WASM and JavaScript incurs serialization overhead (crossing the "WASM boundary" is costly), and they may be blocked from loading in certain restricted browser environments. This explains why the team ultimately chose to rewrite in pure JavaScript — in the structured output scenario, the core logic of constraint validation is not numerically intensive, JavaScript engine JIT compilation provides sufficient performance, while completely eliminating the engineering complexity and loading burden that WASM introduces.
Summary
Transformers.js v4.3 brings constrained decoding — a capability previously only reliably achievable server-side — into the browser via structured output. Two constraint modes (JSON schema and regular expressions), a plugin-based package architecture, and a near-zero-overhead pure JS implementation together form the core value of this update. Complete schema support documentation is already available in the repository, and developers can start experimenting right away.
Related articles

vLLM v0.30.0rc1 Released: Isolates FlashInfer BF16 Autotuning Logic
vLLM v0.30.0rc1 release candidate fixes FlashInfer BF16 autotuning isolation (PR #57285). Learn the technical background and its impact on inference deployment.

Comp AI Raises $34M Series A, Bets on Agentic Security Compliance
Comp AI raises $34M Series A led by Roo Capital and Grand Ventures, betting on "continuously agentic" AI to transform compliance from periodic audits into real-time monitoring.

MIT Technology Review's 35 Innovators Under 35: A Climate Tech Edition Explained
MIT Technology Review's latest 35 Innovators Under 35 list focuses on climate tech, spotlighting nine young global innovators. Here's what the list means and why it matters.