nodejs/node · error · TemplateRuntimeError
Attempted to invoke context filter without context
Error message
Attempted to invoke context filter without context
What it means
Raised by Environment.call_filter (jinja2 environment.py) when the resolved filter function is decorated with @contextfilter (it needs the render Context) but call_filter was invoked without a context argument. Context filters can only run inside the template engine, which always passes the active context; calling them through the public call_filter API without one is unsupported.
Source
Thrown at tools/inspector_protocol/jinja2/environment.py:455
def call_filter(self, name, value, args=None, kwargs=None,
context=None, eval_ctx=None):
"""Invokes a filter on a value the same way the compiler does it.
Note that on Python 3 this might return a coroutine in case the
filter is running from an environment in async mode and the filter
supports async execution. It's your responsibility to await this
if needed.
.. versionadded:: 2.7
"""
func = self.filters.get(name)
if func is None:
fail_for_missing_callable('no filter named %r', name)
args = [value] + list(args or ())
if getattr(func, 'contextfilter', False):
if context is None:
raise TemplateRuntimeError('Attempted to invoke context '
'filter without context')
args.insert(0, context)
elif getattr(func, 'evalcontextfilter', False):
if eval_ctx is None:
if context is not None:
eval_ctx = context.eval_ctx
else:
eval_ctx = EvalContext(self)
args.insert(0, eval_ctx)
elif getattr(func, 'environmentfilter', False):
args.insert(0, self)
return func(*args, **(kwargs or {}))
def call_test(self, name, value, args=None, kwargs=None):
"""Invokes a test on a value the same way the compiler does it.
.. versionadded:: 2.7
"""View on GitHub (pinned to 1b2de5e052)
Solutions
- Pass a context: `env.call_filter('my_ctx_filter', value, context=ctx)` where ctx is a real Context obtained from template.new_context().
- If you do not have a context, use a non-contextfilter version of the filter (drop @contextfilter or wrap it).
- Run such filters from inside a template expression where the engine injects context automatically.
Example fix
# before
env.call_filter('my_ctx_filter', value)
# after
ctx = template.new_context({'value': value})
env.call_filter('my_ctx_filter', value, context=ctx) Defensive patterns
Strategy: validation
Validate before calling
def needs_context(env, name) -> bool:
return getattr(env.filters.get(name), 'contextfilter', False)
# before calling:
if needs_context(env, 'my_ctx_filter') and context is None:
context = template.new_context(vars) Prevention
- Call @contextfilter filters from inside templates, where context is injected.
- When calling via call_filter, always obtain a real Context first.
- Provide a non-context wrapper if you need offline use.
When it happens
Trigger: Calling `env.call_filter('my_ctx_filter', value)` where my_ctx_filter is a @contextfilter, and omitting the context= kwarg; reusing a context filter as a plain Python function from outside a template.
Common situations: Sharing a custom filter between template use (needs context) and ad-hoc script use (no context); calling a third-party @contextfilter from application code rather than from inside a template.
Related errors
- %s (%s; did you forget to quote the callable name?)
- either extensions or filter_func can be passed, but not both
- You can only sort by either "key" or "value"
- can't handle positional and keyword arguments at the same ti
- method must be common, ceil or floor
AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13).
Data as JSON: /api/errors/86ba0609bfbe4cc2.
Report an issue: GitHub.