{"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/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/prompt_templates/factory.py#L475-L511","documentation":"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.","triggerScenarios":"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.","commonSituations":"Using base models (no -Instruct suffix) for chat; air-gapped or proxied environments blocking huggingface.co; missing HUGGINGFACE_API_KEY for gated repos.","solutions":["Pass chat_template explicitly in the completion call","Switch to an instruct/chat variant of the model that ships a chat template","Set HF credentials (HUGGINGFACE_API_KEY) if the repo is gated","Verify network access to huggingface.co and the model id"],"exampleFix":"# before: base model, repo has no chat template\nresp = await litellm.acompletion(model=\"huggingface/meta-llama/Llama-3.2-1B\", messages=messages)\n\n# after: instruct variant that ships a template\nresp = await litellm.acompletion(model=\"huggingface/meta-llama/Llama-3.2-1B-Instruct\", messages=messages)","handlingStrategy":"fallback","validationCode":"import litellm\nfrom litellm.litellm_core_utils.prompt_templates.huggingface_template_handler import _aget_chat_template_file\n\nasync def model_has_chat_template(model: str) -> bool:\n    result = await _aget_chat_template_file(model=model)\n    return result.get(\"status\") == \"success\"\n\n# if not await model_has_chat_template(model): supply chat_template= explicitly","typeGuard":null,"tryCatchPattern":"try:\n    resp = await litellm.acompletion(model=model, messages=messages)\nexcept Exception as e:\n    if \"No chat template found\" in str(e):\n        resp = await litellm.acompletion(\n            model=model, messages=messages,\n            chat_template=DEFAULT_TEMPLATE,  # explicit fallback\n        )\n    else:\n        raise","preventionTips":["Prefer -Instruct/-It chat variants that ship templates in the repo","Pin a known chat_template in config for every HF model you serve","Ensure HUGGINGFACE_API_KEY is set for gated repos and huggingface.co is reachable","Add a startup check that fetches the template once and fails fast with a clear message"],"tags":["huggingface","chat-template","fetch","async"],"backgroundTag":"missing-chat-template","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}