Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langchain ai Langchain HuggingFaceEndpointEmbeddings

From Leeroopedia
Revision as of 11:24, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Langchain_ai_Langchain_HuggingFaceEndpointEmbeddings.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Embeddings, Hugging Face, Inference API
Last Updated 2026-02-11 00:00 GMT

Overview

HuggingFaceEndpointEmbeddings is a LangChain embeddings integration that generates text embeddings using Hugging Face's Inference API or compatible inference providers.

Description

HuggingFaceEndpointEmbeddings extends both BaseModel (Pydantic) and Embeddings (langchain-core) to provide embedding generation via the Hugging Face Hub Inference API. It uses the huggingface_hub library's InferenceClient and AsyncInferenceClient for synchronous and asynchronous operations respectively. The class supports configurable model selection (defaulting to sentence-transformers/all-mpnet-base-v2), custom inference providers (e.g., SambaNova), and the feature-extraction task. Newlines are automatically replaced with spaces in input texts to improve embedding quality.

Usage

Import this class when you need text embeddings from Hugging Face hosted models or custom inference endpoints, particularly for semantic search, RAG pipelines, or other embedding-based workflows.

Code Reference

Source Location

  • Repository: Langchain_ai_Langchain
  • File: libs/partners/huggingface/langchain_huggingface/embeddings/huggingface_endpoint.py
  • Lines: 1-173

Signature

class HuggingFaceEndpointEmbeddings(BaseModel, Embeddings):
    client: Any = None
    async_client: Any = None
    model: str | None = None
    provider: str | None = None
    repo_id: str | None = None
    task: str | None = "feature-extraction"
    model_kwargs: dict | None = None
    huggingfacehub_api_token: str | None = Field(...)

    def embed_documents(self, texts: list[str]) -> list[list[float]]: ...
    async def aembed_documents(self, texts: list[str]) -> list[list[float]]: ...
    def embed_query(self, text: str) -> list[float]: ...
    async def aembed_query(self, text: str) -> list[float]: ...

Import

from langchain_huggingface import HuggingFaceEndpointEmbeddings

I/O Contract

Inputs

Name Type Required Description
model None No Model name to use. Default: "sentence-transformers/all-mpnet-base-v2".
provider None No Name of the inference provider (e.g., "sambanova"). Defaults to HF Inference API.
repo_id None No Hugging Face Hub repository ID. For backward compatibility; use model instead.
task None No Task to call the model with. Default: "feature-extraction".
model_kwargs None No Additional keyword arguments to pass to the model.
huggingfacehub_api_token None No API token. Read from HUGGINGFACEHUB_API_TOKEN or HF_TOKEN env vars.
texts list[str] Yes (for embed_documents) List of texts to embed.
text str Yes (for embed_query) Single text to embed.

Outputs

Name Type Description
embed_documents return list[list[float]] List of embedding vectors, one per input text.
aembed_documents return list[list[float]] Async variant of embed_documents.
embed_query return list[float] Single embedding vector for the query text.
aembed_query return list[float] Async variant of embed_query.

Usage Examples

Basic Usage

from langchain_huggingface import HuggingFaceEndpointEmbeddings

hf = HuggingFaceEndpointEmbeddings(
    model="sentence-transformers/all-mpnet-base-v2",
    task="feature-extraction",
    huggingfacehub_api_token="my-api-key",
)

# Embed documents
vectors = hf.embed_documents(["Hello world", "How are you?"])
print(len(vectors))       # 2

# Embed a query
vector = hf.embed_query("What is the meaning of life?")
print(len(vector))        # Embedding dimension

Async Usage

import asyncio
from langchain_huggingface import HuggingFaceEndpointEmbeddings

hf = HuggingFaceEndpointEmbeddings(model="sentence-transformers/all-mpnet-base-v2")

vectors = asyncio.run(hf.aembed_documents(["Hello", "World"]))

Related Pages

  • Requires langchain-huggingface and huggingface_hub packages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment