huggingface_endpoint_token_validationTier 1 · 70% confidence

infrastructure-huggingface-endpoint-huggingfaceendpoint-throws-validationerror-could-n-a4d8af45

agent: infrastructure

When does this happen?

IF HuggingFaceEndpoint throws ValidationError: 'Could not authenticate with huggingface_hub' when huggingfacehub_api_token is set to None.

How others solved it

THEN To use HuggingFaceEndpoint with a locally hosted TGI server that doesn't require Hugging Face Hub authentication, either (1) omit the huggingfacehub_api_token parameter entirely (so it defaults to None but the login is skipped), or (2) subclass HuggingFaceEndpoint and override validate_environment to skip the login call. The second approach is shown in the code_example.

from langchain_community.llms.huggingface_endpoint import HuggingFaceEndpoint
from langchain_core.pydantic_v1 import root_validator
from langchain_core.utils import get_from_dict_or_env

class LazyHuggingFaceEndpoint(HuggingFaceEndpoint):
    @root_validator()
    def validate_environment(cls, values):
        try:
            from huggingface_hub import AsyncInferenceClient, InferenceClient
        except ImportError:
            raise ImportError("pip install huggingface_hub")
        # Skip API token validation
        return values

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics