{"record":{"id":"7807581f35c4b1dc","repo":"langchain-ai/langchain","slug":"invalid-template-template","errorCode":null,"errorMessage":"Invalid template: {template}","messagePattern":"Invalid template: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/chat.py","lineNumber":534,"sourceCode":"                elif isinstance(tmpl, dict):\n                    if template_format == \"jinja2\":\n                        msg = (\n                            \"jinja2 is unsafe and is not supported for templates \"\n                            \"expressed as dicts. Please use 'f-string' or 'mustache' \"\n                            \"format.\"\n                        )\n                        raise ValueError(msg)\n                    data_template_obj = DictPromptTemplate(\n                        template=cast(\"dict[str, Any]\", tmpl),\n                        template_format=template_format,\n                    )\n                    prompt.append(data_template_obj)\n                else:\n                    msg = f\"Invalid template: {tmpl}\"  # type: ignore[unreachable]\n                    raise ValueError(msg)\n            return cls(prompt=prompt, **kwargs)\n        msg = f\"Invalid template: {template}\"  # type: ignore[unreachable]\n        raise ValueError(msg)\n\n    @classmethod\n    def from_template_file(\n        cls: type[Self],\n        template_file: str | Path,\n        input_variables: list[str],\n        **kwargs: Any,\n    ) -> Self:\n        \"\"\"Create a class from a template file.\n\n        Args:\n            template_file: path to a template file.\n            input_variables: list of input variables.\n            **kwargs: Keyword arguments to pass to the constructor.\n\n        Returns:\n            A new instance of this class.\n        \"\"\"","sourceCodeStart":516,"sourceCodeEnd":552,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/chat.py#L516-L552","documentation":"The outer guard of BaseChatPromptTemplate.from_template: if the template argument is neither a str nor a Sequence (e.g. an int, a dict at top level, or None), ValueError('Invalid template: {template}') is raised. This is the catch-all after the str branch and the Sequence branch have both failed, and is marked unreachable by type checkers because the annotated type excludes other inputs.","triggerScenarios":"Calling from_template(template=123), from_template(None), or from_template({'text': ...}) (a top-level dict is not accepted here — dict elements are only valid inside a Sequence).","commonSituations":"Passing a config-loaded value without checking its type; refactoring code that previously used PromptTemplate.from_template(direct dict); API boundaries where the template comes from user input.","solutions":["Pass a template string or a list of message templates","If the value arrives from external data, validate isinstance(template, (str, list, tuple)) before calling"],"exampleFix":"# before\nChatPromptTemplate.from_template(cfg.get(\"template\"))  # cfg value is None/int\n\n# after\ntmpl = cfg.get(\"template\")\nif not isinstance(tmpl, (str, list, tuple)):\n    msg = f\"template must be str or sequence, got {type(tmpl)}\"\n    raise ValueError(msg)\nChatPromptTemplate.from_template(tmpl)","handlingStrategy":"type-guard","validationCode":"def validate_template_arg(t):\n    if not isinstance(t, (str, list, tuple)):\n        msg = f\"template must be str or sequence, got {type(t)}\"\n        raise ValueError(msg)\n    return t","typeGuard":"def is_template_arg(t) -> bool:\n    return isinstance(t, (str, list, tuple))","tryCatchPattern":null,"preventionTips":["Type-check externally sourced template values (config/JSON) before passing them in"],"tags":["prompts","chat","validation","core"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}