Hugging Face

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

Published 2026-09-16 · Duration 11:08

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.

Download summary

Key takeaways

  1. Structured Output Capability 1:59

    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.

  2. Technical Implementation 3:48

    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.

  3. 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 218s

    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 325s

    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 337s

    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.

Mentioned resources

Channel & topics

Watch on YouTube · Back to latest

This independent, AI-assisted summary is provided for commentary and informational purposes. It may contain errors or omit important context. Please watch the original video for the creator's complete presentation. Video, thumbnail, and related copyrights belong to their respective owners.