Skip to content

Support list[str] inputs in feature_extraction#4115

Merged
hanouticelina merged 2 commits intohuggingface:mainfrom
SJeffZhang:fix/feature-extraction-list-inputs
Apr 27, 2026
Merged

Support list[str] inputs in feature_extraction#4115
hanouticelina merged 2 commits intohuggingface:mainfrom
SJeffZhang:fix/feature-extraction-list-inputs

Conversation

@SJeffZhang
Copy link
Copy Markdown
Contributor

@SJeffZhang SJeffZhang commented Apr 15, 2026

Fixes #2824.

Summary

This updates InferenceClient.feature_extraction and AsyncInferenceClient.feature_extraction to accept either a single string or a list of strings.

Why

FeatureExtractionInput.inputs already supports both str and list[str], but the public client methods were still typed and documented as single-string only. That made the public API inconsistent with the underlying request shape and existing provider support for batched embeddings.

Changes

  • accept str | list[str] in sync feature_extraction
  • accept str | list[str] in async feature_extraction
  • update docstrings to describe batch inputs explicitly
  • add sync and async regression tests to verify list inputs are forwarded unchanged and converted back to float32 numpy arrays

Impact

Users can now batch embedding requests through the public client API without relying on undocumented behavior or type ignores.

Validation

  • python -m pytest tests/test_inference_client.py tests/test_inference_async_client.py -k "feature_extraction_accepts_list_inputs or sync_vs_async_signatures"
  • git diff --check

Note

Low Risk
Small, backward-compatible API surface expansion (type/docs + tests) with no changes to request/response handling logic beyond allowing list inputs.

Overview
InferenceClient.feature_extraction and AsyncInferenceClient.feature_extraction now accept str | list[str], aligning the public API and docs with provider support for batched embedding requests.

Adds sync/async regression tests that pass a list of texts, assert the list is forwarded unchanged to prepare_request, and verify the returned embeddings are converted into float32 numpy arrays (including new async mocking imports).

Reviewed by Cursor Bugbot for commit cefe287. Bugbot is set up for automated code reviews on this repo. Configure here.

@SJeffZhang SJeffZhang marked this pull request as ready for review April 15, 2026 21:40
@Wauplin
Copy link
Copy Markdown
Contributor

Wauplin commented Apr 22, 2026

Hi @SJeffZhang thanks for the PR :) Did you tested this PR on a real endpoint? If yes, could you share the code snippet you used?

As per the PR itself, I think it's fine to add it (cf #2824 (comment)) but I'll let the final go to @hanouticelina

@SJeffZhang
Copy link
Copy Markdown
Contributor Author

@Wauplin thanks for the feedback and for taking a look.
Yes, I tested this on a real endpoint from the current branch using sentence-transformers/all-MiniLM-L6-v2 with hf-inference:

from huggingface_hub import InferenceClient

client = InferenceClient(
    model="sentence-transformers/all-MiniLM-L6-v2",
    provider="hf-inference",
)
embeddings = client.feature_extraction(["hello world", "how are you"])

print(type(embeddings).__name__)
print(embeddings.dtype)
print(embeddings.shape)

It returned:

ndarray
float32
shape (2, 384)
48d4cb51403e6d8cdb9c1deb1fcb1a6d

So list[str] inputs are accepted correctly on a real embedding endpoint and returned as a 2D float32 numpy array.

I also ran the full local test suite with:
python -m pytest ./tests/

The suite completed locally, and the PR-related tests passed. I did see a few unrelated failures in live/environment-dependent tests, but nothing pointing to this feature_extraction change.

@hanouticelina could you please review this when you have a chance?

Copy link
Copy Markdown
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.

Makes sense and tested the PR as well ✅ thanks @SJeffZhang for the PR!

(will merge as soon as the CI is green)

@bot-ci-comment
Copy link
Copy Markdown

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.

@hanouticelina
Copy link
Copy Markdown
Contributor

Failing tests are unrelated and the added tests are passing ✅

@hanouticelina hanouticelina merged commit c60200c into huggingface:main Apr 27, 2026
6 of 16 checks passed
@huggingface-hub-bot
Copy link
Copy Markdown
Contributor

This PR has been shipped as part of the v1.13.0 release.

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.

Support List[str] in InferenceClient.feature_extraction text parameter

3 participants