Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Tools · Const

Hugging Face Transformers

Hugging Face Transformers is represented in the Vercel AI SDK by the Hugging Face provider. Use its provider instance to create language models for text generation and streaming.

How it works

The Hugging Face provider connects AI SDK generation functions to models exposed through Hugging Face Inference Providers. Import the default provider instance, select a model by identifier, and pass that model to functions such as generateText or streamText. A separately configured provider can be created when a custom API key is needed.

Syntax

huggingFace(modelId)

Parameters

NameTypeDescription
modelIdstringThe Hugging Face model identifier, such as meta-llama/Llama-3.1-8B-Instruct.

Returns

A language model instance usable with AI SDK generation functions.

Key points

  • Install and import the provider from the dedicated @ai-sdk/huggingface package.
  • Use the built-in huggingFace instance for standard model selection.
  • Call the provider with a model identifier to obtain a language model for AI SDK functions.
  • Use the same provider-created model with streamText when incremental output is required.
  • Choose model identifiers from Hugging Face's available inference model catalog.

Common mistakes

  • Configure the Hugging Face API key through the provider or the documented environment-variable setup before making requests.
  • Provider-specific settings must be supplied under the huggingface providerOptions namespace.

Example: Generate text with Hugging Face

import { huggingFace } from '@ai-sdk/huggingface';
import { generateText } from 'ai';

const { text } = await generateText({
  model: huggingFace('meta-llama/Llama-3.1-8B-Instruct'),
  prompt: 'Explain embeddings in one sentence.',
});

console.log(text);

Generates text with a Hugging Face model through the AI SDK.

When to use it

Text generation

Generate a one-off completion or response from a Hugging Face-hosted language model.

Streaming responses

Deliver model output incrementally to a user interface.

Custom provider setup

Use a custom Hugging Face API key or provider configuration.

  • createHuggingFace
  • generateText
  • streamText
  • providerOptions.huggingface

Source: Hugging Face Provider - AI SDK. Checked 2026-10-15.