{"record":{"id":"5a42048337844fcc","repo":"huggingface/transformers","slug":"there-are-one-or-more-stop-strings-either-in-the","errorCode":null,"errorMessage":"There are one or more stop strings, either in the arguments to `generate` or in the model's generation config, but we could not locate a tokenizer. When generating with stop strings, you must pass the model's tokenizer to the `tokenizer` argument of `generate`.","messagePattern":"There are one or more stop strings, either in the arguments to `generate` or in the model's generation config, but we could not locate a tokenizer\\. When generating with stop strings, you must pass the model's tokenizer to the `tokenizer` argument of `generate`\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/utils.py","lineNumber":1377,"sourceCode":"        self: \"GenerativePreTrainedModel\",\n        generation_config: GenerationConfig,\n        stopping_criteria: StoppingCriteriaList | None,\n        tokenizer: Optional[\"PreTrainedTokenizerBase\"] = None,\n    ) -> StoppingCriteriaList:\n        criteria = StoppingCriteriaList()\n        if generation_config.max_length is not None:\n            max_position_embeddings = getattr(self.config, \"max_position_embeddings\", None)\n            criteria.append(\n                MaxLengthCriteria(\n                    max_length=generation_config.max_length,\n                    max_position_embeddings=max_position_embeddings,\n                )\n            )\n        if generation_config.max_time is not None:\n            criteria.append(MaxTimeCriteria(max_time=generation_config.max_time))\n        if generation_config.stop_strings is not None:\n            if tokenizer is None:\n                raise ValueError(\n                    \"There are one or more stop strings, either in the arguments to `generate` or in the \"\n                    \"model's generation config, but we could not locate a tokenizer. When generating with \"\n                    \"stop strings, you must pass the model's tokenizer to the `tokenizer` argument of `generate`.\"\n                )\n            criteria.append(StopStringCriteria(stop_strings=generation_config.stop_strings, tokenizer=tokenizer))\n        if generation_config._eos_token_tensor is not None:\n            criteria.append(EosTokenCriteria(eos_token_id=generation_config._eos_token_tensor))\n        if (\n            generation_config.is_assistant\n            and generation_config.assistant_confidence_threshold is not None\n            and generation_config.assistant_confidence_threshold > 0\n        ):\n            criteria.append(\n                ConfidenceCriteria(assistant_confidence_threshold=generation_config.assistant_confidence_threshold)\n            )\n        criteria = self._merge_criteria_processor_list(criteria, stopping_criteria)\n        return criteria\n","sourceCodeStart":1359,"sourceCodeEnd":1395,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/utils.py#L1359-L1395","documentation":"Stopping criteria can include stop STRINGS, which must be matched against decoded text and therefore require a tokenizer. `generate` tries to build a `StopStringCriteria` from `generation_config.stop_strings`, and if no tokenizer was passed (and none could be inferred) it raises instead of silently ignoring your stop strings.","triggerScenarios":"`generation_config.stop_strings=[\"\\n\\n\"]` (set in `generate(...)` kwargs or in the model's `generation_config.json`) while calling `model.generate(input_ids, ...)` without `tokenizer=...` — common when inputs are prepared manually instead of via `pipeline` or `model.generate(**tokenizer_inputs)`.","commonSituations":"Using pre-tokenized `input_ids` tensors; models without an attached `tokenizer` attribute; copying a `generation_config.json` from the Hub that ships `stop_strings`; calling a served/wrapped model where only ids are forwarded.","solutions":["Pass the tokenizer to generate: `model.generate(**tokenizer(prompt, return_tensors=\"pt\"), stop_strings=[\"\\n\\n\"], tokenizer=tokenizer)`.","If you tokenize yourself, still pass `tokenizer=tokenizer` alongside `input_ids`.","If stop strings are unwanted, remove `stop_strings` from `model.generation_config` (`model.generation_config.stop_strings = None`) or from your generate kwargs."],"exampleFix":"# before\ninput_ids = tokenizer(prompt, return_tensors=\"pt\").input_ids\nout = model.generate(input_ids, stop_strings=[\"User:\"])  # ValueError: no tokenizer\n\n# after\nout = model.generate(\n    **tokenizer(prompt, return_tensors=\"pt\"),\n    stop_strings=[\"User:\"],\n    tokenizer=tokenizer,\n)","handlingStrategy":"validation","validationCode":"if generation_config.stop_strings and tokenizer is None:\n    raise ValueError(\"stop_strings requires passing `tokenizer` to generate()\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass the full tokenizer output plus `tokenizer=tokenizer` when using stop_strings.","When building generate kwargs programmatically, add tokenizer automatically if `stop_strings` is present.","Prefer stop_strings + tokenizer over manual post-hoc string stopping so the model stops at the right step."],"tags":["generation","stop-strings","tokenizer","stopping-criteria"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}