microsoft/graphrag · error · KeyError
Template '{template_name}' not found.
Error message
Template '{template_name}' not found. What it means
JinjaTemplateEngine.render raises this KeyError when the named template cannot be found: it is not in the in-memory cache and the underlying TemplateManager.get() returned None for it. This is the template-resolution failure path before any Jinja compilation happens.
Source
Thrown at packages/graphrag-llm/graphrag_llm/templating/jinja_template_engine.py:40
def __init__(self, *, template_manager: "TemplateManager", **kwargs: Any) -> None:
"""Initialize the template engine.
Args
----
template_manager: TemplateManager
The template manager to use for loading templates.
"""
self._templates = {}
self._template_manager = template_manager
def render(self, template_name: str, context: dict[str, Any]) -> str:
"""Render a template with the given context."""
jinja_template = self._templates.get(template_name)
if jinja_template is None:
template_contents = self._template_manager.get(template_name)
if template_contents is None:
msg = f"Template '{template_name}' not found."
raise KeyError(msg)
jinja_template = Template(template_contents, undefined=StrictUndefined)
self._templates[template_name] = jinja_template
try:
return jinja_template.render(**context)
except UndefinedError as e:
msg = f"Missing key in context for template '{template_name}': {e.message}"
raise KeyError(msg) from e
except Exception as e:
msg = f"Error rendering template '{template_name}': {e!s}"
raise RuntimeError(msg) from e
@property
def template_manager(self) -> "TemplateManager":
"""Template manager associated with this engine."""
return self._template_manager
View on GitHub (pinned to f40e9a26ce)
Solutions
- Check what templates exist: list files in the templates directory or keys registered on the template manager
- Correct the template name to match, respecting the configured template_extension
- Add/deploy the missing template file
- If using a custom TemplateManager, ensure it registers templates under the expected names
Example fix
# before
engine.render("summarze", ctx)
# after
engine.render("summarize", ctx) Defensive patterns
Strategy: type-guard
Validate before calling
if engine.template_manager.get(template_name) is None:
raise KeyError(f"Missing template: {template_name}") Type guard
def template_exists(engine, name: str) -> bool:
return engine.template_manager.get(name) is not None Try / catch
try:
out = engine.render(name, ctx)
except KeyError as e:
if "not found" in str(e):
# surface missing-template error with dir listing for debugging
raise
raise Prevention
- Preflight check template_manager.get(name) before render
- Standardize template naming and extension conventions
- Ship a template manifest and assert it at startup
When it happens
Trigger: Calling render(template_name, context) with a name that has no corresponding template file (wrong name, missing extension handling, or the templates directory simply lacks the file).
Common situations: Template file renamed or not deployed, name mismatch (e.g. 'prompt' vs 'prompt.txt' depending on template_extension), or using an in-memory template manager without registering the template.
Related errors
- Missing key in context for template '{template_name}': {e.me
- Error rendering template '{template_name}': {e!s}
- Templates directory '{base_dir}' does not exist or is not a
- TemplateEngineConfig.type '{strategy}' is not registered in
- MetricsConfig.store '{strategy}' is not registered in the Me
AI-assisted analysis of microsoft/graphrag@f40e9a26ce (2026-08-27).
Data as JSON: /api/errors/591ca967b46606c7.
Report an issue: GitHub.