Coding
AI Prompt for Writing Docstrings
Generate professional docstrings with this AI prompt: Google, NumPy or JSDoc style, typed params, edge cases, raises sections, and runnable examples.
The prompt
Role: You are a documentation perfectionist who writes docstrings that make APIs a joy to use. Context: Language: [LANGUAGE]. Convention: [CONVENTION: Google / NumPy / JSDoc / Javadoc / etc.]. Code: [CODE: function/class/module] Task: Write the complete docstring including: 1) One-line summary + longer description of what it does and when to use it. 2) Every parameter: type, meaning, constraints, default behavior. 3) Return value: type and meaning. 4) Exceptions/errors raised and when. 5) Edge-case behavior. 6) A runnable usage example. 7) Any "see also" references to related functions. Format: The docstring in a code block, ready to paste above the code, followed by a note on anything you inferred (vs explicitly in the code). Ask the AI to mark every inferred type and edge case with a confidence note, so you can verify the guesses that matter. Docstrings built on wrong inferences mislead every future reader. Constraints: Follow [CONVENTION] exactly — this feeds doc generators. Infer types from the code but LABEL inferences so I can correct them. The example must actually run.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[LANGUAGE] [CONVENTION] [CODE]
Example input
LANGUAGE=Python, STYLE=Google docstrings, CODE=def train_model(data, epochs=10, lr=0.001) with early stopping, INCLUDE=Args, Returns, Raises, Example
How to use this prompt
Name your docstring convention (Google, NumPy, JSDoc, Javadoc) — format matters for doc generators. List the sections you want; Examples are the most valuable.
Tips for the best results
- Always verify inferred types and edge cases — the AI guesses from context.
- Ask for the example to use realistic values from your domain.
- Generate docstrings for public APIs first; internal helpers need less.
Frequently asked questions
Does it infer types and edge cases?
It writes complete docstrings from the signature and body, inferring types and edge cases — you verify the inferences.
Do private functions need docstrings too?
A one-liner suffices for privates; save the full treatment for public APIs. The prompt scales detail to the function's visibility.