{"record":{"id":"f06da5638063beb7","repo":"langchain-ai/langchain","slug":"trying-to-close-tag-tag-key-nlooks-like-it-was","errorCode":null,"errorMessage":"Trying to close tag \"{tag_key}\"\\nLooks like it was not opened.\\nline {_CURRENT_LINE + 1}","messagePattern":"Trying to close tag \"(.+?)\"\\\\nLooks like it was not opened\\.\\\\nline (.+?)","errorType":"exception","errorClass":"ChevronError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/utils/mustache.py","lineNumber":282,"sourceCode":"        # If we are a section tag\n        elif tag_type in {\"section\", \"inverted section\"}:\n            # Then open a new section\n            open_sections.append(tag_key)\n            _LAST_TAG_LINE = _CURRENT_LINE\n\n        # If we are an end tag\n        elif tag_type == \"end\":\n            # Then check to see if the last opened section\n            # is the same as us\n            try:\n                last_section = open_sections.pop()\n            except IndexError as e:\n                msg = (\n                    f'Trying to close tag \"{tag_key}\"\\n'\n                    \"Looks like it was not opened.\\n\"\n                    f\"line {_CURRENT_LINE + 1}\"\n                )\n                raise ChevronError(msg) from e\n            if tag_key != last_section:\n                # Otherwise we need to complain\n                msg = (\n                    f'Trying to close tag \"{tag_key}\"\\n'\n                    f'last open tag is \"{last_section}\"\\n'\n                    f\"line {_CURRENT_LINE + 1}\"\n                )\n                raise ChevronError(msg)\n\n        # Do the second check to see if we're a standalone\n        is_standalone = r_sa_check(template, tag_type, is_standalone)\n\n        # Which if we are\n        if is_standalone:\n            # Remove the stuff before the newline\n            template = template.split(\"\\n\", 1)[-1]\n\n            # Partials need to keep the spaces on their left","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/utils/mustache.py#L264-L300","documentation":"Raised by the Mustache tokenizer in `langchain_core.utils.mustache` when a closing section tag `{{/name}}` appears but there is no currently open section to close (the stack of opened sections is empty). Mustache sections must be properly nested and closed; a close with no open is a structural error. The line number is one-based.","triggerScenarios":"A template containing `{{/items}}` with no preceding `{{#items}}` (or `{{^items}}`), rendered via `MustachePlaceholderFormatter.format` or parsed with `tokenize`. Also happens when the opening tag was itself malformed (e.g. typo `{{#tiems}}`) so it never registered as opened.","commonSituations":"Copy-paste editing of prompt templates that drops the opening tag; renaming a section in the opener but not the closer; closing tags left behind after deleting a conditional block; templates assembled by concatenation losing the opener.","solutions":["Check the reported line and add the matching opening section tag `{{#name}}` above the content, or delete the stray closer.","Make sure opener and closer names match exactly (case-sensitive).","Lint templates with the tokenizer in CI or at startup so structural errors surface immediately."],"exampleFix":"# before\ntemplate = \"Answer: {{answer}} {{/details}}\"\n\n# after\ntemplate = \"{{#details}}Answer: {{answer}}{{/details}}\"","handlingStrategy":"validation","validationCode":"import re\n\ndef sections_balanced(template: str) -> bool:\n    stack = []\n    for typ, key in re.findall(r\"\\{\\{\\s*(#|\\^|/)\\s*(.*?)\\s*\\}\\}\", template):\n        if typ in \"#^\":\n            stack.append(key)\n        elif not stack or stack.pop() != key:\n            return False\n    return not stack","typeGuard":null,"tryCatchPattern":"try:\n    list(tokenize(template))\nexcept ChevronError as e:\n    raise ValueError(f\"invalid template: {e}\") from e","preventionTips":["Lint templates with the tokenizer before deploying.","Keep section openers/closers adjacent in source and review diffs that touch one side."],"tags":["mustache","sections","prompt-template"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}