{"record":{"id":"d51864f15ae53bb6","repo":"deepset-ai/haystack","slug":"approximate-summary-tokens-must-be-a-positive-nu","errorCode":null,"errorMessage":"`approximate_summary_tokens` must be a positive number of tokens, got {approximate_summary_tokens}.","messagePattern":"`approximate_summary_tokens` must be a positive number of tokens, got (.+?)\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"haystack/hooks/compaction/summarization.py","lineNumber":233,"sourceCode":"        :param min_keep_steps: The fewest complete recent Agent steps to keep, even when they exceed the target.\n        :param approximate_summary_tokens: About how long you expect a summary to come out. This is an estimate used\n            for planning, not a limit imposed on the model. The compactor uses it to work out how much of the\n            conversation to summarize. A higher value causes the compactor to summarize more of the conversation per\n            round, so the result is likelier to land under the target, at the cost of giving up more of the\n            conversation. A lower value summarizes less per round and keeps more, but may leave the result above the\n            target.\n        :param summary_instruction: The prompt instructions for how to summarize a portion of the conversation.\n            The default instructions ask for a summary with fixed sections covering the objective, decisions and\n            constraints, completed work, exact identifiers, and unresolved work.\n        :param raise_on_failure: Whether to raise an exception if the chat generator fails or returns a summary that\n            does not shrink the conversation. By default the failure is logged and any successful partial compaction\n            is returned.\n        :raises ValueError: If `min_keep_steps` is negative or `approximate_summary_tokens` is not positive.\n        \"\"\"\n        if min_keep_steps < 0:\n            raise ValueError(f\"`min_keep_steps` must be at least 0, got {min_keep_steps}.\")\n        if approximate_summary_tokens < 1:\n            raise ValueError(\n                f\"`approximate_summary_tokens` must be a positive number of tokens, got {approximate_summary_tokens}.\"\n            )\n        self.chat_generator = chat_generator\n        self.min_keep_steps = min_keep_steps\n        self.approximate_summary_tokens = approximate_summary_tokens\n        self.summary_instruction = summary_instruction\n        self.raise_on_failure = raise_on_failure\n\n    def compact(\n        self, messages: list[ChatMessage], target_tokens: int, token_counter: TokenCounter\n    ) -> list[ChatMessage] | None:\n        \"\"\"\n        Return a progressively summarized conversation, or None when no useful reduction is possible.\n\n        :param messages: The conversation to compact, ordered oldest to newest.\n        :param target_tokens: The token budget the compacted messages should aim to fit.\n        :param token_counter: The counter used both to plan compaction and verify generated summaries.\n        :returns: A smaller replacement conversation, or None when nothing was reduced.","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/hooks/compaction/summarization.py#L215-L251","documentation":"SummarizationCompactor also requires approximate_summary_tokens >= 1 (haystack/hooks/compaction/summarization.py:233); it uses this estimate when building the summarization prompt. Zero or negative values would produce an invalid token budget.","triggerScenarios":"Constructing SummarizationCompactor with approximate_summary_tokens=0 or negative, or a None/empty config value coerced to 0.","commonSituations":"Placeholder 0 meant to be 'auto'; reading the value from an unset env var; misconfigured summary-length settings.","solutions":["Set approximate_summary_tokens to a realistic positive estimate, e.g. 512.","Clamp: approximate_summary_tokens = max(1, value).","Provide a default (e.g. 500-1000 tokens) in config when unset."],"exampleFix":"// before\ncompactor = SummarizationCompactor(chat_generator=g, approximate_summary_tokens=0)\n// after\ncompactor = SummarizationCompactor(chat_generator=g, approximate_summary_tokens=512)","handlingStrategy":"validation","validationCode":"def validate_summary_tokens(v):\n    if not isinstance(v, int) or v < 1:\n        raise ValueError(f\"approximate_summary_tokens must be >= 1, got {v!r}\")\nvalidate_summary_tokens(cfg.get(\"approximate_summary_tokens\", 512))","typeGuard":"def is_positive_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 1","tryCatchPattern":"try:\n    compactor = SummarizationCompactor(chat_generator=gen, approximate_summary_tokens=t)\nexcept ValueError as e:\n    logger.error(\"bad approximate_summary_tokens: %s\", e)\n    compactor = SummarizationCompactor(chat_generator=gen, approximate_summary_tokens=512)","preventionTips":["Never use 0 as an 'auto' placeholder; pick a real budget like 512","Read env/config with a non-zero default","Validate alongside min_keep_steps before construction"],"tags":["validation","constructor","config","compaction"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}