huggingface_hub icon indicating copy to clipboard operation
huggingface_hub copied to clipboard

fix: update payload preparation to merge parameters into the output dictionary

Open mishig25 opened this issue 5 months ago • 1 comments

Fix feature extraction parameter structure for Inference Endpoints compatibility

Problem

The feature_extraction method's truncate parameter (and other parameters like normalize, prompt_name, truncation_direction) is not working correctly when using custom Inference Endpoints via base_url.

Root Cause

When the provider helper system was introduced in #2777, the parameter structure for HF Inference API requests was inadvertently changed from root-level parameters to nested parameters:

Before (working):

{
  "inputs": "text",
  "truncate": true,
  "normalize": true,
  "prompt_name": "query"
}

After #2777 (broken):

  {
    "inputs": "text",
    "parameters": {
      "truncate": true,
      "normalize": true,
      "prompt_name": "query"
    }
  }

Impact

  • ❌ Python InferenceClient.feature_extraction(truncate=True) fails with custom endpoints
  • ✅ JavaScript @huggingface/inference client works correctly (uses root-level parameters)
  • ✅ HF Serverless Inference API works (likely handles both formats)

This inconsistency breaks existing Inference Endpoints that were deployed expecting the original parameter format.

Solution

Restore the original parameter structure by modifying HFInferenceTask._prepare_payload_as_dict() to place parameters at the root level instead of nesting them under a "parameters" key.

Fixed payload:

  {
    "inputs": "text",
    "truncate": true,
    "normalize": true,
    "prompt_name": "query"
  }

Testing

Without this PR, the snippet below fails:

from huggingface_hub import InferenceClient

client = InferenceClient(base_url="MY_INFERENCE_ENDPOINT_URL", token="hf_TOKEN")

embeddings = client.feature_extraction("This is a test sentence. "*100, truncate=True)

print(embeddings)

mishig25 avatar Jun 12 '25 15:06 mishig25

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

actually, when I try after @Wauplin changes, I'm getting the error of parameters being ignored

mishig25 avatar Jun 23 '25 14:06 mishig25

@mishig25 what is the exact error you're getting? with this PR, the following snippet works as expected and truncate is sent at the root level:

from huggingface_hub import InferenceClient

client = InferenceClient(base_url="MY_INFERENCE_ENDPOINT_URL", token="hf_TOKEN")

embeddings = client.feature_extraction("This is a test sentence. "*100, truncate=True)

print(embeddings)

hanouticelina avatar Jun 23 '25 14:06 hanouticelina

@hanouticelina it is working now, will merge it

mishig25 avatar Jun 24 '25 08:06 mishig25