docs/README.md
The Transformers docs live under docs/source/<lang>/ and are built with doc-builder.
[!TIP] You usually don't need to build the docs locally. When you open a PR that touches files under
docs/, a bot builds a preview and posts the link as a comment on the PR.
Build locally for a faster iteration loop, or while drafting changes before you open a PR.
Install the quality dependencies and doc-builder from the root of the repository. The [quality] extra is enough for docs-only work. Use [dev] if your change also needs the full development dependency set.
pip install -e ".[quality]"
pip install git+https://github.com/huggingface/doc-builder
To build the Markdown files into a temporary folder you can inspect in any Markdown editor:
doc-builder build transformers docs/source/en/ --build_dir ~/tmp/test-build
To live-preview the docs in a browser at http://localhost:5173, install watchdog and run preview:
pip install watchdog
doc-builder preview transformers docs/source/en/
[!WARNING]
previewonly picks up files that exist when it starts. After you add a brand-new page, update_toctree.ymland restartpreview.
Don't commit the built output. Only changes under docs/source/ are reviewed.
Pages live in docs/source/<lang>/ as Markdown (.md) files. The sidebar navigation lives in _toctree.yml, and a new page only shows up in the sidebar after you add it there.
docs/source/en/ (or the appropriate language directory). Match the file naming and license header of an existing page. Copying a similar page is the fastest way to start.docs/source/en/_toctree.yml that points to the filename without the .md extension.Each entry has two fields:
local: the file path relative to docs/source/<lang>/, without the extension.title: the human-readable label that appears in the sidebar.For a page nested inside a subsection, add it to the inner sections list:
- isExpanded: false
sections:
- local: contributing
title: Contribute to Transformers
- local: my_new_contributor_guide
title: My new contributor guide
title: Contribute
doc-builder accepts standard Markdown plus a few extensions you'll see across our docs. For the full set of supported syntax (inference snippets, file-include blocks, redirects, multilingual builds, etc.), see the doc-builder README.
Use GitHub-style blockquote callouts for notes, tips, and warnings:
> [!TIP]
> Use `device_map="auto"` to let Transformers place model shards across available devices.
> [!WARNING]
> `from_pretrained` downloads the full checkpoint on first use. Set `cache_dir` to control where it lands.
Older pages may use the legacy <Tip> component. Prefer blockquotes for new content.
Wrap a class, function, or method name in square brackets and backticks to link to its doc page. doc-builder resolves the link automatically:
Use [`AutoModel`] to load a model from a checkpoint, then call [`~PreTrainedModel.from_pretrained`].
A few variations:
~ to render only the last component (from_pretrained) instead of the full path (PreTrainedModel.from_pretrained).utils.ModelOutput.accelerate.Accelerator.To show alternative snippets (CLI vs. Python, different backends, etc.) as tabs, use <hfoptions>:
<hfoptions id="install">
<hfoption id="pip">
```bash
pip install transformers
```
</hfoption>
<hfoption id="uv">
```bash
uv pip install transformers
```
</hfoption>
</hfoptions>
[!IMPORTANT] Always leave a blank line after
[[autodoc]]so the CI checks pass.
Use [[autodoc]] to render the docstring of a class or function. The marker pulls the description, arguments, and (for classes) every public method:
## AutoModel
[[autodoc]] AutoModel
To restrict the output to specific methods, list them as a bulleted sub-list:
[[autodoc]] BertTokenizer
- build_inputs_with_special_tokens
- get_special_tokens_mask
To pull in a method that isn't documented by default (for example, __call__), start the list with all and then add the extras:
[[autodoc]] BertTokenizer
- all
- __call__
Tag Python fences with runnable (optionally followed by :<label>) to mark them as testable examples. doc-builder strips the annotation from the rendered output:
```py runnable:quickstart
from transformers import pipeline
pipe = pipeline("sentiment-analysis")
print(pipe("I love this!"))
```
Most docstrings are automatically generated in the library source code, especially modeling_*.py, configuration_*.py, processing, and tokenizer files. For model classes and forward methods, use the @auto_docstring decorator when it applies. It keeps shared arguments and returns consistent docstrings without repeating full Args: blocks in every model file.
Use hand-written docstrings, following the Google Python Style Guide, for objects that are not covered by @auto_docstring, or when a method needs model-specific behavior documented.
Run make style to format docstrings and code examples with Ruff.
The script can fail on syntax errors. Commit before running make style so you can revert if something goes wrong.
Don't commit images, videos, or other binary assets to the repository because they bloat the repository. Host them on the Hub instead and reference them by URL. The standard home for documentation media is the huggingface/documentation-images dataset. For external PRs, attach the images to the PR and ask a Hugging Face maintainer to migrate them to the dataset.