{"record":{"id":"c78888013727cdfa","repo":"huggingface/transformers","slug":"pretrained-model-name-or-path-does-not-contain","errorCode":null,"errorMessage":"`{pretrained_model_name_or_path}` does not contain a `custom_generate` subdirectory with a `generate.py` file, can't load the custom generate function.","messagePattern":"`(.+?)` does not contain a `custom_generate` subdirectory with a `generate\\.py` file, can't load the custom generate function\\.","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/utils.py","lineNumber":489,"sourceCode":"                should only be set to `True` for repositories you trust and in which you have read the code, as it will\n                execute code present on the Hub on your local machine.\n            **kwargs:\n                Additional keyword arguments for remote code loading.\n\n        Raises:\n            OSError: If `pretrained_model_name_or_path` does not contain a `custom_generate` subdirectory.\n\n        Returns:\n            A callable that can be used to generate text.\n        \"\"\"\n        # Fetches the generate.py file from the model repo. If it doesn't exist, a file in `.no_exist` cache directory\n        # is created (preventing future hub requests), and an OSError is raised.\n        try:\n            module = get_cached_module_file(\n                pretrained_model_name_or_path, module_file=\"custom_generate/generate.py\", **kwargs\n            )\n        except OSError:\n            raise OSError(\n                f\"`{pretrained_model_name_or_path}` does not contain a `custom_generate` subdirectory with a \"\n                \"`generate.py` file, can't load the custom generate function.\"\n            )\n\n        # Loading a custom generate function executes the repository's `custom_generate/generate.py`\n        # (via `get_class_in_module` below), so it is remote code and must be gated by\n        # `trust_remote_code` -- including when it is loaded from a local directory. `from_pretrained`\n        # likewise requires `trust_remote_code` for custom modeling code that lives in a local repo;\n        # treating a local path as trusted here previously let `custom_generate/generate.py` run with\n        # no opt-in.\n        error_message = (\n            f\"The repository `{pretrained_model_name_or_path}` contains custom generation code that will override \"\n            \"the default `generate` method.\"\n        )\n        resolve_trust_remote_code(\n            trust_remote_code,\n            pretrained_model_name_or_path,\n            has_local_code=False,","sourceCodeStart":471,"sourceCodeEnd":507,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/utils.py#L471-L507","documentation":"OSError raised while loading a custom generate function: the hub/local repo at pretrained_model_name_or_path must contain custom_generate/generate.py, and get_cached_module_file failed to fetch it (a .no_exist cache entry is written to avoid re-requesting). The error is re-raised with a wrapper message, so the underlying network/404 cause is swallowed into this generic text.","triggerScenarios":"Calling load_custom_generate(model_id) (or a generate() path that resolves custom generation code) on a repo that lacks the custom_generate/ subdirectory; typo in the repo id; offline/hub outage causing the file fetch to fail; the file exists only on a different revision/branch.","commonSituations":"Using a community repo that never shipped custom_generate/; pointing at a fine-tune saved locally without copying the custom_generate folder; HF_HUB_OFFLINE=1 with a cold cache.","solutions":["Check the repo on the Hub for custom_generate/generate.py; if missing, the model has no custom generate function — don't request it.","If it should exist, verify the exact repo id/revision and re-fetch (delete the .no_exist cache entry in the local hub cache for that file).","For local dirs, ensure <dir>/custom_generate/generate.py exists and pass trust_remote_code=True.","Retry with network access if the initial fetch failed offline."],"exampleFix":"# before\nfn = load_custom_generate(\"user/model-x\", trust_remote_code=True)  # repo has no custom_generate/\n\n# after\n# upload custom_generate/generate.py to the repo first, then:\nfn = load_custom_generate(\"user/model-x\", trust_remote_code=True)","handlingStrategy":"try-catch","validationCode":"from huggingface_hub import file_exists\n\nif not file_exists(repo_id, \"custom_generate/generate.py\", revision=revision):\n    raise FileNotFoundError(f\"{repo_id} has no custom_generate/generate.py\")","typeGuard":null,"tryCatchPattern":"try:\n    fn = load_custom_generate(repo_id, trust_remote_code=True)\nexcept OSError as e:\n    if \"custom_generate\" in str(e):\n        logger.info(\"no custom generate code on %s; using default generate()\", repo_id)\n        fn = None\n    else:\n        raise","preventionTips":["Check the repo layout on the Hub before requesting custom generate code.","Remember a failed lookup writes a .no_exist cache entry — clear the hub cache after fixing the repo.","Ship custom_generate/generate.py in your own repos if you advertise custom generation."],"tags":["hub","custom-code","generate","remote-code","offline"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}