{"record":{"id":"2e8b277a36628fb1","repo":"langchain-ai/langchain","slug":"invalid-template-tmpl","errorCode":null,"errorMessage":"Invalid template: {tmpl}","messagePattern":"Invalid template: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/chat.py","lineNumber":531,"sourceCode":"                        msg = f\"Invalid image template: {tmpl}\"  # type: ignore[unreachable]\n                        raise ValueError(msg)\n                    prompt.append(img_template_obj)\n                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","sourceCodeStart":513,"sourceCodeEnd":549,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/chat.py#L513-L549","documentation":"Inside the sequence-template loop of from_messages/from_template, an element that is neither str, dict, nor an accepted shape falls through to ValueError('Invalid template: {tmpl}'). Static analysis marks it unreachable because the loop's isinstance chain covers str and dict; reaching it requires an element type outside those branches (int, None, arbitrary objects) slipping into the template list.","triggerScenarios":"Passing a list-of-templates element like 42, None, or a custom object: ChatPromptTemplate.from_messages(['system: hi', None]) — the None is neither str nor dict and trips the fallthrough.","commonSituations":"Optional message sections concatenated with 'or []' patterns that actually yield [None]; data-driven prompt assembly from JSON configs where a field is null; conditional splices inserting a sentinel value.","solutions":["Filter the list before passing: [t for t in templates if t is not None]","Ensure every element is a str or dict as documented for list templates"],"exampleFix":"# before\nparts = [system_prompt, user_section or None]\nChatPromptTemplate.from_messages(parts)  # ValueError on None\n\n# after\nparts = [p for p in [system_prompt, user_section] if p is not None]\nChatPromptTemplate.from_messages(parts)","handlingStrategy":"validation","validationCode":"def clean_template_list(templates):\n    bad = [t for t in templates if not isinstance(t, (str, dict))]\n    if bad:\n        msg = f\"non-str/dict template elements: {bad!r}\"\n        raise ValueError(msg)\n    return templates","typeGuard":"def is_valid_template_element(t) -> bool:\n    return isinstance(t, (str, dict))","tryCatchPattern":null,"preventionTips":["Filter None out of conditionally assembled template lists before from_messages","Keep template lists homogeneous (str/dict) when loading from config files"],"tags":["prompts","chat","validation","core"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}