nodejs/node · error · ValueError

buffer size too small

Error message

buffer size too small

What it means

enable_buffering(size) on a Template turns on output buffering for the streaming generate() generator, concatenating `size` items before yielding each chunk. A size of 0 or 1 provides no buffering benefit and is rejected as a programming error.

Source

Thrown at tools/inspector_protocol/jinja2/environment.py:1262

        while 1:
            try:
                while c_size < size:
                    c = next(self._gen)
                    push(c)
                    if c:
                        c_size += 1
            except StopIteration:
                if not c_size:
                    return
            yield concat(buf)
            del buf[:]
            c_size = 0

    def enable_buffering(self, size=5):
        """Enable buffering.  Buffer `size` items before yielding them."""
        if size <= 1:
            raise ValueError('buffer size too small')

        self.buffered = True
        self._next = partial(next, self._buffered_generator(size))

    def __iter__(self):
        return self

    def __next__(self):
        return self._next()


# hook in default template class.  if anyone reads this comment: ignore that
# it's possible to use custom templates ;-)
Environment.template_class = Template

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Pass an integer >= 2, e.g. template.enable_buffering(5).
  2. Validate the value before calling: if buffer_size > 1: tmpl.enable_buffering(buffer_size).
  3. To disable buffering, set template.buffered = False instead of calling enable_buffering with a tiny size.

Example fix

// before
tmpl.enable_buffering(1)  # raises: buffer size too small

// after
tmpl.enable_buffering(5)
# or to disable buffering:
tmpl.buffered = False
Defensive patterns

Strategy: validation

Validate before calling

def safe_enable_buffering(tmpl, size):
    if not isinstance(size, int) or size <= 1:
        raise ValueError('buffer size must be an integer >= 2, got %r' % (size,))
    tmpl.enable_buffering(size)

Type guard

def is_valid_buffer_size(size) -> bool:
    return isinstance(size, int) and size >= 2

Prevention

When it happens

Trigger: Calling template.enable_buffering(0), template.enable_buffering(1), or passing any value <= 1 (e.g. computed from a config that defaults to 1).

Common situations: Wiring enable_buffering to a tunable that defaults to 1; misreading the parameter as a byte size instead of an item count; trying to disable buffering by passing 1.

Related errors


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