# Transformers.js v4.3: Structured Output in the browser

## Executive summary

Transformers.js v4.3 introduces structured output capabilities via the new `@huggingface/transformers-structured-output` package. This feature allows developers to force Large Language Models (LLMs) to adhere to strict JSON Schemas or regular expressions directly in the browser. This solves the common problem of unreliable LLM output formats (e.g., markdown fences, inconsistent JSON structure) by implementing constrained decoding, which hooks into the model's logits processor to filter out non-compliant tokens.

## Key takeaways

- Structured Output Capability: The new package allows users to force an LLM to follow an exact JSON Schema or regex, ensuring predictable and reliable data extraction without needing to parse markdown or assume structure.
- Technical Implementation: Constrained decoding works by hooking into the `logits processor` within Transformers.js. This allows the system to validate potential next tokens against the defined constraints (JSON Schema or regex) and strip out non-compliant tokens.
- Performance Improvement: The initial prototype used a WASM wrapper around Rust's `llguidance`. This was successfully rewritten into a pure JavaScript implementation, eliminating the large 1MB WASM overhead while maintaining minimal performance overhead across models like Gemma 4, Granite, and LFM 2.5.

## Technical details

- Constrained Decoding: This technique, implemented by the new `@huggingface/transformers-structured-output` package, forces the LLM to generate text that strictly conforms to a provided JSON Schema or regex pattern, ensuring reliable data structure.
- Integration Point: The structured output processor hooks into the `logits processor` in Transformers.js, which is the final step of token generation. This allows the system to validate and filter all possible next tokens before they are outputted.
- Architecture Update: The repository structure was updated with a new `packages` folder, enabling the distribution of new add-on packages (like structured output) that function as plugins to the core Transformers.js library.

## Practical implications

- Build engineers can now integrate LLM-generated data into CI/CD pipelines with high confidence, as the output structure is guaranteed by the constrained decoding mechanism.
- The ability to enforce JSON Schema or regex output makes LLMs suitable for reliable data extraction tasks (e.g., generating structured recipes or personal details) that previously required fragile post-processing.
- The pure JavaScript implementation of the underlying logic improves deployment size and portability for web-based applications.

## Topics

Transformers.js, LLMs, Structured Output, Constrained Decoding, JSON Schema, Regex, JavaScript, WASM, Release notes, NPM (Transformers.js), Structured output package

Source: https://www.youtube.com/watch?v=InnVG6pOPpM
