{"record":{"id":"7749d979502fc899","repo":"huggingface/transformers","slug":"invalid-value-for-do-sample-expected-a-boolean","errorCode":null,"errorMessage":"Invalid value for `do_sample`: expected a boolean, got {type(generation_config.do_sample).__name__}","messagePattern":"Invalid value for `do_sample`: expected a boolean, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/utils.py","lineNumber":1109,"sourceCode":"                    inputs_tensor=inputs_tensor,\n                    logits_processor=logits_processor,\n                    target_tokenizer=target_tokenizer,\n                    assistant_tokenizer=assistant_tokenizer,\n                    atm_translator=atm_translator,\n                )\n            elif generation_config.do_sample is False:\n                candidate_generator = AssistedCandidateGeneratorDifferentTokenizers(\n                    input_ids=input_ids,\n                    assistant_model=assistant_model,\n                    generation_config=generation_config,\n                    model_kwargs=model_kwargs,\n                    inputs_tensor=inputs_tensor,\n                    logits_processor=logits_processor,\n                    target_tokenizer=target_tokenizer,\n                    assistant_tokenizer=assistant_tokenizer,\n                )\n            else:\n                raise ValueError(\n                    f\"Invalid value for `do_sample`: expected a boolean, got {type(generation_config.do_sample).__name__}\"\n                )\n        else:\n            candidate_generator = AssistedCandidateGenerator(\n                input_ids=input_ids,\n                assistant_model=assistant_model,\n                generation_config=generation_config,\n                model_kwargs=model_kwargs,\n                inputs_tensor=inputs_tensor,\n                logits_processor=logits_processor,\n            )\n        return candidate_generator\n\n    def _get_logits_processor(\n        self: \"GenerativePreTrainedModel\",\n        generation_config: GenerationConfig,\n        input_ids_seq_length: int | None = None,\n        encoder_input_ids: torch.LongTensor | None = None,","sourceCodeStart":1091,"sourceCodeEnd":1127,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/utils.py#L1091-L1127","documentation":"During assisted generation where the assistant model uses a DIFFERENT tokenizer, the code selects the candidate generator by branching on `generation_config.do_sample is True` / `is False`. These identity checks only match real Python booleans, so any other type (int, string, None, numpy.bool_) falls into the else branch and raises. The error names the offending type so you can find where the non-boolean value came from.","triggerScenarios":"Calling `model.generate(..., assistant_model=assistant, assistant_tokenizer=...)` (or any setup where main/assistant vocab sizes differ) while `do_sample` is not a strict Python `bool`: `do_sample=1`, `do_sample=0`, `do_sample=\"true\"`, `do_sample=None`, or a `numpy.bool_` loaded from a config file or constructed programmatically.","commonSituations":"GenerationConfig saved to/from JSON with integer 1/0 instead of true/false; configs built by tooling or YAML loaders that coerce booleans; passing numpy booleans from data pipelines; hand-written `GenerationConfig(do_sample=1)`.","solutions":["Set `do_sample` to a real Python boolean: `generation_config.do_sample = bool(generation_config.do_sample)` (or `True`/`False`) before calling generate.","Check where the value was set: inspect `model.generation_config.do_sample`, any `GenerationConfig` passed to `generate`, and any saved `generation_config.json` for non-boolean values.","If loading configs from JSON/YAML, normalize types after load: `cfg.do_sample = bool(cfg.do_sample)`.","If you intended sampling behavior, verify `do_sample=True` plus temperature/top_p are set after fixing the type."],"exampleFix":"// before\ngeneration_config.do_sample = 1  # int, fails identity check `is True`/`is False`\nout = model.generate(**inputs, assistant_model=assistant, assistant_tokenizer=assistant_tok, generation_config=generation_config)\n\n// after\ngeneration_config.do_sample = True  # real Python bool\nout = model.generate(**inputs, assistant_model=assistant, assistant_tokenizer=assistant_tok, generation_config=generation_config)","handlingStrategy":"type-guard","validationCode":"gc = model.generation_config\nif gc.do_sample is not None and not isinstance(gc.do_sample, bool):\n    gc.do_sample = bool(gc.do_sample)","typeGuard":"def is_bool_do_sample(gc) -> bool:\n    return gc.do_sample is None or isinstance(gc.do_sample, bool)","tryCatchPattern":"try:\n    out = model.generate(**inputs, assistant_model=assistant, assistant_tokenizer=a_tok)\nexcept ValueError as e:\n    if \"Invalid value for `do_sample`\" in str(e):\n        model.generation_config.do_sample = bool(model.generation_config.do_sample)\n        out = model.generate(**inputs, assistant_model=assistant, assistant_tokenizer=a_tok)\n    else:\n        raise","preventionTips":["Always write do_sample as a literal True/False, never 1/0.","After loading GenerationConfig from JSON/YAML, normalize boolean fields with bool().","In shared inference helpers, assert isinstance(gc.do_sample, bool) before generate."],"tags":["generation","assisted-decoding","type-validation","do-sample"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}