HuggingFace 0.4.1-dev.88

This is a prerelease version of HuggingFace.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package HuggingFace --version 0.4.1-dev.88
                    
NuGet\Install-Package HuggingFace -Version 0.4.1-dev.88
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HuggingFace" Version="0.4.1-dev.88" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HuggingFace" Version="0.4.1-dev.88" />
                    
Directory.Packages.props
<PackageReference Include="HuggingFace" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HuggingFace --version 0.4.1-dev.88
                    
#r "nuget: HuggingFace, 0.4.1-dev.88"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HuggingFace@0.4.1-dev.88
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HuggingFace&version=0.4.1-dev.88&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=HuggingFace&version=0.4.1-dev.88&prerelease
                    
Install as a Cake Tool

HuggingFace

Nuget package dotnet License: MIT Discord

Features

  • Fully generated C# SDK based on HuggingFace Hub, TGI and TEI OpenAPI specs using AutoSDK
  • Three typed clients: HuggingFaceClient (Hub API), HuggingFaceInferenceClient (TGI chat/completions), HuggingFaceEmbeddingClient (TEI embeddings/reranking)
  • Microsoft.Extensions.AI support: IChatClient and IEmbeddingGenerator<string, Embedding<float>>
  • All modern .NET features — nullability, trimming, NativeAOT, source-generated JSON
  • Targets net10.0

Getting Started

Installation

dotnet add package HuggingFace

Authentication

All clients require a HuggingFace API key. Get one at huggingface.co/settings/tokens.

using HuggingFace;

// Chat and completions (TGI)
using var inferenceClient = new HuggingFaceInferenceClient(apiKey);

// Embeddings, reranking, similarity (TEI)
using var embeddingClient = new HuggingFaceEmbeddingClient(apiKey);

// Hub API (model info, datasets, etc.)
using var hubClient = new HuggingFaceClient(apiKey);

Examples

Chat Completion

Send a chat message to a HuggingFace-hosted model using the Microsoft.Extensions.AI IChatClient interface.

var apiKey = GetApiKey();
using var client = new HuggingFaceInferenceClient(apiKey);
IChatClient chatClient = client;

var response = await chatClient.GetResponseAsync(
    [new ChatMessage(ChatRole.User, "Say hello in one word.")],
    new ChatOptions
    {
        ModelId = "Qwen/Qwen2.5-Coder-32B-Instruct",
        MaxOutputTokens = 32,
    });

Console.WriteLine(response.Text);

Streaming Chat Completion

Stream chat completion tokens as they are generated using the IChatClient interface.

var apiKey = GetApiKey();

using var client = new HuggingFaceInferenceClient(apiKey);
IChatClient chatClient = client;

await foreach (var update in chatClient.GetStreamingResponseAsync(
    [new ChatMessage(ChatRole.User, "Say hello in one word.")],
    new ChatOptions
    {
        ModelId = "Qwen/Qwen2.5-Coder-32B-Instruct",
        MaxOutputTokens = 32,
    }))
{
    Console.Write(update.Text);
}

Generate Embeddings

Generate text embeddings using the Microsoft.Extensions.AI IEmbeddingGenerator interface with HuggingFace TEI.

var apiKey = GetApiKey();

using var client = new HuggingFaceEmbeddingClient(apiKey);
IEmbeddingGenerator<string, Embedding<float>> generator = client;

var result = await generator.GenerateAsync(
    ["Hello world", "How are you?"],
    new EmbeddingGenerationOptions
    {
        ModelId = "sentence-transformers/all-MiniLM-L6-v2",
    });

Console.WriteLine($"Embedding dimension: {result[0].Vector.Length}");
Console.WriteLine($"Embeddings generated: {result.Count}");

Rerank Texts

Rerank a list of texts by relevance to a query using the TEI reranking endpoint.

var apiKey = GetApiKey();

using var client = new HuggingFaceEmbeddingClient(apiKey);

var results = await client.RerankAsync(
    query: "What is Deep Learning?",
    texts:
    [
        "Deep Learning is a subset of Machine Learning.",
        "The weather is sunny today.",
        "Neural networks are inspired by the human brain.",
    ],
    returnText: true);

foreach (var rank in results.OrderByDescending(r => r.Score))
{
    Console.WriteLine($"[{rank.Index}] score={rank.Score:F4} text={rank.Text}");
}

Similarity Scoring

Compute cosine similarity between a source sentence and a list of candidate sentences.

var apiKey = GetApiKey();

using var client = new HuggingFaceEmbeddingClient(apiKey);

var scores = await client.SimilarityAsync(
    inputs: new SimilarityInput
    {
        SourceSentence = "What is Deep Learning?",
        Sentences =
        [
            "Deep Learning is a subset of Machine Learning.",
            "The weather is sunny today.",
            "Neural networks are inspired by the human brain.",
        ],
    });

for (var i = 0; i < scores.Count; i++)
{
    Console.WriteLine($"[{i}] similarity={scores[i]:F4}");
}

Tokenize Text

