BerriAI/litellm · error · Exception

Invalid task_type={task_type}. Expected one of={hf_tasks_emb

Error message

Invalid task_type={task_type}. Expected one of={hf_tasks_embeddings}

What it means

Raised by get_hf_task_embedding_for_model (sync path) when the caller passes a task_type that is not one of the supported embedding pipeline tags: 'sentence-similarity', 'feature-extraction', 'rerank', 'embed', 'similarity'. HF embedding routing uses the model's pipeline tag to pick the right TEI endpoint; an unknown task_type is rejected before any request is sent.

Source

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

from .transformation import HuggingFaceEmbeddingConfig

config: Final = HuggingFaceEmbeddingConfig()

HF_HUB_URL: Final = "https://huggingface.co"

hf_tasks_embeddings: Final = (
    Literal[  # pipeline tags + hf tei endpoints - https://huggingface.github.io/text-embeddings-inference/#/
        "sentence-similarity", "feature-extraction", "rerank", "embed", "similarity"
    ]
)


def get_hf_task_embedding_for_model(model: str, task_type: str | None, api_base: str) -> str | None:
    if task_type is not None:
        if task_type in get_args(hf_tasks_embeddings):
            return task_type
        else:
            raise Exception(f"Invalid task_type={task_type}. Expected one of={hf_tasks_embeddings}")
    http_client: Final = HTTPHandler(concurrent_limit=1)

    model_info: Final = http_client.get(url=f"{api_base}/api/models/{model}")

    model_info_dict: Final = model_info.json()

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

    return pipeline_tag


async def async_get_hf_task_embedding_for_model(model: str, task_type: str | None, api_base: str) -> str | None:
    if task_type is not None:
        if task_type in get_args(hf_tasks_embeddings):
            return task_type
        else:
            raise Exception(f"Invalid task_type={task_type}. Expected one of={hf_tasks_embeddings}")
    http_client: Final = get_async_httpx_client(

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Use one of the exact allowed strings: 'sentence-similarity', 'feature-extraction', 'rerank', 'embed', or 'similarity' (lowercase).
  2. If unsure, omit task_type entirely — the function then queries the model's pipeline_tag from the Hub API and uses that automatically.
  3. Most embeddings use cases should simply not pass task_type; set it only when the auto-detected tag is wrong.

Example fix

# before
litellm.embedding(model='hf/BAAI/bge-large-en-v1.5', input=['hi'], task_type='embedding')
# raises Exception: Invalid task_type=embedding

# after — omit it and let it auto-detect
litellm.embedding(model='hf/BAAI/bge-large-en-v1.5', input=['hi'])
# or use an allowed value: task_type='feature-extraction'
Defensive patterns

Strategy: validation

Validate before calling

from typing import get_args
from litellm.llms.huggingface.embedding.handler import hf_tasks_embeddings

ALLOWED_TASK_TYPES = set(get_args(hf_tasks_embeddings))
# {'sentence-similarity','feature-extraction','rerank','embed','similarity'}

def valid_task_type(task_type: str | None) -> bool:
    return task_type is None or task_type in ALLOWED_TASK_TYPES

Type guard

def is_valid_hf_task_type(v: str) -> bool:
    """Narrows a string to a HF embeddings task_type slug."""
    return v in {"sentence-similarity", "feature-extraction", "rerank", "embed", "similarity"}

Prevention

When it happens

Trigger: Calling litellm.embedding(model='hf/<model>', ..., task_type='<bad>') with a value outside the allowed set — e.g. 'embedding', 'text-embedding', 'embeddings' (plural), or a chat tag like 'text-generation'. Exact string match only.

Common situations: Assuming the value is 'embedding' instead of 'embed'; copying task_type values from HF Hub UI labels that differ from the literal slugs; case mistakes ('Embed' vs 'embed'); parameter added defensively with a guessed default.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/c7ec8fd8c8e5c71a. Report an issue: GitHub.