{"record":{"id":"1d0ffd97ea812566","repo":"huggingface/transformers","slug":"max-new-tokens-must-be-greater-than-0-but-is","errorCode":null,"errorMessage":"`max_new_tokens` must be greater than 0, but is {}.","messagePattern":"`max_new_tokens` must be greater than 0, but is (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/configuration_utils.py","lineNumber":670,"sourceCode":"        Note that some parameters not validated here are best validated at generate runtime, as they may depend on\n        other inputs and/or the model, such as parameters related to the generation length.\n\n        Args:\n            strict (bool): If True, raise an exception for any issues found. If False, only log issues.\n            user_set_attributes (set[str], *optional*): Names of attributes the caller explicitly provided. When\n                supplied, \"minor issue\" warnings about conflicting flag combinations (e.g. sampling-only flags set\n                while `do_sample=False`) only fire if the conflicting flag is in this set -- avoiding noisy warnings\n                when the value was inherited from a model's default `generation_config.json`. When `None`, all set\n                attributes are considered user-set (backward-compatible behavior for direct `validate()` calls).\n        \"\"\"\n        minor_issues = {}  # format: {attribute_name: issue_description}\n\n        # 1. Validation of individual attributes\n        # 1.1. Decoding attributes\n        if self.early_stopping not in {None, True, False, \"never\"}:\n            raise ValueError(f\"`early_stopping` must be a boolean or 'never', but is {self.early_stopping}.\")\n        if self.max_new_tokens is not None and self.max_new_tokens <= 0:\n            raise ValueError(f\"`max_new_tokens` must be greater than 0, but is {self.max_new_tokens}.\")\n        if self.assistant_ensemble_weight is not None and not (0.0 < self.assistant_ensemble_weight < 1.0):\n            raise ValueError(\n                f\"`assistant_ensemble_weight` must be in the open interval `(0.0, 1.0)`, \"\n                f\"but is {self.assistant_ensemble_weight}. Use `None` for standard (lossless) speculative decoding.\"\n            )\n        if self.pad_token_id is not None and self.pad_token_id < 0:\n            minor_issues[\"pad_token_id\"] = (\n                f\"`pad_token_id` should be positive but got {self.pad_token_id}. This will cause errors when batch \"\n                \"generating, if there is padding. Please set `pad_token_id` explicitly as \"\n                \"`model.generation_config.pad_token_id=PAD_TOKEN_ID` to avoid errors in generation\"\n            )\n        # 1.2. Cache attributes\n        # \"paged\" re-routes to continuous batching and so it is a valid cache implementation. But we do not want to test\n        # it with the `generate` as the other would be, so we we cannot add it to ALL_CACHE_IMPLEMENTATIONS\n        valid_cache_implementations = ALL_CACHE_IMPLEMENTATIONS + (\"paged\",)\n        if self.cache_implementation is not None and self.cache_implementation not in valid_cache_implementations:\n            raise ValueError(\n                f\"Invalid `cache_implementation` ({self.cache_implementation}). Choose one of: \"","sourceCodeStart":652,"sourceCodeEnd":688,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/configuration_utils.py#L652-L688","documentation":"GenerationConfig.validate() rejects max_new_tokens <= 0. Zero or negative generation length is meaningless for generate(), so it fails at validation time (which runs when generate() is called) rather than producing empty output silently.","triggerScenarios":"model.generate(max_new_tokens=0), GenerationConfig(max_new_tokens=-5), or computing max_new_tokens from a budget (max_length - prompt_len) that goes non-positive for long prompts.","commonSituations":"Programmatic budgets where prompt length meets/exceeds max_length; CLI/config-driven runs receiving 0 defaults; math like max(0, limit - len(prompt)) instead of skipping generation.","solutions":["Pass a positive max_new_tokens","Guard budget computations: only call generate when the computed budget is >= 1","If you meant 'no new tokens', skip calling generate entirely"],"exampleFix":"# before\nbudget = max_length - input_ids.shape[1]  # can be <= 0\nout = model.generate(input_ids, max_new_tokens=budget)\n# after\nbudget = max_length - input_ids.shape[1]\nout = model.generate(input_ids, max_new_tokens=budget) if budget > 0 else input_ids","handlingStrategy":"validation","validationCode":"def valid_max_new_tokens(v) -> bool:\n    return v is None or (isinstance(v, int) and not isinstance(v, bool) and v > 0)","typeGuard":"def is_positive_token_count(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v > 0","tryCatchPattern":null,"preventionTips":["Skip generate() when the computed budget is <= 0 instead of calling it","Use max(1, budget) only when at least one token is genuinely desired"],"tags":["python","transformers","generation","generation-config","validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}