microsoft/graphrag · error · RuntimeError
Error rendering template '{template_name}': {e!s}
Error message
Error rendering template '{template_name}': {e!s} What it means
JinjaTemplateEngine.render wraps any non-UndefinedError exception from jinja_template.render() in a RuntimeError with the template name and the original message. This catches genuine Jinja failures such as syntax errors in the template, bad filters, or invalid operations on context values, chaining the original exception as __cause__.
Source
Thrown at packages/graphrag-llm/graphrag_llm/templating/jinja_template_engine.py:50
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
- Inspect the chained original exception (e.__cause__) and message for the underlying Jinja error
- Fix the template syntax/filter or load the required Jinja extension
- Validate the types of context values passed to render
- Preview/test templates with jinja2.Template(...).render in isolation to confirm they compile
Example fix
# before
{{ name | nosuchfilter }}
# after
{{ name | upper }} Defensive patterns
Strategy: try-catch
Validate before calling
from jinja2 import Environment, TemplateSyntaxError
src = engine.template_manager.get(name)
if src is not None:
try:
Environment().parse(src)
except TemplateSyntaxError as e:
raise ValueError(f"Template {name} has syntax error: {e}") from e Type guard
from jinja2 import Environment, TemplateSyntaxError
def template_compiles(src: str) -> bool:
try:
Environment().parse(src)
return True
except TemplateSyntaxError:
return False Try / catch
try:
out = engine.render(name, ctx)
except RuntimeError as e:
log.error("Template render failed: %s", e)
if e.__cause__ is not None:
log.error("Underlying Jinja error: %r", e.__cause__)
raise Prevention
- Parse/compile templates once at startup to catch syntax errors early
- Type-check context values before render
- Load required Jinja extensions when constructing engines
When it happens
Trigger: Calling render() with a template containing Jinja syntax errors, applying an undefined/nonexistent filter, or performing invalid operations (e.g. arithmetic on None) on context values.
Common situations: Hand-edited templates with syntax mistakes, filters requiring extra extensions not loaded, or context values with unexpected types.
Related errors
- Template '{template_name}' not found.
- Missing key in context for template '{template_name}': {e.me
- 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/c98d2658906fd162.
Report an issue: GitHub.