Code Documentation Prompt: Docstrings From Pasted Code
Paste a Python module and get Google-style docstrings that describe only what the code does, doctest examples you can run, and a TODO list of every unknown.
Prompt
Act as a Python maintainer who writes docstrings for code the team inherited and does not fully trust. You read the pasted code line by line, document only the behavior that is visible in the code or confirmed by the tests and notes you are given, and write TODO beside anything that is unclear instead of guessing the author's intent. Your docstrings are short, in the requested style, and every example in them is one you can derive from the code by hand. Inputs: - Language, version and docstring style: [Language and Style] - The code to document, pasted as is: [Code to Document] - Behavior the team already knows or tests that pass: [Known Behavior] - Who calls this code and what they need to know: [Callers] - Edge cases to check by reading the code: [Edge Cases] - Conventions to follow, such as doctest or type hints: [Conventions] Generate: 1. A behavior inventory from [Code to Document]: for each function, inputs, return value, side effects and every raised error, each with the line that shows it. 2. Docstrings for every function in [Language and Style], using only the inventory and [Known Behavior]. 3. Examples in the form [Conventions] asks for, each worked out by hand from the code, with the result written next to it. 4. An edge case table for [Edge Cases]: input, what the code does, and whether the docstring now says so. 5. A TODO list of unknowns the code cannot answer, with the question for the author or a test to write. 6. A short note for [Callers] on what changed in how they should read the docstrings, and nothing about code you did not see.
Instructions
Paste the whole module so the inventory sees every branch, and give the prompt your real tests and known bugs. Run the doctest examples with python -m doctest to prove each one, and delete any that fail instead of editing the expected output to match. Variation: for a class, ask for a class docstring plus one line per public method before the full method docstrings. Variation: for JavaScript, change the style input to JSDoc and keep the same TODO rule. Tip: treat every TODO as a ticket; those are the questions the code could not answer. Never paste secrets, keys or customer data into the Code to Document input. Not affiliated with Python, Google or any documentation host.
Generated Output
This image was generated using the prompt above.

Examples
Example Input
Language and Style: Python 3.12, Google-style docstrings.
Code to Document:
def parse_duration(text):
parts = text.strip().lower().split()
total = 0
for part in parts:
if part.endswith("h"):
total += int(part[:-1]) * 3600
elif part.endswith("m"):
total += int(part[:-1]) * 60
elif part.endswith("s"):
total += int(part[:-1])
else:
raise ValueError(f"unknown unit in {part!r}")
return total
def chunk_log_lines(lines, size=100, keep_blank=False):
batch = []
for line in lines:
if not keep_blank and not line.strip():
continue
batch.append(line.rstrip("\n"))
if len(batch) == size:
yield batch
batch = []
if batch:
yield batch
Known Behavior: a test passes for "1h 30m" returning 5400, for "" returning 0 and for "90" raising ValueError. Nobody has tested chunk_log_lines with size 0.
Callers: an on-call script that turns "2h 15m" style text from alert messages into seconds, and a log shipper that reads a file in batches.
Edge Cases: negative values, decimals, repeated units, upper case, blank lines.
Conventions: doctest examples for anything deterministic, no type hints added.Example Output
1. BEHAVIOR INVENTORY (parse_duration)
Input: one string. Splits on whitespace after strip and lower, so "1H" works. Each part must end in h, m or s: hours x 3600, minutes x 60, seconds x 1. Returns the sum as an int. Raises ValueError("unknown unit in ...") for a part ending in another character. The number part goes through int(), so "1.5h" or "xm" raises ValueError from int(). Empty string returns 0. Negative parts are allowed ("-5m" gives -300). The same unit can repeat and adds up.
def parse_duration(text):
"""Convert text like "1h 30m 10s" into a total number of seconds.
Parts are separated by whitespace and are case-insensitive. Each part is
a whole number followed by h (hours), m (minutes) or s (seconds). Parts
are added together, and a unit may appear more than once.
Args:
text: The duration text. Leading and trailing spaces are ignored.
Returns:
The total as an int number of seconds. An empty string returns 0.
Raises:
ValueError: If a part does not end in h, m or s, or its number is
not a whole number (for example "1.5h").
Examples:
>>> parse_duration("1h 30m")
5400
>>> parse_duration("2h 15m 10s")
8110
>>> parse_duration("")
0
TODO: negative values such as "-5m" return -300. Decide whether that
is intended; no test covers it.
"""
2. BEHAVIOR INVENTORY (chunk_log_lines)
It is a generator: it yields lists of lines. Lines that are empty or only whitespace are skipped unless keep_blank is True. Each kept line has a trailing newline removed with rstrip("\n") and other spaces left alone. A list is yielded when it reaches size, and a shorter final list is yielded if lines remain. Empty input yields nothing.
def chunk_log_lines(lines, size=100, keep_blank=False):
"""Yield lists of up to size log lines from an iterable of lines.
Blank lines (empty or whitespace only) are skipped unless keep_blank is
True. A trailing newline is removed from each line; other whitespace is
kept.
Args:
lines: An iterable of strings, such as an open file.
size: The number of lines in each full batch.
keep_blank: If True, blank lines are kept in the batches.
Yields:
Lists of strings. The last list may be shorter than size.
Examples:
>>> list(chunk_log_lines(["a", "", "b", "c"], size=2))
[['a', 'b'], ['c']]
TODO: size is not validated. By reading the code, a size of 0 or less
never matches len(batch), so everything comes back in one list. Confirm
this is intended and add a test.
"""
3. EDGE CASE TABLE
negative values | accepted, returns negative seconds | docstring TODO
decimals | ValueError from int() | stated in Raises
repeated units | added together | stated
upper case | works, lowercased first | stated
blank lines | skipped unless keep_blank | stated
4. TODO LIST
- Negative durations: intended? Add a test either way.
- size of 0 or less: validate or document; add a test.
- Whitespace-only lines kept by keep_blank=True are kept with their spaces; confirm that is what the log shipper wants.
5. NOTE FOR CALLERS
The on-call script gets ValueError for "90" and "1.5h" only; "-5m" is accepted and not an error. The log shipper should not rely on size values of 0 or less.