Topic

Regex

All digests tagged Regex

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

· 11:08

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

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

  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.

Watch on YouTube Full article