nodejs/node · error · RuntimeError

The environment was not created with async mode enabled.

Error message

The environment was not created with async mode enabled.

What it means

Raised by Template.render_async (in jinja2 asyncsupport.py) when the template's environment was not constructed with async support enabled. render_async is the coroutine variant of render(); it requires that Environment(enable_async=True) was set at construction time so that the async code path is wired in.

Source

Thrown at tools/inspector_protocol/jinja2/asyncsupport.py:60

def wrap_generate_func(original_generate):
    def _convert_generator(self, loop, args, kwargs):
        async_gen = self.generate_async(*args, **kwargs)
        try:
            while 1:
                yield loop.run_until_complete(async_gen.__anext__())
        except StopAsyncIteration:
            pass
    def generate(self, *args, **kwargs):
        if not self.environment.is_async:
            return original_generate(self, *args, **kwargs)
        return _convert_generator(self, asyncio.get_event_loop(), args, kwargs)
    return update_wrapper(generate, original_generate)


async def render_async(self, *args, **kwargs):
    if not self.environment.is_async:
        raise RuntimeError('The environment was not created with async mode '
                           'enabled.')

    vars = dict(*args, **kwargs)
    ctx = self.new_context(vars)

    try:
        return await concat_async(self.root_render_func(ctx))
    except Exception:
        exc_info = sys.exc_info()
    return self.environment.handle_exception(exc_info, True)


def wrap_render_func(original_render):
    def render(self, *args, **kwargs):
        if not self.environment.is_async:
            return original_render(self, *args, **kwargs)
        loop = asyncio.get_event_loop()
        return loop.run_until_complete(self.render_async(*args, **kwargs))

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Construct the Environment with enable_async=True: `Environment(loader=..., enable_async=True)`.
  2. If you must keep a sync Environment, call the regular render()/generate() instead of the *_async variants.
  3. For async frameworks, follow their Jinja2 integration docs (e.g. starlette's Jinja2Templates) which set enable_async for you.

Example fix

# before
env = Environment(loader=FileSystemLoader('tpl'))
await env.get_template('x.html').render_async()
# after
env = Environment(loader=FileSystemLoader('tpl'), enable_async=True)
await env.get_template('x.html').render_async()
Defensive patterns

Strategy: validation

Validate before calling

def async_env(**kw):
    from jinja2 import Environment
    kw.setdefault('enable_async', True)
    return Environment(**kw)

assert env.is_async, 'enable_async was not set on this Environment'

Type guard

def supports_async(template) -> bool:
    return getattr(template.environment, 'is_async', False)

Prevention

When it happens

Trigger: Calling `await template.render_async(...)` on a Template produced by an Environment whose enable_async kwarg was False or omitted; reusing a sync-configured Environment (e.g. a shared Flask/Django env) for async rendering; loading asyncsupport's render_async onto a sync environment.

Common situations: Migrating a sync Jinja2 setup to an async web framework (Starlette, FastAPI, aiohttp) and forgetting to flip enable_async; sharing one module-level Environment across sync and async code paths; upgrading Jinja2 and assuming async is on by default.

Related errors


AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13). Data as JSON: /api/errors/424578253cf43355. Report an issue: GitHub.