huggingface/transformers · error · ValueError

You need to install `libcst` to run this command -> `pip ins

Error message

You need to install `libcst` to run this command -> `pip install libcst`

What it means

The add-new-model-like command uses libcst to parse and rewrite the source files of the base model into a new modular file. The import of libcst is optional at module level, so the command explicitly checks is_libcst_available() and raises ValueError (not ImportError) telling you to pip install libcst when it is absent.

Source

Thrown at src/transformers/cli/add_new_model_like.py:555

    """
    Creates a new model module like a given model of the Transformers library.

    Args:
        repo_path (`Path`):
            The path to the root of the Transformers repository.
        old_model_infos (`ModelInfos`):
            The structure containing the class information of the old model.
        new_lowercase_name (`str`):
            The new lowercase model name.
        new_model_paper_name (`str`):
            The fully cased name (as in the official paper name) of the new model.
        filenames_to_add (`list[tuple[str, bool]]`):
            A list of tuples of all potential filenames to add for a new model, along a boolean flag describing if we
            should add this file or not. For example, [(`modeling_xxx.px`, True), (`configuration_xxx.py`, True), (`tokenization_xxx.py`, False),...]
    """
    # As the import was protected, raise if not present (as it's actually a hard dependency for this command)
    if not is_libcst_available():
        raise ValueError("You need to install `libcst` to run this command -> `pip install libcst`")

    old_lowercase_name = old_model_infos.lowercase_name

    # 1. We create the folder for our new model
    new_module_folder = repo_path / "src" / "transformers" / "models" / new_lowercase_name
    os.makedirs(new_module_folder, exist_ok=True)

    # 2. Create and add the modular file
    modular_file, public_classes = create_modular_file(
        repo_path, old_model_infos, new_lowercase_name, filenames_to_add
    )
    with open(new_module_folder / f"modular_{new_lowercase_name}.py", "w") as f:
        f.write(modular_file)

    # 3. Create and add the __init__.py
    init_file = create_init_file(old_lowercase_name, new_lowercase_name, filenames_to_add)
    with open(new_module_folder / "__init__.py", "w") as f:
        f.write(init_file)

View on GitHub (pinned to a597f97485)

Solutions

  1. pip install libcst
  2. Or install transformers dev extras: pip install -e "transformers[dev]" from the repo
  3. Re-run the add-new-model-like command afterwards

Example fix

# before
transformers add-new-model-like ...  # ValueError: install libcst

# after
pip install libcst
transformers add-new-model-like ...
Defensive patterns

Strategy: validation

Validate before calling

try:
    import libcst  # noqa
    ok = True
except ImportError:
    ok = False
if not ok:
    raise SystemExit("pip install libcst before running add-new-model-like")

Prevention

When it happens

Trigger: Running `transformers add-new-model-like ...` in an environment where libcst is not installed; a minimal/pinned install of transformers that skips optional dev dependencies; CI containers that only install runtime requirements.

Common situations: Fresh contributor environments; tox/nox environments that install only `pip install -e .` without dev extras; caching/lock files that omit libcst.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/bef4c742ddac732e. Report an issue: GitHub.