Tokenize text into tokens using the TEI tokenization endpoint.

var apiKey = GetApiKey();

using var client = new HuggingFaceEmbeddingClient(apiKey);

var tokens = await client.TokenizeAsync(
    inputs: new TokenizeInput("Hello world"),
    addSpecialTokens: true);

foreach (var token in tokens[0])
{
    Console.WriteLine($"id={token.Id} text=\"{token.Text}\" special={token.Special}");
}

Sparse Embeddings

Generate sparse embeddings for text using the TEI sparse embedding endpoint.

var apiKey = GetApiKey();

using var client = new HuggingFaceEmbeddingClient(apiKey);

var sparseEmbeddings = await client.EmbedSparseAsync(
    inputs: new Input("Hello world"));

foreach (var sv in sparseEmbeddings[0].Take(5))
{
    Console.WriteLine($"index={sv.Index} value={sv.Value:F4}");
}

Native Embeddings

Generate dense embeddings using the TEI-native embed endpoint with normalization control.

var apiKey = GetApiKey();

using var client = new HuggingFaceEmbeddingClient(apiKey);

var embeddings = await client.EmbedAsync(
    inputs: new Input("Hello world"),
    normalize: true);

Console.WriteLine($"Embedding dimension: {embeddings[0].Count}");

Decode Tokens

Tokenize text and decode it back using the TEI tokenization and decode endpoints.

var apiKey = GetApiKey();

using var client = new HuggingFaceEmbeddingClient(apiKey);

// Tokenize text into token IDs.
var tokens = await client.TokenizeAsync(
    inputs: new TokenizeInput("Hello world"),
    addSpecialTokens: false);

var tokenIds = tokens[0].Select(t => t.Id).ToList();
Console.WriteLine($"Token IDs: [{string.Join(", ", tokenIds)}]");

// Decode token IDs back to text.
var decoded = await client.DecodeAsync(
    ids: new InputIds(value1: tokenIds, value2: null),
    skipSpecialTokens: true);

Console.WriteLine($"Decoded: {decoded[0]}");

Who Am I

Get the authenticated user's account information using the Hub API.

var apiKey = GetApiKey();
using var client = new HuggingFaceClient(apiKey);

var response = await client.Auth.GetWhoamiV2Async();

Console.WriteLine($"User: {response}");

List recently trending models, datasets, and spaces on the HuggingFace Hub.

var apiKey = GetApiKey();
using var client = new HuggingFaceClient(apiKey);

var response = await client.Models.GetTrendingAsync(limit: 5);

foreach (var item in response.RecentlyTrending)
{
    var id = item.Value1?.RepoData?.Id ?? item.Value2?.RepoData?.Id ?? item.Value3?.RepoData?.Id;
    var author = item.Value1?.RepoData?.Author ?? item.Value2?.RepoData?.Author ?? item.Value3?.RepoData?.Author;
    if (id is not null)
    {
        Console.WriteLine($"{id} by {author}");
    }
}

List Model Tags

List available model tags grouped by type from the HuggingFace Hub.

var apiKey = GetApiKey();
using var client = new HuggingFaceClient(apiKey);

var tags = await client.Models.GetModelsTagsByTypeAsync();

foreach (var (tagType, tagList) in tags)
{
    Console.WriteLine($"{tagType}: {tagList.Count} tags");
}

Support

Priority place for bugs: https://github.com/tryAGI/HuggingFace/issues
Priority place for ideas and general questions: https://github.com/tryAGI/HuggingFace/discussions
Discord: https://discord.gg/Ca2xhfBf3v

OpenAPI specs

Acknowledgments

JetBrains logo

This project is supported by JetBrains through the Open Source Support Program.

CodeRabbit logo

This project is supported by CodeRabbit through the Open Source Support Program.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on HuggingFace:

Package Downloads
LangChain.Providers.HuggingFace

HuggingFace API LLM and Chat model provider.

HPD-Agent.Providers.HuggingFace

HuggingFace Inference API provider for HPD-Agent

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.1-dev.94 0 3/18/2026
0.4.1-dev.93 0 3/18/2026
0.4.1-dev.92 0 3/18/2026
0.4.1-dev.91 0 3/18/2026
0.4.1-dev.90 0 3/18/2026
0.4.1-dev.89 0 3/18/2026
0.4.1-dev.88 0 3/18/2026
0.4.1-dev.87 0 3/18/2026
0.4.1-dev.86 0 3/18/2026
0.4.1-dev.85 0 3/18/2026
0.4.1-dev.84 0 3/18/2026
0.4.1-dev.83 0 3/18/2026
0.4.1-dev.82 0 3/18/2026
0.4.1-dev.81 3 3/17/2026
0.4.1-dev.80 3 3/17/2026
0.4.1-dev.79 4 3/17/2026
0.4.1-dev.78 8 3/17/2026
0.4.1-dev.77 10 3/17/2026
0.4.1-dev.76 19 3/17/2026
0.4.0 12,617 11/22/2024
Loading failed