docstring

Official PyTorch guidance for precise, consistent, and verifiable docstrings.

Docstring is an official skill in the PyTorch repository. It helps developers and technical writers create or revise function and method documentation in the style used by the PyTorch project. According to the provider, the skill follows conventions found in central PyTorch files such as torch/_tensor_docs.py and torch/nn/functional.py. Its purpose is not to generate arbitrary product prose. It is intended to structure technical docstrings so that signatures, behavior, parameters, return values, examples, and references remain understandable to readers and usable by the project's documentation tooling.

Purpose and structure

The guide starts with a signature on the first line. That line should show the function name, positional arguments, keyword arguments, defaults, and return type, and it should not end with a period. A short behavior description follows. Depending on the function, the docstring can add mathematical formulas with the project's Sphinx notation, cross-references to classes, functions, methods, or attributes, and notes or warnings. The source therefore separates quick orientation from the details required for a dependable API reference.

Parameters and results

A central part of the skill is systematic argument documentation. Parameter names should be lowercase, types belong in parentheses, and optional values should state their default. Keyword arguments may receive a separate section when that improves readability. Return values deserve an explanation when their shape, type, or semantics are not already clear from the signature. These conventions expose assumptions that would otherwise remain implicit, including tensor shape conventions, behavior for particular data types, or the effect of a flag.

Examples and cross-references

According to the provider, examples should be included whenever they are useful. A strong example demonstrates a realistic call and can show an important variation or expected output. Cross-references connect the text to related PyTorch classes and functions. The result is a navigable reference rather than an isolated paragraph. For mathematical operations, a formula can make the relationship between the implementation and its documented behavior more precise. The skill distinguishes native Python functions from C-bound functions: for the latter pattern, documentation is attached to the bound object through the repository's designated add-documentation function.

Quality assurance and boundaries

Docstring is a writing and review guide, not an automatic proof that an implementation is correct. Before publication, authors must check that the signature, code, tests, and observed runtime behavior agree. Optional arguments, shapes and device types, edge cases, warnings, and references to renamed or removed APIs deserve particular attention. The skill points to Sphinx and reStructuredText, while the exact PyTorch version, documentation build, and local project rules remain authoritative. A model can produce plausible but incorrect technical statements, so examples should be executed or checked against tests whenever practical. Confidential source code should be processed only in an explicitly authorized workspace.

Source and classification

The primary source is https://github.com/pytorch/pytorch/tree/main/.claude/skills/docstring. The complementary official PyTorch documentation at https://docs.pytorch.org/docs/stable/notes/doc.html describes the project's documentation conventions. PyTorch is released under BSD-3-Clause. This description was reviewed on September 9, 2026. GitHub stars are not stored because the Skill model has no github_stars field. The skill is marked as compatible with Claude Code because it is published under .claude/skills. It does not replace expert review, tests, or the documentation build for the PyTorch source state being changed.

Installation instructions

What the docstring skill is and why it matters

The skill named docstring lives directly inside the official PyTorch repository at .claude/skills/docstring/SKILL.md and is automatically picked up by Anthropic Claude Code whenever someone works inside the PyTorch source tree. A Claude Code skill is essentially a markdown file with focused instructions that Claude loads for a specific kind of task, so the same conventions do not need to be re-explained in every request. In this case the skill focuses on writing and updating docstrings, the in-code documentation blocks attached to functions and methods that help other PyTorch developers understand an API without reading the implementation. According to the source files torch/_tensor_docs.py and torch/nn/functional.py referenced in the skill, it follows the internal style PyTorch has maintained for years, which keeps generated text consistent with the rest of the documented codebase. It is worth noting for readers unfamiliar with the ecosystem that PyTorch is one of the most widely used machine learning frameworks, and consistent docstrings there directly affect both code readability and the automatically generated documentation website.

Step 1: Prerequisites

Using the skill requires a working Claude Code installation and access to the PyTorch source, either through a local clone of the pytorch/pytorch repository or through a separate project that wants to adopt the same documentation conventions. Because the skill file is plain text with no executable dependencies, no additional packages, API keys, or permissions are needed. Anyone who wants to reuse the conventions in a project outside PyTorch itself should have a basic understanding of Sphinx and reStructuredText, since that markup format is the foundation of every rule in the skill.

Setting it up step by step

Anyone working directly inside the official PyTorch repository does not need to do anything extra: Claude Code automatically reads skills from the .claude/skills directory and activates the matching one whenever a task involves writing or editing a docstring. To reuse the conventions in a separate project, such as a PyTorch extension or a closely related library, copy the SKILL.md file from the PyTorch repository into your own .claude/skills/docstring folder and adjust the name and description in the header if needed so Claude can match it correctly. Claude Code then picks up the skill on its next start; restarting the tool is usually enough.

Security and best practices

Because the skill only defines formatting and style rules for text documentation and never executes code, makes network calls, or touches credentials, the risk profile of using it is very low. Even so, it is worth manually reviewing generated docstrings, especially where mathematical formulas or cross-references to other classes and functions are involved, because broken Sphinx directives typically only surface once the documentation is actually built, resulting in broken links or misrendered formulas. Anyone adapting the skill for a separate project should also make sure the adopted conventions genuinely fit that project's own documentation tooling, since copying them without adjustment can produce inconsistent results if the target project does not use Sphinx.

Practical example and limitations

A typical example is documenting a new convolution function: the skill guides Claude to start with the full function signature including parameters and return type, follow it with a short description, then list parameters under an Args section with type annotations and default values, and reference related classes such as torch.nn.Conv2d where useful. The clear limitation of the skill is that it was built specifically for PyTorch's reStructuredText style and cannot be transferred unchanged to other common docstring formats such as the Google style or the NumPy style. Anyone working outside the PyTorch ecosystem should therefore treat the rules as a template that needs adaptation to local conventions rather than something to adopt verbatim.

Free
Provider
PyTorch
License
BSD-3-Clause
Last reviewed
09.09.2026

Repository and documentation

Categories

Compatible with

Claude Code