{"record":{"id":"01071785822152cf","repo":"BerriAI/litellm","slug":"no-chat-template-found","errorCode":null,"errorMessage":"No chat template found","messagePattern":"No chat template found","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/prompt_templates/factory.py","lineNumber":493,"sourceCode":"            bos_token = _extract_token_value(token_value=tokenizer_data.get(\"bos_token\"))\n            eos_token = _extract_token_value(token_value=tokenizer_data.get(\"eos_token\"))\n            chat_template = tokenizer_data[\"chat_template\"]\n        else:\n            # Fallback: Try to fetch chat template from separate .jinja file\n            template_result: Final = await get_template_fn(hf_model_name=model)\n            if template_result.get(\"status\") == \"success\":\n                chat_template = template_result[\"chat_template\"]\n                # Still try to get tokens from tokenizer_config if available\n                if (\n                    tokenizer_config.get(\"status\") == \"success\"\n                    and \"tokenizer\" in tokenizer_config\n                    and isinstance(tokenizer_config[\"tokenizer\"], dict)\n                ):\n                    tokenizer_data: dict = tokenizer_config[\"tokenizer\"]\n                    bos_token = _extract_token_value(token_value=tokenizer_data.get(\"bos_token\"))\n                    eos_token = _extract_token_value(token_value=tokenizer_data.get(\"eos_token\"))\n            else:\n                raise Exception(\"No chat template found\")\n\n    return chat_template, bos_token, eos_token\n\n\ndef _fetch_and_extract_template(\n    model: str, chat_template: Any | None, get_config_fn, get_template_fn\n) -> tuple[str, str, str]:\n    \"\"\"\n    Sync version: Fetch template and tokens from HuggingFace.\n\n    Returns: (chat_template, bos_token, eos_token)\n    \"\"\"\n    from litellm.litellm_core_utils.prompt_templates.huggingface_template_handler import (\n        _extract_token_value,\n    )\n\n    bos_token = \"\"\n    eos_token = \"\"","sourceCodeStart":475,"sourceCodeEnd":511,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/prompt_templates/factory.py#L475-L511","documentation":"Async HuggingFace template fetch (_afetch_and_extract_template): LiteLLM queried the model repo for a chat template and the result did not report success, so no template exists to render your messages. LiteLLM refuses to guess and raises this Exception. Without a chat template, a chat-formatted call to that model cannot be constructed.","triggerScenarios":"Calling acompletion() with a HuggingFace-hosted base model (no chat_template in tokenizer_config.json or chat_template.jinja), a typo'd/nonexistent repo name, or when HuggingFace returned an error/timeout payload treated as a failure.","commonSituations":"Using raw base models (e.g. Llama-2 style) that only have completion templates; private/gated repos where the anonymous fetch fails; network egress blocked so the HF fetch fails; repos that ship only a completion_format prompt.","solutions":["Pass your own chat_template=\"<jinja2>\" (or chat_template_file) so LiteLLM never needs to fetch one","Switch to an instruct/chat-tuned model whose repo ships a chat template","If the repo is gated/private, ensure HF credentials are configured so the fetch succeeds","Verify the repo actually has a template: check tokenizer_config.json for 'chat_template' on huggingface.co"],"exampleFix":"# before\nresp = await litellm.acompletion(\n    model=\"huggingface/meta-llama/Llama-2-7b-hf\",\n    messages=[{\"role\": \"user\", \"content\": \"Hello\"}],\n)\n\n# after\nresp = await litellm.acompletion(\n    model=\"huggingface/meta-llama/Llama-2-7b-hf\",\n    messages=[{\"role\": \"user\", \"content\": \"Hello\"}],\n    chat_template=\"{% for m in messages %}{{ bos_token }}[INST] {{ m['content'] }} [/INST]{% endfor %}\",\n)","handlingStrategy":"fallback","validationCode":"import requests\n\ndef model_has_chat_template(repo_id: str) -> bool:\n    cfg = requests.get(f\"https://huggingface.co/{repo_id}/raw/main/tokenizer_config.json\", timeout=10)\n    if cfg.status_code != 200:\n        return False\n    return bool(cfg.json().get(\"chat_template\"))","typeGuard":null,"tryCatchPattern":"try:\n    resp = await litellm.acompletion(model=hf_model, messages=messages)\nexcept Exception as e:\n    if \"No chat template found\" in str(e):\n        resp = await litellm.acompletion(model=hf_model, messages=messages,\n                                         chat_template=DEFAULT_CHAT_TEMPLATE)\n    else:\n        raise","preventionTips":["Always pass chat_template= for base models","Verify the repo ships a chat_template before deploying","Configure HF credentials for gated repos"],"tags":["huggingface","chat-template","async","network"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}