{"record":{"id":"769149d163aee157","repo":"huggingface/transformers","slug":"when-generating-with-token-healing-you-must-pass","errorCode":null,"errorMessage":" When generating with token healing, you must pass the model's tokenizer to the `tokenizer` argument of `generate`.","messagePattern":" When generating with token healing, you must pass the model's tokenizer to the `tokenizer` argument of `generate`\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/utils.py","lineNumber":2710,"sourceCode":"            if this_peer_finished_flag.item() == 0.0:\n                return False\n        elif this_peer_finished:\n            return False\n        return True\n\n    def heal_tokens(\n        self, input_ids: torch.LongTensor, tokenizer: Optional[\"PreTrainedTokenizerBase\"] = None\n    ) -> torch.LongTensor:\n        r\"\"\"\n        Generates sequences of token ids for models with a language modeling head.\n        Parameters:\n            input_ids (`torch.LongTensor`): The sequence used as a prompt for the generation.\n            tokenizer (`PreTrainedTokenizerBase`, *optional*): The tokenizer used to decode the input ids.\n        Return:\n            `torch.LongTensor` where each sequence has its tail token replaced with its appropriate extension.\n        \"\"\"\n        if tokenizer is None:\n            raise ValueError(\n                \" When generating with token healing, you must pass the model's tokenizer to the `tokenizer` \"\n                \"argument of `generate`.\"\n            )\n\n        bos_token_id, pad_token_id = tokenizer.bos_token_id, tokenizer.pad_token_id\n        vocab_trie = ExtensionsTrie(tokenizer.get_vocab())\n        generation_config = GenerationConfig(max_new_tokens=1, pad_token_id=pad_token_id)\n\n        # assumption: leading/trailing whitespace is not meaningful, so the prompts are\n        # stripped before re-tokenizing to desensitize generation to whitespace artefacts\n        prompts = [p.strip() for p in tokenizer.decode(input_ids, skip_special_tokens=True)]\n        input_ids = tokenizer(\n            prompts,\n            return_tensors=\"pt\",\n            padding=True,\n        ).input_ids.to(input_ids.device)\n\n        # replace bos with pad to not condition healing on it","sourceCodeStart":2692,"sourceCodeEnd":2728,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/utils.py#L2692-L2728","documentation":"Error \" When generating with token healing, you must pass the model's tokenizer to the `tokenizer` argument of `generate`.\" thrown in huggingface/transformers.","triggerScenarios":"Raised in generate() when token healing is enabled but no tokenizer was passed to generate().","commonSituations":"Using token healing (heal_tokens / related flag) without forwarding the model tokenizer into generate().","solutions":["Pass the model's tokenizer via `generate(..., tokenizer=tokenizer)` when using token healing.","Disable token healing if no tokenizer is available."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}