BerriAI/litellm · error · Exception

No chat template found

Error message

No chat template found

What it means

Async path: litellm fetched the chat template for a HuggingFace model, but the fetch result did not have status == 'success', so no template was extracted from the repo. Without a chat template the messages cannot be rendered, so it raises. bos/eos tokens are only read when the template fetch itself succeeded.

Solutions

  1. Pass chat_template explicitly in the completion call
  2. Switch to an instruct/chat variant of the model that ships a chat template
  3. Set HF credentials (HUGGINGFACE_API_KEY) if the repo is gated
  4. Verify network access to huggingface.co and the model id

Example fix

# before: base model, repo has no chat template
resp = await litellm.acompletion(model="huggingface/meta-llama/Llama-3.2-1B", messages=messages)

# after: instruct variant that ships a template
resp = await litellm.acompletion(model="huggingface/meta-llama/Llama-3.2-1B-Instruct", messages=messages)
Defensive patterns

Strategy: fallback

Validate before calling

import litellm
from litellm.litellm_core_utils.prompt_templates.huggingface_template_handler import _aget_chat_template_file

async def model_has_chat_template(model: str) -> bool:
    result = await _aget_chat_template_file(model=model)
    return result.get("status") == "success"

# if not await model_has_chat_template(model): supply chat_template= explicitly

Try / catch

try:
    resp = await litellm.acompletion(model=model, messages=messages)
except Exception as e:
    if "No chat template found" in str(e):
        resp = await litellm.acompletion(
            model=model, messages=messages,
            chat_template=DEFAULT_TEMPLATE,  # explicit fallback
        )
    else:
        raise

Prevention

When it happens

Trigger: The model repo has no chat_template (base/completion-only models); a network failure or HF outage; a gated or private repo without valid credentials; a typo'd model id that resolves to nothing.

Common situations: Using base models (no -Instruct suffix) for chat; air-gapped or proxied environments blocking huggingface.co; missing HUGGINGFACE_API_KEY for gated repos.

Related errors


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

Appendix: source

Thrown at litellm/litellm_core_utils/prompt_templates/factory.py:493

            bos_token = _extract_token_value(token_value=tokenizer_data.get("bos_token"))
            eos_token = _extract_token_value(token_value=tokenizer_data.get("eos_token"))
            chat_template = tokenizer_data["chat_template"]
        else:
            # Fallback: Try to fetch chat template from separate .jinja file
            template_result: Final = await get_template_fn(hf_model_name=model)
            if template_result.get("status") == "success":
                chat_template = template_result["chat_template"]
                # Still try to get tokens from tokenizer_config if available
                if (
                    tokenizer_config.get("status") == "success"
                    and "tokenizer" in tokenizer_config
                    and isinstance(tokenizer_config["tokenizer"], dict)
                ):
                    tokenizer_data: dict = tokenizer_config["tokenizer"]
                    bos_token = _extract_token_value(token_value=tokenizer_data.get("bos_token"))
                    eos_token = _extract_token_value(token_value=tokenizer_data.get("eos_token"))
            else:
                raise Exception("No chat template found")

    return chat_template, bos_token, eos_token


def _fetch_and_extract_template(
    model: str, chat_template: Any | None, get_config_fn, get_template_fn
) -> tuple[str, str, str]:
    """
    Sync version: Fetch template and tokens from HuggingFace.

    Returns: (chat_template, bos_token, eos_token)
    """
    from litellm.litellm_core_utils.prompt_templates.huggingface_template_handler import (
        _extract_token_value,
    )

    bos_token = ""
    eos_token = ""

View on GitHub (pinned to 77b7c6c40c)