Skip to content
AI Dev Toolkit.
Esc
  • Loading…

Prompt template · Git and docs

Docstring and code comment prompt

A good docstring prompt names your convention, such as Google, NumPy, reStructuredText or JSDoc, and asks for what a caller needs: behaviour, parameters, return value, errors and side effects. This template explains why rather than how, forbids any code change, and adds a short NOTE wherever the code’s behaviour looks unclear.

Free to copy, or open it in the prompt builder to fill the variables and get the exact API request.

187 tokens unfilled · 227 with the example below · o200k_base (GPT-4o and later)

Template

The docstring prompt template (copy it)

Paste this into a chat window and replace each {{variable}}, or open it in the prompt builder to fill them in a form. Sections are marked with XML tags, which every major model reads well.

Prompt (one message, plain text)
You write concise, accurate API documentation for {{language}} code.

<context>
<code>
{{code}}
</code>
</context>

<constraints>
- Document behaviour a caller needs: what it does, parameters, return value, errors raised, side effects.
- Explain why when the code does something non-obvious. Don’t restate what each line does.
- Don’t change any code. Only add or replace docstrings and comments.
- If the behaviour for an input is unclear from the code, write what the code actually does and flag it with a short NOTE.
- Keep each docstring as short as it can be while staying complete.
</constraints>

<output_format>
The full code with docstrings added, in one code block. Nothing before or after it.
</output_format>

<task>
Add a docstring to every public function and class in this code, in {{style}} style.
</task>
For an API call: system prompt and user message

With a system prompt, the role, constraints and output format stay fixed across calls (and can be cached), while the context and task go in the user message.

System prompt
You write concise, accurate API documentation for {{language}} code.

<constraints>
- Document behaviour a caller needs: what it does, parameters, return value, errors raised, side effects.
- Explain why when the code does something non-obvious. Don’t restate what each line does.
- Don’t change any code. Only add or replace docstrings and comments.
- If the behaviour for an input is unclear from the code, write what the code actually does and flag it with a short NOTE.
- Keep each docstring as short as it can be while staying complete.
</constraints>

<output_format>
The full code with docstrings added, in one code block. Nothing before or after it.
</output_format>
User message
<context>
<code>
{{code}}
</code>
</context>

<task>
Add a docstring to every public function and class in this code, in {{style}} style.
</task>

Variables

What do the variables mean?

{{language}}
The language, such as Python or TypeScript.
{{code}}
The module or functions to document.
{{style}}
The docstring convention: Google, NumPy or reStructuredText for Python; JSDoc or TSDoc for TypeScript; Javadoc for Java.

Use

When should I use this docstring prompt?

Use it to document a module before publishing it, to bring an undocumented internal library up to standard, or to generate first drafts that you then trim. It works well because the code is the source of truth and the task is mostly faithful description.

It won’t know your intent where the code is ambiguous. That’s what the NOTE flags are for: each one is a place where the behaviour may be a bug, and worth a look.

Structure

Why is the prompt structured this way?

The style variable gives the model a precise target. Google, NumPy and JSDoc styles each have exact section names, and a named convention produces consistent output that tools such as Sphinx or TypeDoc can render.

“Why, not how” is the rule that separates useful documentation from noise. A comment saying “loop over the items” adds nothing; one saying “sorted first because the API returns pages out of order” saves the next developer an afternoon.

Forbidding code changes makes the output safe to apply as a whole: you can diff it and see only documentation. The NOTE rule stops the model from documenting what the function was probably meant to do, which would quietly paper over bugs.

Example

Example: a filled-in docstring prompt

{{language}}
Python
{{code}}
def chunk(items, size):
    if size < 1:
        raise ValueError("size must be at least 1")
    return [items[i:i + size] for i in range(0, len(items), size)]
{{style}}
Google

What to look for. A good docstring states that the last chunk may be shorter, documents the ValueError, and mentions that it works on any sliceable sequence, so strings come back as lists of substrings. That last point is the kind of non-obvious behaviour worth a NOTE.

The complete filled-in prompt (227 tokens)
Filled-in prompt
You write concise, accurate API documentation for Python code.

<context>
<code>
def chunk(items, size):
    if size < 1:
        raise ValueError("size must be at least 1")
    return [items[i:i + size] for i in range(0, len(items), size)]
</code>
</context>

<constraints>
- Document behaviour a caller needs: what it does, parameters, return value, errors raised, side effects.
- Explain why when the code does something non-obvious. Don’t restate what each line does.
- Don’t change any code. Only add or replace docstrings and comments.
- If the behaviour for an input is unclear from the code, write what the code actually does and flag it with a short NOTE.
- Keep each docstring as short as it can be while staying complete.
</constraints>

<output_format>
The full code with docstrings added, in one code block. Nothing before or after it.
</output_format>

<task>
Add a docstring to every public function and class in this code, in Google style.
</task>

Models

Does it work the same in ChatGPT, Claude and Gemini?

Yes, the structure works in all three: tagged sections that keep your material apart from the instructions, and an explicit output format. The tips below cover where the providers’ own guides differ for this docstring prompt.

  • All models

    Run it file by file and diff the result. Because the template forbids code changes, any non-comment change in the diff means the model ignored the rule, and you can reject it at a glance.

  • Claude

    Anthropic’s guide suggests saying what you want rather than what you don’t. “Nothing before or after it” in the format line is the positive version of “no preamble”, and it replaces the old prefill trick, which Claude 4.6 models and later don’t support.

  • OpenAI GPT

    Non-reasoning GPT models benefit from precise instructions, OpenAI notes. If yours adds inline comments to every line, repeat the “why, not how” rule with a short example of a comment you’d keep.

FAQ

Questions people ask

What is the difference between Google and NumPy docstrings?

Layout. Google style uses short labelled sections such as Args:, Returns: and Raises: with indented entries. NumPy style uses section titles underlined with dashes, such as Parameters and Returns, and suits long scientific docstrings. Sphinx can render both with its Napoleon extension; pick the one your project already uses and name it in the style variable.

What is PEP 257?

The Python Enhancement Proposal that sets basic docstring conventions: triple double quotes, a one-line summary ending in a full stop, a blank line before any longer description, and docstrings for public modules, functions, classes and methods. It doesn’t define sections such as Args, which is why Google and NumPy styles build on top of it.

How do I document a whole file at once?

Paste the file and let the template add a docstring to every public function and class. Because it forbids code changes, you can diff the result and see only documentation; any other change means the model ignored the rule. Run it file by file rather than across a package, so each diff stays small enough to review.

Related

Also useful with this prompt: Technical writer system prompt.

Edited the template? Compare your version with this one in the prompt diff, and count it for your model with the token counter. Browse all 29 prompt templates.

Keep exploring