HuggingFace 0.4.2

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package HuggingFace --version 0.4.2
 
NuGet\Install-Package HuggingFace -Version 0.4.2
 
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.2" />
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HuggingFace" Version="0.4.2" />
 
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.2
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HuggingFace, 0.4.2"
 
#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.2
 
#: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.2
 
Install as a Cake Addin
#tool nuget:?package=HuggingFace&version=0.4.2
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

using var client = new HuggingFaceClient(apiKey);
var tags = await client.Models.GetModelsTagsByTypeAsync();
foreach (var (tagType, tagList) in tags)
{
 Console.WriteLine($"{tagType}: {tagList.Count} tags");
}

Search Models

Search for models, datasets, and spaces on the HuggingFace Hub using quicksearch.

using var client = new HuggingFaceClient(apiKey);
var response = await client.RepoSearch.CreateQuicksearchAsync(
 request: new Request45
 {
 Q = "text-generation",
 Limit = 5,
 Exclude = [],
 });
Console.WriteLine($"Found {response.ModelsCount} models, {response.DatasetsCount} datasets");
foreach (var model in response.Models)
{
 Console.WriteLine($" {model.Id} (weight={model.TrendingWeight:F2})");
}

Error Handling

Handle API errors gracefully using the ApiException type.

using var client = new HuggingFaceClient("invalid-api-key");
try
{
 await client.Auth.GetWhoamiV2Async();
}
catch (ApiException ex)
{
 Console.WriteLine($"Status: {ex.StatusCode}");
 Console.WriteLine($"Message: {ex.Message}");
 Console.WriteLine($"Body: {ex.ResponseBody}");
}

Search Datasets

Search for datasets on the HuggingFace Hub using quicksearch and list results.

using var client = new HuggingFaceClient(apiKey);
var response = await client.RepoSearch.CreateQuicksearchAsync(
 request: new Request45
 {
 Q = "sentiment analysis",
 Limit = 5,
 Exclude = [],
 });
Console.WriteLine($"Models: {response.ModelsCount}, Datasets: {response.DatasetsCount}, Spaces: {response.SpacesCount}");
foreach (var dataset in response.Datasets)
{
 Console.WriteLine($" Dataset: {dataset.Id}");
}
foreach (var space in response.Spaces)
{
 Console.WriteLine($" Space: {space.Id}");
}

List trending items filtered by type (model, dataset, or space).

using var client = new HuggingFaceClient(apiKey);
var spaces = await client.Models.GetTrendingAsync(
 type: Type5.Space,
 limit: 3);
Console.WriteLine("Trending Spaces:");
foreach (var item in spaces.RecentlyTrending)
{
 var id = item.Value1?.RepoData?.Id ?? item.Value2?.RepoData?.Id ?? item.Value3?.RepoData?.Id;
 Console.WriteLine($" {id}");
}

Search Papers

Search for papers and collections on the HuggingFace Hub.

using var client = new HuggingFaceClient(apiKey);
var response = await client.RepoSearch.CreateQuicksearchAsync(
 request: new Request45
 {
 Q = "transformer attention",
 Limit = 5,
 Exclude = [],
 });
Console.WriteLine($"Papers: {response.PapersCount}, Collections: {response.CollectionsCount}");
foreach (var paper in response.Papers)
{
 Console.WriteLine($" Paper: {paper.Id}");
}
foreach (var collection in response.Collections)
{
 Console.WriteLine($" Collection: {collection.Title} - {collection.Description}");
}

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

This project is supported by JetBrains 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 (1)

Showing the top 1 popular GitHub repositories that depend on HuggingFace:

Repository Stars
tryAGI/LangChain
C# implementation of LangChain. We try to be as close to the original as possible in terms of abstractions, but are open to new entities.
Version Downloads Last Updated
0.4.3-dev.92 58 6/16/2026
0.4.3-dev.51 65 5/21/2026
0.4.3-dev.49 71 5/7/2026
0.4.2 224 4/30/2026
0.4.2-dev.1 66 4/30/2026
0.4.1-dev.155 90 4/1/2026
0.4.1-dev.149 79 3/29/2026
0.4.1-dev.148 74 3/29/2026
0.4.1-dev.143 167 3/28/2026
0.4.1-dev.136 82 3/27/2026
0.4.1-dev.130 75 3/23/2026
0.4.1-dev.124 92 3/20/2026
0.4.1-dev.123 72 3/20/2026
0.4.1-dev.119 76 3/20/2026
0.4.1-dev.117 76 3/19/2026
0.4.1-dev.116 78 3/19/2026
0.4.1-dev.115 76 3/19/2026
0.4.1-dev.112 67 3/19/2026
0.4.1-dev.111 76 3/19/2026
0.4.1-dev.110 72 3/19/2026
Loading failed