{"record":{"id":"1360b5aef7444175","repo":"microsoft/semantic-kernel","slug":"the-template-could-not-be-parsed-environment-newl","errorCode":null,"errorMessage":"The template could not be parsed:{Environment.NewLine}{error}","messagePattern":"The template could not be parsed:(.+?)(.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Extensions/PromptTemplates.Liquid/LiquidPromptTemplate.cs","lineNumber":65,"sourceCode":"    /// <exception cref=\"ArgumentNullException\">throw if <paramref name=\"config\"/> is null</exception>\n    /// <exception cref=\"ArgumentNullException\">throw if the template in <paramref name=\"config\"/> is null</exception>\n    public LiquidPromptTemplate(PromptTemplateConfig config, bool allowDangerouslySetContent = false)\n    {\n        Verify.NotNull(config, nameof(config));\n        Verify.NotNull(config.Template, nameof(config.Template));\n        if (config.TemplateFormat != LiquidPromptTemplateFactory.LiquidTemplateFormat)\n        {\n            throw new ArgumentException($\"Invalid template format: {config.TemplateFormat}\");\n        }\n\n        this._allowDangerouslySetContent = allowDangerouslySetContent;\n        this._config = config;\n\n        // Parse the template now so we can check for errors, understand variable usage, and\n        // avoid having to parse on each render.\n        if (!s_parser.TryParse(config.Template, out this._liquidTemplate, out string error))\n        {\n            throw new ArgumentException(error is not null ?\n                $\"The template could not be parsed:{Environment.NewLine}{error}\" :\n                 \"The template could not be parsed.\");\n        }\n\n        // Ideally the prompty author would have explicitly specified input variables. If they specified any,\n        // assume they specified them all. If they didn't, heuristically try to find the variables, looking for\n        // variables that are read but never written and that appear to be simple values rather than complex objects.\n        if (config.InputVariables.Count == 0)\n        {\n            foreach (string implicitVariable in SimpleVariablesVisitor.InferInputs(this._liquidTemplate))\n            {\n                config.InputVariables.Add(new() { Name = implicitVariable, AllowDangerouslySetContent = config.AllowDangerouslySetContent });\n            }\n        }\n\n        // Configure _inputVariables with the default values from the config. This will be used\n        // in RenderAsync to seed the arguments used when evaluating the template.\n        this._inputVariables = [];","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Extensions/PromptTemplates.Liquid/LiquidPromptTemplate.cs#L47-L83","documentation":"Thrown by the LiquidPromptTemplate constructor when `s_parser.TryParse` returns false during eager parsing. The parser runs at construction time so syntax errors surface immediately; the message includes the parser's `error` detail when available, otherwise a generic parse-failure string.","triggerScenarios":"A Liquid template containing unbalanced tags (`{% if %}` with no `{% endif %}`), invalid filter syntax, unclosed output tags, or any syntax the Fluid-based parser rejects.","commonSituations":"Iterating on prompt text and introducing a typo; loading templates from files/DB where a partial edit corrupted the markup; using Liquid features unsupported by the configured parser.","solutions":["Read the embedded `error` string — it names the offending line/token.","Validate the template in a standalone Liquid/FFluid playground first.","Balance all `{% %}` blocks (if/unless/for/case each need a matching end tag)."],"exampleFix":"// before\n{% if active %}Hello{% end %}\n// after\n{% if active %}Hello{% endif %}","handlingStrategy":"try-catch","validationCode":"// Optionally pre-validate with the same Fluid parser used internally before construction.","typeGuard":null,"tryCatchPattern":"try { var t = new LiquidPromptTemplate(config); }\ncatch (ArgumentException ex) {\n    // ex.Message contains the parser error detail; report line/token to the author\n    logger.LogError(ex, \"Liquid template parse failed\"); throw;\n}","preventionTips":["Lint templates in CI before deployment.","Keep templates small and test each block independently."],"tags":["liquid","prompt-template","parsing"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}