Prompt template · Git and docs
Docstring and code comment prompt
Docstrings in your project’s style covering parameters, returns, errors and the non-obvious why. 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 prompt
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.
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.
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><context>
<code>
{{code}}
</code>
</context>
<task>
Add a docstring to every public function and class in this code, in {{style}} style.
</task>Variables
What to put in each variable
- {{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 to use it
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 it’s 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
A filled-in example
- {{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)
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
Tips by model family
- 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.
Related
Related templates and tools
- Write a READMEA project README from your code and notes: what it is, install, quick start, configuration and limits.
- Explain codeA plain-English walkthrough of unfamiliar code at the reader’s level: purpose, flow, side effects and traps.
- Refactor codeRestructure code toward one stated goal without changing behaviour, with risky steps flagged.
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.