{"record":{"id":"f17a6557af2c8c89","repo":"huggingface/transformers","slug":"input-length-of-input-ids-string-is-input-ids-l","errorCode":null,"errorMessage":"Input length of {input_ids_string} is {input_ids_length}, but `max_length` is set to {generation_config.max_length}. This can lead to unexpected behavior. You should consider increasing `max_length` or, better yet, setting `max_new_tokens`.","messagePattern":"Input length of (.+?) is (.+?), but `max_length` is set to (.+?)\\. This can lead to unexpected behavior\\. You should consider increasing `max_length` or, better yet, setting `max_new_tokens`\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/utils.py","lineNumber":1684,"sourceCode":"                \" generate arguments will also show up in this list)\"\n            )\n\n    def _validate_generated_length(\n        self: \"GenerativePreTrainedModel\", generation_config, input_ids_length, has_default_max_length\n    ):\n        \"\"\"Performs validation related to the resulting generated length\"\"\"\n        # 1. Max length warnings related to poor parameterization\n        if has_default_max_length and generation_config.max_new_tokens is None:\n            # 20 is the default max_length of the generation config\n            warnings.warn(\n                f\"Using the model-agnostic default `max_length` (={generation_config.max_length}) to control the \"\n                \"generation length. We recommend setting `max_new_tokens` to control the maximum length of the \"\n                \"generation.\",\n                UserWarning,\n            )\n        if input_ids_length >= generation_config.max_length:\n            input_ids_string = \"decoder_input_ids\" if self.config.is_encoder_decoder else \"input_ids\"\n            raise ValueError(\n                f\"Input length of {input_ids_string} is {input_ids_length}, but `max_length` is set to\"\n                f\" {generation_config.max_length}. This can lead to unexpected behavior. You should consider\"\n                \" increasing `max_length` or, better yet, setting `max_new_tokens`.\"\n            )\n\n        # 2. Min length warnings due to unfeasible parameter combinations\n        min_length_error_suffix = (\n            \" Generation will stop at the defined maximum length. You should decrease the minimum length and/or \"\n            \"increase the maximum length.\"\n        )\n        if has_default_max_length:\n            min_length_error_suffix += (\n                f\" Note that `max_length` is set to {generation_config.max_length}, its default value.\"\n            )\n        if generation_config.min_length is not None and generation_config.min_length > generation_config.max_length:\n            warnings.warn(\n                f\"Unfeasible length constraints: `min_length` ({generation_config.min_length}) is larger than\"\n                f\" the maximum possible length ({generation_config.max_length}).\" + min_length_error_suffix,","sourceCodeStart":1666,"sourceCodeEnd":1702,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/utils.py#L1666-L1702","documentation":"Generation counts total length: prompt plus new tokens must fit within `max_length`. If `input_ids` (or `decoder_input_ids`) is already at least `max_length` tokens long, there is no room to generate anything, so `generate` raises rather than returning an empty/invalid continuation.","triggerScenarios":"`model.generate(**inputs)` with default `max_length=20` and a prompt >= 20 tokens; a long prompt with a small saved `max_length`; encoder-decoder models where `decoder_input_ids` length >= `max_length`.","commonSituations":"Forgetting `max_new_tokens` so the legacy default `max_length=20` applies (a related UserWarning also fires); truncation disabled in the tokenizer (`truncation=False`) so long documents exceed the limit; configs where `max_length` was tuned for short prompts.","solutions":["Prefer `max_new_tokens`: `model.generate(**inputs, max_new_tokens=200)` — it is computed relative to the prompt length.","Or raise `max_length` above prompt+desired output: `model.generate(**inputs, max_length=input_len + 200)`.","Or truncate the prompt at tokenization: `tokenizer(text, truncation=True, max_length=...)`.","For encoder-decoder models, keep the decoder start short and set `max_new_tokens`."],"exampleFix":"# before\nout = model.generate(**tokenizer(long_doc, return_tensors=\"pt\"))  # prompt >= max_length(20) -> ValueError\n\n# after\nout = model.generate(**tokenizer(long_doc, return_tensors=\"pt\", truncation=True, max_length=1024), max_new_tokens=256)","handlingStrategy":"validation","validationCode":"prompt_len = inputs[\"input_ids\"].shape[-1]\nmax_len = kwargs.get(\"max_length\", model.generation_config.max_length)\nif max_len is not None and prompt_len >= max_len:\n    kwargs[\"max_new_tokens\"] = kwargs.get(\"max_new_tokens\", 64)  # or raise/increase max_length","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer max_new_tokens over max_length everywhere.","Tokenize with truncation=True, max_length=<model budget> for long documents.","Log prompt length vs max_length before generate in pipelines that accept arbitrary user text."],"tags":["generation","max-length","truncation","context-length"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}