{"record":{"id":"69abf5cdfc016803","repo":"microsoft/autogen","slug":"tail-size-must-be-greater-than-0","errorCode":null,"errorMessage":"tail_size must be greater than 0.","messagePattern":"tail_size must be greater than 0\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/model_context/_head_and_tail_chat_completion_context.py","lineNumber":37,"sourceCode":"    \"\"\"A chat completion context that keeps a view of the first n and last m messages,\n    where n is the head size and m is the tail size. The head and tail sizes\n    are set at initialization.\n\n    Args:\n        head_size (int): The size of the head.\n        tail_size (int): The size of the tail.\n        initial_messages (List[LLMMessage] | None): The initial messages.\n    \"\"\"\n\n    component_config_schema = HeadAndTailChatCompletionContextConfig\n    component_provider_override = \"autogen_core.model_context.HeadAndTailChatCompletionContext\"\n\n    def __init__(self, head_size: int, tail_size: int, initial_messages: List[LLMMessage] | None = None) -> None:\n        super().__init__(initial_messages)\n        if head_size <= 0:\n            raise ValueError(\"head_size must be greater than 0.\")\n        if tail_size <= 0:\n            raise ValueError(\"tail_size must be greater than 0.\")\n        self._head_size = head_size\n        self._tail_size = tail_size\n\n    async def get_messages(self) -> List[LLMMessage]:\n        \"\"\"Get at most `head_size` recent messages and `tail_size` oldest messages.\"\"\"\n        head_messages = self._messages[: self._head_size]\n        # Handle the last message is a function call message.\n        if (\n            head_messages\n            and isinstance(head_messages[-1], AssistantMessage)\n            and isinstance(head_messages[-1].content, list)\n            and all(isinstance(item, FunctionCall) for item in head_messages[-1].content)\n        ):\n            # Remove the last message from the head.\n            head_messages = head_messages[:-1]\n\n        tail_messages = self._messages[-self._tail_size :]\n        # Handle the first message is a function call result message.","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/model_context/_head_and_tail_chat_completion_context.py#L19-L55","documentation":"The second validation in HeadAndTailChatCompletionContext.__init__: after head_size passes, tail_size <= 0 raises ValueError. The tail holds the most recent tail_size messages, and a zero tail would make the context return effectively nothing useful, so the constructor refuses it. If you see this specific message, head_size was already valid.","triggerScenarios":"`HeadAndTailChatCompletionContext(head_size=1, tail_size=0)` or negative tail; computed tail sizes (e.g. from remaining token budget) hitting 0 for cheap models/short histories.","commonSituations":"Token-budget arithmetic that can round to 0; configs reusing a single 'size' knob for both parameters where it is 0; migrating from BufferedChatCompletionContext(buffer_size=0) code paths.","solutions":["Pass tail_size >= 1.","Guard budget-derived values: `tail = max(1, budget // avg_msg_tokens)`.","Consider TokenLimitedChatCompletionContext when the real goal is token-budget trimming rather than fixed counts."],"exampleFix":"# before\nctx = HeadAndTailChatCompletionContext(head_size=1, tail_size=0)  # ValueError\n\n# after\nctx = HeadAndTailChatCompletionContext(head_size=1, tail_size=20)","handlingStrategy":"validation","validationCode":"if not (isinstance(tail_size, int) and tail_size >= 1):\n    raise ValueError(\"tail_size must be a positive integer\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard token-budget-derived sizes with max(1, value).","Consider TokenLimitedChatCompletionContext when trimming by token count rather than message count."],"tags":["python","autogen-core","model-context","configuration","validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}