BerriAI/litellm · error · Exception

Invalid hf task - {task}. Valid formats - {hf_tasks}.

Error message

Invalid hf task - {task}. Valid formats - {hf_tasks}.

What it means

Raised by the HuggingFace chat transformation when litellm_params['task'] is missing, not a string, or not one of the supported tasks: 'text-generation-inference', 'conversational', 'text-classification', 'text-generation' (defined in litellm/llms/huggingface/common_utils.py). It guards request building before any HTTP call.

Source

Thrown at litellm/llms/huggingface/embedding/transformation.py:211

        elif model in conversational_models:
            return "conversational", model
        elif "roneneldan/TinyStories" in model:
            return "text-generation", model
        else:
            return "text-generation-inference", model  # default to tgi

    def transform_request(
        self,
        model: str,
        messages: list[AllMessageValues],
        optional_params: dict,
        litellm_params: dict,
        headers: dict,
    ) -> dict:
        task: Final = litellm_params.get("task", None)
        ## VALIDATE API FORMAT
        if task is None or not isinstance(task, str) or task not in hf_task_list:
            raise Exception(f"Invalid hf task - {task}. Valid formats - {hf_tasks}.")

        ## Load Config
        config: Final = litellm.HuggingFaceEmbeddingConfig.get_config()
        for k, v in config.items():
            if (
                k not in optional_params
            ):  # completion(top_k=3) > huggingfaceConfig(top_k=3) <- allows for dynamic variables to be passed in
                optional_params[k] = v

        ### MAP INPUT PARAMS
        #### HANDLE SPECIAL PARAMS
        special_params: Final = self.get_special_options_params()
        special_params_dict: Final = {}
        # Create a list of keys to pop after iteration
        keys_to_pop: Final = []

        for k, v in optional_params.items():
            if k in special_params:

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Pass a valid task: litellm.completion(model='huggingface/<model>', messages=..., task='text-generation-inference').
  2. Use one of exactly: text-generation-inference, conversational, text-classification, text-generation.
  3. For embeddings/rerank, use litellm.embedding / litellm.rerank instead of the completion path.

Example fix

# before
litellm.completion(model='huggingface/meta-llama/Llama-3.2-3B-Instruct', messages=messages)

# after
litellm.completion(model='huggingface/meta-llama/Llama-3.2-3B-Instruct', messages=messages, task='text-generation-inference')
Defensive patterns

Strategy: validation

Validate before calling

HF_TASKS = {'text-generation-inference', 'conversational', 'text-classification', 'text-generation'}

def validate_hf_task(task: str | None) -> str:
    if task not in HF_TASKS:
        raise ValueError(f'task must be one of {sorted(HF_TASKS)}, got {task!r}')
    return task

Type guard

def is_valid_hf_task(task: object) -> bool:
    return isinstance(task, str) and task in {'text-generation-inference', 'conversational', 'text-classification', 'text-generation'}

Prevention

When it happens

Trigger: Calling completion() with custom_llm_provider='huggingface' (or model='huggingface/...') without task=..., or passing task='embedding' / task='sentence-similarity' / a typo like 'text-generation_inference'.

Common situations: Users assume the task is inferred from the model id; in this transformation path it must be passed explicitly. Typos or using a newer HF pipeline name not in the allow-list also trigger it.

Related errors


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