{"record":{"id":"fd4fc563c44787ae","repo":"BerriAI/litellm","slug":"sentence-similarity-requires-2-sentences","errorCode":null,"errorMessage":"sentence-similarity requires 2+ sentences","messagePattern":"sentence-similarity requires 2\\+ sentences","errorType":"exception","errorClass":"HuggingFaceError","httpStatus":400,"severity":"error","filePath":"litellm/llms/huggingface/embedding/handler.py","lineNumber":80,"sourceCode":"\n    pipeline_tag: Final[str | None] = model_info_dict.get(\"pipeline_tag\", None)\n\n    return pipeline_tag\n\n\nclass HuggingFaceEmbedding(BaseLLM):\n    _client_session: httpx.Client | None = None\n    _aclient_session: httpx.AsyncClient | None = None\n\n    def __init__(self) -> None:\n        super().__init__()\n\n    def _transform_input_on_pipeline_tag(self, input: list, pipeline_tag: str | None) -> dict:\n        if pipeline_tag is None:\n            return {\"inputs\": input}\n        if pipeline_tag == \"sentence-similarity\" or pipeline_tag == \"similarity\":\n            if len(input) < 2:\n                raise HuggingFaceError(\n                    status_code=400,\n                    message=\"sentence-similarity requires 2+ sentences\",\n                )\n            return {\"inputs\": {\"source_sentence\": input[0], \"sentences\": input[1:]}}\n        elif pipeline_tag == \"rerank\":\n            if len(input) < 2:\n                raise HuggingFaceError(\n                    status_code=400,\n                    message=\"reranker requires 2+ sentences\",\n                )\n            return {\"inputs\": {\"query\": input[0], \"texts\": input[1:]}}\n        return {\"inputs\": input}  # default to feature-extraction pipeline tag\n\n    async def _async_transform_input(\n        self,\n        model: str,\n        task_type: str | None,\n        embed_url: str,","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/huggingface/embedding/handler.py#L62-L98","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass at least two inputs when using similarity-style models: input=[source, candidate, ...]","For plain embeddings switch to an embedding model (pipeline_tag feature-extraction/embed), e.g. 'hf/BAAI/bge-m3'","Or force task_type='feature-extraction' for models that support it, which skips the pairwise reshape","Guard batch pipelines to skip/accumulate when fewer than 2 inputs remain"],"exampleFix":"# before\nlitellm.embedding(model=\"hf/sentence-transformers/all-MiniLM-L6-v2\", input=[\"litellm is great\"])\n\n# after\nlitellm.embedding(\n    model=\"hf/sentence-transformers/all-MiniLM-L6-v2\",\n    input=[\"litellm is great\", \"a proxy for LLM APIs\"],\n)\n# or use a feature-extraction model for single inputs","handlingStrategy":"validation","validationCode":"def ok_for_similarity_model(input_list: list[str], pipeline_tag: str) -> bool:\n    if pipeline_tag in (\"sentence-similarity\", \"similarity\"):\n        return len(input_list) >= 2\n    return True\n\nassert ok_for_similarity_model(input, \"sentence-similarity\"), \"need source + at least one candidate\"","typeGuard":"def is_valid_hf_embedding_input(inputs: list, pipeline_tag: str | None) -> bool:\n    if pipeline_tag is None:\n        return True\n    if pipeline_tag in (\"sentence-similarity\", \"similarity\", \"rerank\"):\n        return len(inputs) >= 2\n    return True","tryCatchPattern":null,"preventionTips":["Use feature-extraction/embed models for single-sentence embedding needs","Check the model's pipeline_tag on its HF page before wiring it into embedding code","In batch pipelines, accumulate until >= 2 items or switch models when a batch shrinks to one"],"tags":["huggingface","embeddings","sentence-similarity","input-validation","litellm"],"backgroundTag":"input-validation-failed","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}