{"record":{"id":"86ba0609bfbe4cc2","repo":"nodejs/node","slug":"attempted-to-invoke-context-filter-without-context","errorCode":null,"errorMessage":"Attempted to invoke context filter without context","messagePattern":"Attempted to invoke context filter without context","errorType":"exception","errorClass":"TemplateRuntimeError","httpStatus":null,"severity":"error","filePath":"tools/inspector_protocol/jinja2/environment.py","lineNumber":455,"sourceCode":"\n    def call_filter(self, name, value, args=None, kwargs=None,\n                    context=None, eval_ctx=None):\n        \"\"\"Invokes a filter on a value the same way the compiler does it.\n\n        Note that on Python 3 this might return a coroutine in case the\n        filter is running from an environment in async mode and the filter\n        supports async execution.  It's your responsibility to await this\n        if needed.\n\n        .. versionadded:: 2.7\n        \"\"\"\n        func = self.filters.get(name)\n        if func is None:\n            fail_for_missing_callable('no filter named %r', name)\n        args = [value] + list(args or ())\n        if getattr(func, 'contextfilter', False):\n            if context is None:\n                raise TemplateRuntimeError('Attempted to invoke context '\n                                           'filter without context')\n            args.insert(0, context)\n        elif getattr(func, 'evalcontextfilter', False):\n            if eval_ctx is None:\n                if context is not None:\n                    eval_ctx = context.eval_ctx\n                else:\n                    eval_ctx = EvalContext(self)\n            args.insert(0, eval_ctx)\n        elif getattr(func, 'environmentfilter', False):\n            args.insert(0, self)\n        return func(*args, **(kwargs or {}))\n\n    def call_test(self, name, value, args=None, kwargs=None):\n        \"\"\"Invokes a test on a value the same way the compiler does it.\n\n        .. versionadded:: 2.7\n        \"\"\"","sourceCodeStart":437,"sourceCodeEnd":473,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/inspector_protocol/jinja2/environment.py#L437-L473","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nenv.call_filter('my_ctx_filter', value)\n# after\nctx = template.new_context({'value': value})\nenv.call_filter('my_ctx_filter', value, context=ctx)","handlingStrategy":"validation","validationCode":"def needs_context(env, name) -> bool:\n    return getattr(env.filters.get(name), 'contextfilter', False)\n\n# before calling:\nif needs_context(env, 'my_ctx_filter') and context is None:\n    context = template.new_context(vars)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["jinja2","filter","context","api"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}