Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 24, 2025

Conversation

mishig25
Copy link
Contributor

@mishig25 mishig25 commented Jun 12, 2025

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)

@HuggingFaceDocBuilderDev

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.

@Wauplin Wauplin marked this pull request as ready for review June 17, 2025 14:35
Copy link
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mishig25 , thanks for reporting + opened a PR. I just pushed 889a4ee so that this fix applies only to "feature-extraction" task and not to all tasks. I also added a quick test.

I'm approving right now but if that's fine with you, I'd prefer to wait until @hanouticelina 's back to have her opinion as well.

Note: I went back to our input.json specs which clearly states that parameters must be at root level.

@@ -46,7 +46,7 @@
from typing import TYPE_CHECKING


__version__ = "0.33.0.dev0"
__version__ = "0.33.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this change 50dba39

Suggested change
__version__ = "0.33.0"
__version__ = "0.33.0.dev0"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need it in the meantime. Otherwise, getting error when using with diffusers: version has to be xyz

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes ok so let's not forget about it before merging. Converting back to draft in the meantime

@Wauplin Wauplin marked this pull request as draft June 17, 2025 15:21
@Wauplin Wauplin marked this pull request as draft June 17, 2025 15:21
Copy link
Contributor

@hanouticelina hanouticelina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! let's move this PR to ready for review and revert the package version change before merging. Once it's merged, we can go ahead and release a patch.

Side note: This also got me thinking that we should probably consider switch now to OpenAI-compatible specs for embeddings/feature-extraction for HF Inference API and update the SDKs accordingly (in another PRs of course!). cc @tomaarsen for viz

@mishig25
Copy link
Contributor Author

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

@hanouticelina
Copy link
Contributor

@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)

This reverts commit 50dba39.
@mishig25 mishig25 marked this pull request as ready for review June 24, 2025 08:34
@mishig25
Copy link
Contributor Author

@hanouticelina it is working now, will merge it

@mishig25 mishig25 merged commit 07b7654 into main Jun 24, 2025
24 of 25 checks passed
@mishig25 mishig25 deleted the fix__prepare_payload_as_dict branch June 24, 2025 08:37
hanouticelina pushed a commit that referenced this pull request Jun 25, 2025
…ictionary (#3160)

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

* dev change

* Update only feature-extraction task + add test + add specs link

* [dev change]

* Revert "[dev change]"

This reverts commit 50dba39.

---------

Co-authored-by: Lucain Pouget <[email protected]>
mintyleaf pushed a commit to Swarmind/huggingface_hub that referenced this pull request Jul 11, 2025
…ictionary (huggingface#3160)

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

* dev change

* Update only feature-extraction task + add test + add specs link

* [dev change]

* Revert "[dev change]"

This reverts commit 50dba39.

---------

Co-authored-by: Lucain Pouget <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants