{"record":{"id":"aaa2ec250740aa2c","repo":"unslothai/unsloth","slug":"every-seed-must-be-between-0-and-2-53-1","errorCode":null,"errorMessage":"every seed must be between 0 and 2**53 - 1","messagePattern":"every seed must be between 0 and 2\\*\\*53 - 1","errorType":"validation","errorClass":"ValueError","httpStatus":422,"severity":"error","filePath":"studio/backend/models/inference.py","lineNumber":2978,"sourceCode":"        max_length = 32,\n        description = \"Per-image seeds for batched generation: one image per seed (with \"\n        \"`prompts`, lengths must match; alone, every image uses `prompt`). Each image is \"\n        \"individually reproducible from its own seed.\",\n    )\n\n    @field_validator(\"prompts\")\n    @classmethod\n    def _non_empty_prompts(cls, value: Optional[list[str]]) -> Optional[list[str]]:\n        if value is not None and any(not p.strip() for p in value):\n            raise ValueError(\"every prompt in prompts must be non-empty\")\n        return value\n\n    @field_validator(\"seeds\")\n    @classmethod\n    def _seeds_json_safe(cls, value: Optional[list[int]]) -> Optional[list[int]]:\n        # Same JSON safe-integer bound as `seed`, so every per-image seed survives the gallery recipe.\n        if value is not None and any(s < 0 or s > 2**53 - 1 for s in value):\n            raise ValueError(\"every seed must be between 0 and 2**53 - 1\")\n        return value\n\n    @model_validator(mode = \"after\")\n    def _prompts_seeds_lengths_match(self) -> \"DiffusionGenerateRequest\":\n        if (\n            self.prompts is not None\n            and self.seeds is not None\n            and len(self.prompts) != len(self.seeds)\n        ):\n            raise ValueError(\n                f\"prompts and seeds must have the same length (got {len(self.prompts)} \"\n                f\"prompts, {len(self.seeds)} seeds)\"\n            )\n        return self\n\n    # Image-conditioned workflows (base64 or data-URL): init_image alone runs img2img, init_image + mask_image runs inpaint.\n    # Cap each base64 string so one request cannot buffer a multi-GB payload; ~32 MiB fits a full 4096px image.\n    init_image: Optional[str] = Field(","sourceCodeStart":2960,"sourceCodeEnd":2996,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/models/inference.py#L2960-L2996","documentation":"Raised by the seeds field_validator on DiffusionGenerateRequest when any per-image seed is negative or exceeds 2**53-1. The bound matches the single-seed field so every per-image seed survives round-tripping through the gallery recipe, which stores recipes as JSON where integers beyond 2**53-1 lose precision.","triggerScenarios":"Sending seeds: [-1, 42] or seeds: [2**53] with a generation request; typically from generating 64-bit random seeds client-side instead of 53-bit.","commonSituations":"Using secrets.randbits(64) or UUID-derived integers for seeds; porting torch seed ranges; negative seeds from signed arithmetic on unsigned values.","solutions":["Generate seeds in [0, 2**53-1], e.g. random.randrange(2**53) in Python.","Mask incoming 64-bit seeds: seed & ((1 << 53) - 1).","Take abs() of computed seeds and reject the result if it still exceeds the cap."],"exampleFix":"# before\nseeds = [secrets.randbits(64) for _ in prompts]  # exceeds 2**53-1\n\n# after\nseeds = [random.randrange(2 ** 53) for _ in prompts]","handlingStrategy":"validation","validationCode":"MAX_SEED = 2 ** 53 - 1\ndef valid_seeds(seeds: list[int] | None) -> bool:\n    return seeds is None or all(0 <= s <= MAX_SEED for s in seeds)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Generate seeds with random.randrange(2**53), not randbits(64)","Mask external seeds: s & (2**53 - 1)","Keep the single-seed and per-image-seed generation code paths sharing one clamp helper"],"tags":["pydantic","validation","diffusion","seeds","json-safe-integers"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}