github huggingface/transformers.js 4.3.0

one day ago

🚀 Transformers.js v4.3 — Structured Output, New Models, WebGPU upgrade, Documentation Overhaul

This release adds structured output, three new model architectures, WebGPU support for Safari 26+, and a documentation overhaul. We also upgraded ONNX Runtime to the latest version.

What's new?

Structured output

Constrain generation to a JSON schema, JSON object, or regular expression with the experimental, dependency-free @huggingface/transformers-structured-output package in #1758.

For example, classify customer feedback into a fixed set of sentiments and topics:

import { pipeline, TextStreamer } from "@huggingface/transformers";
import { StructuredOutputProcessor } from "@huggingface/transformers-structured-output";

// Create the pipeline first so its tokenizer is available.
const generator = await pipeline(
  "text-generation",
  "onnx-community/LFM2.5-350M-ONNX",
  { dtype: "q4f16", device: "webgpu" },
);

const processor = new StructuredOutputProcessor(generator.tokenizer, {
  type: "json_schema",
  json_schema: {
    type: "object",
    properties: {
      sentiment: { enum: ["positive", "negative", "neutral"] },
      topic: { enum: ["price", "quality", "delivery", "other"] },
    },
    required: ["sentiment", "topic"],
    additionalProperties: false,
  },
});

const messages = [
  {
    role: "user",
    content: "Classify this feedback: The product is way too expensive.",
  },
];

const output = await generator(messages, {
  max_new_tokens: 512,
  do_sample: false,
  streamer: new TextStreamer(generator.tokenizer, {
    skip_prompt: true,
    skip_special_tokens: true,
  }),
  logits_processor: [processor],
});

console.log(output[0].generated_text.at(-1).content);
// {"sentiment": "negative", "topic": "price"}

Currently supports one generated sequence at a time. Set a sufficient token budget so the output can finish.

New models

Browser and storage improvements

  • Enable WebGPU for Safari 26 and above in #1700
  • Update Cross-Origin Storage to use requestFileHandle() and allow all origins by @tomayac in #1709 and #1716
  • Skip browser cache writes for non-HTTP(S) resources by @patrickkettner in #1723
  • Fix Rspack and Webpack import.meta warnings by @nico-martin in #1760

Fixes

  • Fix duplicate model file downloads when progress_callback is active by @anishesg in #1664
  • Fix Whisper progress callbacks in #1680
  • Ensure num_logits_to_keep is always set to 1 for generation in #1681
  • Fix the number of features in the Granite Speech processor in #1685
  • Restore missing global exports in #1696
  • Fix RawAudio.toBlob() to respect typed array byte offsets and lengths by @yushuosun in #1712
  • Dispose of the KV cache after Chatterbox generation by @m96-chan in #1737
  • Fix Moonshine ASR token decoding by @Mr-Neutr0n in #1738
  • Pin the WebGPU KV cache for Gemma3n and Gemma4 image-audio-text-to-text models by @shoemoney in #1757

Documentation and maintenance

  • Overhaul the documentation in #1665
  • Enable weekly Dependabot updates for GitHub Actions by #1690
  • Run tests on internal workers in #1710
  • Remove unused code and exports by @nico-martin in #1763
  • Update dependencies in #1766

New contributors

Thanks to our first-time contributors:

Full changelog: 4.2.0...4.3.0

Don't miss a new transformers.js release

NewReleases is sending notifications on new releases.