{"record":{"id":"c7a61437da91ac04","repo":"microsoft/autogen","slug":"head-size-must-be-greater-than-0","errorCode":null,"errorMessage":"head_size must be greater than 0.","messagePattern":"head_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":35,"sourceCode":"\nclass HeadAndTailChatCompletionContext(ChatCompletionContext, Component[HeadAndTailChatCompletionContextConfig]):\n    \"\"\"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","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/model_context/_head_and_tail_chat_completion_context.py#L17-L53","documentation":"HeadAndTailChatCompletionContext validates both constructor size arguments; head_size <= 0 raises ValueError immediately. The head keeps the oldest head_size messages (typically the initial system prompt), so a non-positive head would silently drop the system messages — hence fail-fast at construction.","triggerScenarios":"`HeadAndTailChatCompletionContext(head_size=0, tail_size=5)` or a negative/computed head_size that lands at 0. Distinct from error 596: this one names head_size, meaning the head argument failed validation first.","commonSituations":"Configs where 0 means 'no head' — not supported; deriving head_size from token budgets that compute to 0 for short prompts; copy-paste of the tail_size value into both parameters during refactor.","solutions":["Pass head_size >= 1; a typical value is 1 to preserve just the system message.","If you intended 'keep everything recent only', use BufferedChatCompletionContext(tail) semantics instead.","Clamp computed budgets with max(1, value)."],"exampleFix":"# before\nctx = HeadAndTailChatCompletionContext(head_size=0, tail_size=10)  # ValueError\n\n# after\nctx = HeadAndTailChatCompletionContext(head_size=1, tail_size=10)  # keep system message + last 10","handlingStrategy":"validation","validationCode":"if not (isinstance(head_size, int) and head_size >= 1):\n    raise ValueError(\"head_size must be a positive integer\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use head_size=1 as the minimum when you only need to preserve the system message.","Validate both sizes together at config-load time."],"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"}