Document Code (Docstrings and README)

Write clear docstrings and a README section from your code, without inventing behavior.

documentationdocstringsREADME All models

Prompt

Prompt
Document the {language} code below.

<code>
{code}
</code>

<audience>
{audience}
</audience>

<format>
Docstring style: {docstring_style}
README section: {readme_scope}
</format>

Produce two things.

Part 1. The code with docstrings added to every public function, class and module. Each docstring states what it does, its parameters with types, what it returns, and what errors it can raise. Do not change the code itself.

Part 2. A README section covering: {readme_scope}. Include a short usage example that runs with the code as written.

Rules:
- Describe only what the code actually does. Do not invent features, parameters, defaults or configuration that are not in the code.
- Use plain language suitable for the audience. Avoid restating the function name as the description.
- If behavior is unclear from the code, or if names are misleading, list those places under "Needs your input" instead of guessing.
- Keep each docstring short. A one-line summary first, then details only where useful.

End with a list of anything in the documentation you could not confirm from the code alone.

Variables

VariableWhat to put inExample
{language}The programming language.Python
{code}The code to document.Paste the module or function here.
{audience}Who will read the documentation.New developers joining the team.
{docstring_style}The docstring format your project uses.Google style
{readme_scope}What the README section should cover.Purpose, installation, usage example, configuration.

How to use

  1. Include the whole module so the documentation can describe how parts relate.
  2. Read the "Needs your input" list and answer it, since only you know the intent behind unclear code.
  3. Run the README example yourself to make sure it works before you commit it.

Example result

Illustrative example

def total(items, discount=0.0): """Return the order total after applying a discount. Args: items: A list of (price, quantity) pairs. discount: A fraction between 0 and 1. Raises: ValueError: If a quantity is negative. """ Needs your input: should discount above 1 raise an error or be capped?

Model notes

Claude

Claude tends to write accurate docstrings when told not to invent behavior. Keep the "needs your input" list.

ChatGPT

Works as-is. Ask for the README in Markdown so you can paste it straight into your repository.

Gemini

If you can attach the repository files, name them so the README refers to real file names and commands.

Grok

Keep documentation tied to the code you provide. Ask it to leave out claims about performance or compatibility that you did not state.

Refine the result

  • Prompt
    Shorten each docstring to one line plus the arguments.
  • Prompt
    Add a troubleshooting section based on the errors the code can raise.
  • Prompt
    Rewrite the README introduction for a non-technical reader.

Related templates

  • Refactor for Readability

    Improve the structure and naming of code without changing what it does, with a list of changes.

    refactoringreadabilityclean code All models
  • Code Review

    Get a prioritized code review that covers correctness, security, readability and tests.

    code reviewqualitysecurity All models
  • Write Unit Tests

    Generate unit tests that cover normal cases, edge cases and failures, with the reasoning for each.

    testingunit testsquality All models

Related guide