{"record":{"id":"bf065e335289b97a","repo":"langchain-ai/langchain","slug":"jinja2-not-installed-which-is-needed-to-use-the-j-bf065e","errorCode":null,"errorMessage":"jinja2 not installed, which is needed to use the jinja2_formatter. Please install it with `pip install jinja2`.","messagePattern":"jinja2 not installed, which is needed to use the jinja2_formatter\\. Please install it with `pip install jinja2`\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/string.py","lineNumber":106,"sourceCode":"\n    warning_message = \"\"\n    if missing_variables:\n        warning_message += f\"Missing variables: {missing_variables} \"\n\n    if extra_variables:\n        warning_message += f\"Extra variables: {extra_variables}\"\n\n    if warning_message:\n        warnings.warn(warning_message.strip(), stacklevel=7)\n\n\ndef _get_jinja2_variables_from_template(template: str) -> set[str]:\n    if not _HAS_JINJA2:\n        msg = (\n            \"jinja2 not installed, which is needed to use the jinja2_formatter. \"\n            \"Please install it with `pip install jinja2`.\"\n        )\n        raise ImportError(msg)\n    env = SandboxedEnvironment()\n    ast = env.parse(template)\n    return meta.find_undeclared_variables(ast)\n\n\ndef mustache_formatter(template: str, /, **kwargs: Any) -> str:\n    \"\"\"Format a template using mustache.\n\n    Args:\n        template: The template string.\n        **kwargs: The variables to format the template with.\n\n    Returns:\n        The formatted string.\n    \"\"\"\n    return mustache.render(template, kwargs)\n\n","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/string.py#L88-L124","documentation":"Raised by `_get_jinja2_variables_from_template` in `langchain_core.prompts.string` when LangChain needs to statically parse a Jinja2 template to discover its variables (e.g. in `get_template_variables` or when a `PromptTemplate` with `template_format='jinja2'` infers its `input_variables`) but the optional `jinja2` package is absent. Unlike the formatter variant (which fires at render time), this fires at template-construction/inspection time because parsing the AST requires the library.","triggerScenarios":"`PromptTemplate.from_template(t, template_format=\"jinja2\")` without explicit `input_variables` in an environment lacking jinja2 — variable inference calls this helper and hits the `_HAS_JINJA2` guard. Also `get_template_variables(t, \"jinja2\")` directly.","commonSituations":"Fresh installs of `langchain-core` alone (jinja2 is not a hard dependency); CI pipelines that trim requirements files; sharing code across teams where one environment has jinja2 and a minimal deployment environment does not.","solutions":["Install jinja2: `pip install jinja2` (or add it to the dependency list for any service using jinja2-format prompts)","Pass `input_variables` explicitly if you only need to construct the object — but note formatting will still need jinja2, so installing it is the real fix","If the template does not need Jinja2 control flow, rewrite it as an f-string template (`{var}`) which needs no extra dependency"],"exampleFix":"# before\n# jinja2 not installed\nvars_ = get_template_variables(\"{{ name }} is {{ age }}\", \"jinja2\")  # ImportError\n\n# after\npip install jinja2\nvars_ = get_template_variables(\"{{ name }} is {{ age }}\", \"jinja2\")  # ['age', 'name']","handlingStrategy":"validation","validationCode":"def assert_jinja2_available() -> None:\n    try:\n        import jinja2  # noqa: F401\n    except ImportError as e:\n        msg = \"jinja2 required to infer variables from jinja2 templates\"\n        raise RuntimeError(msg) from e\n\nassert_jinja2_available()  # before PromptTemplate.from_template(t, template_format=\"jinja2\")","typeGuard":null,"tryCatchPattern":"try:\n    p = PromptTemplate.from_template(t, template_format=\"jinja2\")\nexcept ImportError:\n    raise SystemExit(\"pip install jinja2\") from None","preventionTips":["Include jinja2 in lockfiles/requirements for any repo touching jinja2-format prompts","Fail fast at app startup rather than at first prompt construction","Pin jinja2 version to avoid transitive-removal surprises on upgrades"],"tags":["prompts","jinja2","importerror","dependencies","variable-inference"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}