microsoft/autogen · error · ValueError

buffer_size must be greater than 0.

Error message

buffer_size must be greater than 0.

What it means

BufferedChatCompletionContext keeps only the most recent buffer_size messages and rejects non-positive sizes at construction with ValueError, since a buffer of 0 (or negative) would make get_messages always return an empty list and silently starve the model of all context. This is a fail-fast configuration check on the constructor argument.

Source

Thrown at python/packages/autogen-core/src/autogen_core/model_context/_buffered_chat_completion_context.py:31

    initial_messages: List[LLMMessage] | None = None


class BufferedChatCompletionContext(ChatCompletionContext, Component[BufferedChatCompletionContextConfig]):
    """A buffered chat completion context that keeps a view of the last n messages,
    where n is the buffer size. The buffer size is set at initialization.

    Args:
        buffer_size (int): The size of the buffer.
        initial_messages (List[LLMMessage] | None): The initial messages.
    """

    component_config_schema = BufferedChatCompletionContextConfig
    component_provider_override = "autogen_core.model_context.BufferedChatCompletionContext"

    def __init__(self, buffer_size: int, initial_messages: List[LLMMessage] | None = None) -> None:
        super().__init__(initial_messages)
        if buffer_size <= 0:
            raise ValueError("buffer_size must be greater than 0.")
        self._buffer_size = buffer_size

    async def get_messages(self) -> List[LLMMessage]:
        """Get at most `buffer_size` recent messages."""
        messages = self._messages[-self._buffer_size :]
        # Handle the first message is a function call result message.
        if messages and isinstance(messages[0], FunctionExecutionResultMessage):
            # Remove the first message from the list.
            messages = messages[1:]
        return messages

    def _to_config(self) -> BufferedChatCompletionContextConfig:
        return BufferedChatCompletionContextConfig(
            buffer_size=self._buffer_size, initial_messages=self._initial_messages
        )

    @classmethod
    def _from_config(cls, config: BufferedChatCompletionContextConfig) -> Self:

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Pass a positive integer: `BufferedChatCompletionContext(buffer_size=10)`.
  2. If 0 in your config means unlimited, use UnboundedChatCompletionContext instead of mapping it to buffer_size.
  3. Clamp computed values: `buffer_size=max(1, computed)` when the source can legitimately be 0.

Example fix

# before
context = BufferedChatCompletionContext(buffer_size=0)  # ValueError

# after
from autogen_core.model_context import UnboundedChatCompletionContext
context = UnboundedChatCompletionContext()  # if '0' meant unlimited
# or: BufferedChatCompletionContext(buffer_size=10)
Defensive patterns

Strategy: validation

Validate before calling

buffer_size = int(cfg.get("buffer_size", 10))
if buffer_size <= 0:
    raise ValueError("buffer_size must be > 0; use UnboundedChatCompletionContext for unlimited")

Prevention

When it happens

Trigger: `BufferedChatCompletionContext(buffer_size=0)`, negative values, or a computed size that evaluates to 0 (e.g. `len(something)` on an empty collection, or a config default of 0 meaning 'unlimited'). Note: there is no unlimited option — omit the context or use UnboundedChatCompletionContext for that.

Common situations: Config files where 0 conventionally means 'default/unlimited'; deriving buffer_size from message counts or model limits that can be 0 in edge cases; disabling buffering by passing 0.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/6194cb85329e28c8. Report an issue: GitHub.