BerriAI/litellm · error · HuggingFaceError

sentence-similarity requires 2+ sentences

Error message

sentence-similarity requires 2+ sentences

What it means

For HF embedding models whose resolved pipeline tag is 'sentence-similarity' or 'similarity', LiteLLM reshapes the input list into TEI's pairwise payload {'source_sentence': input[0], 'sentences': input[1:]}. That shape is meaningless with fewer than two inputs, so a single-element input raises HuggingFaceError(400) client-side. Embedding one sentence with such a model is simply not a supported operation through this route.

Solutions

  1. Pass at least two inputs when using similarity-style models: input=[source, candidate, ...]
  2. For plain embeddings switch to an embedding model (pipeline_tag feature-extraction/embed), e.g. 'hf/BAAI/bge-m3'
  3. Or force task_type='feature-extraction' for models that support it, which skips the pairwise reshape
  4. Guard batch pipelines to skip/accumulate when fewer than 2 inputs remain

Example fix

# before
litellm.embedding(model="hf/sentence-transformers/all-MiniLM-L6-v2", input=["litellm is great"])

# after
litellm.embedding(
    model="hf/sentence-transformers/all-MiniLM-L6-v2",
    input=["litellm is great", "a proxy for LLM APIs"],
)
# or use a feature-extraction model for single inputs
Defensive patterns

Strategy: validation

Validate before calling

def ok_for_similarity_model(input_list: list[str], pipeline_tag: str) -> bool:
    if pipeline_tag in ("sentence-similarity", "similarity"):
        return len(input_list) >= 2
    return True

assert ok_for_similarity_model(input, "sentence-similarity"), "need source + at least one candidate"

Type guard

def is_valid_hf_embedding_input(inputs: list, pipeline_tag: str | None) -> bool:
    if pipeline_tag is None:
        return True
    if pipeline_tag in ("sentence-similarity", "similarity", "rerank"):
        return len(inputs) >= 2
    return True

Prevention

When it happens

Trigger: litellm.embedding(model='hf/sentence-transformers/all-MiniLM-L6-v2', input=['only sentence']) (pipeline_tag sentence-similarity); also when task_type='sentence-similarity' or 'similarity' is passed explicitly with len(input) < 2.

Common situations: Reusing a sentence-transformers similarity model for plain single-text embeddings; batch code where a filtered list collapsed to one item; porting OpenAI-style single-input embedding calls to HF models.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/fd4fc563c44787ae. Report an issue: GitHub.

Appendix: source

Thrown at litellm/llms/huggingface/embedding/handler.py:80

    pipeline_tag: Final[str | None] = model_info_dict.get("pipeline_tag", None)

    return pipeline_tag


class HuggingFaceEmbedding(BaseLLM):
    _client_session: httpx.Client | None = None
    _aclient_session: httpx.AsyncClient | None = None

    def __init__(self) -> None:
        super().__init__()

    def _transform_input_on_pipeline_tag(self, input: list, pipeline_tag: str | None) -> dict:
        if pipeline_tag is None:
            return {"inputs": input}
        if pipeline_tag == "sentence-similarity" or pipeline_tag == "similarity":
            if len(input) < 2:
                raise HuggingFaceError(
                    status_code=400,
                    message="sentence-similarity requires 2+ sentences",
                )
            return {"inputs": {"source_sentence": input[0], "sentences": input[1:]}}
        elif pipeline_tag == "rerank":
            if len(input) < 2:
                raise HuggingFaceError(
                    status_code=400,
                    message="reranker requires 2+ sentences",
                )
            return {"inputs": {"query": input[0], "texts": input[1:]}}
        return {"inputs": input}  # default to feature-extraction pipeline tag

    async def _async_transform_input(
        self,
        model: str,
        task_type: str | None,
        embed_url: str,

View on GitHub (pinned to 77b7c6c40c)