Sub topic 9
Drafting READMEs and usage guides
A README is the first document a new user reads. It tells them what the project does, how to install it, and how to run something useful within a few minutes. This chapter covers how to get an assistant to draft one, and how to keep it from inventing details.
This chapter assumes you have read the earlier chapters on prompt context and docstring generation. You should be comfortable with Python packaging basics, running commands in a terminal, and reviewing a diff in Git.
Why READMEs go wrong when drafted by an assistant
Docstrings describe one function and sit next to the code they describe. A README describes the whole project, and much of what it says is not in any single file. Installation steps, supported versions, configuration, and the project's purpose are spread across pyproject.toml, CI files, and the team's heads.
An assistant fills those gaps with plausible text. If you ask for "a README for this project" and give it only one source file, you will usually get these problems:
- An install command such as
pip install pricingfor a package that is not published. - A Python version requirement the project never declared.
- A "Configuration" section describing environment variables that do not exist.
- Badges, license lines, and contributing sections copied from typical projects.
- Usage examples that call functions with the wrong arguments or show the wrong output.
None of these look wrong on a quick read. They only fail when a new user follows them. The fix is to control what the assistant knows and what it is allowed to say.
Separate facts from prose
Treat README drafting as two jobs. You collect facts from the project, and the assistant turns those facts into readable prose. The assistant is good at the second job and unreliable at the first.
| README section | Source of truth | Risk if the assistant guesses |
|---|---|---|
| One-line description | Your intent, docstrings | Overstated or vague claims |
| Installation | `pyproject.toml`, release process | Wrong package name or command |
| Requirements | `requires-python`, dependencies | Invented version floors |
| Quick start | Public function signatures, a real run | Wrong arguments, wrong output |
| Error behavior | `raise` statements, docstrings | Missing or invented exceptions |
| Configuration | Code that reads env vars or files | Options that do not exist |
| License, contributing | `LICENSE`, team policy | Wrong license |
Give the assistant the material in the second column. For anything you cannot supply, tell it to leave a marker instead of filling in a value.
Collect the public API automatically
You can feed the assistant the real signatures and docstrings with a short script. This is more reliable than pasting whole files, because it removes private helpers and keeps only what users can call. It assumes the pricing.py module with the documented apply_discount from earlier chapters.
import inspect
import pricing
def public_api_summary(module) -> str:
"""Return the signature and docstring of each public function in module."""
entries = []
for name, obj in inspect.getmembers(module, inspect.isfunction):
if name.startswith("_") or obj.__module__ != module.__name__:
continue
doc = inspect.getdoc(obj) or "(no docstring)"
entries.append(f"{name}{inspect.signature(obj)}\n{doc}")
return "\n\n".join(entries)
if __name__ == "__main__":
print(public_api_summary(pricing))The check on obj.__module__ skips functions the module merely imported. The output is a block of text you paste into the prompt as the "facts" section. If a function has no docstring, the summary says so. That is a signal to generate and review the docstring first, since the README depends on it.
Write a prompt that forbids invention
A good README prompt names the audience, the sections you want, the facts available, and what to do when a fact is missing.
Draft a README.md for the Python module described below.
Audience: developers who want to call the function from their own code.
Sections, in this order:
1. Title and one-sentence description
2. Requirements
3. Installation
4. Quick start
5. Error handling
Rules:
- Use only facts from the "Facts" block. Do not invent package names,
version numbers, options, or commands.
- If a section needs a fact that is not in the block, write
TODO: <what is missing> instead of guessing.
- Every code example must use the exact signatures in the block.
- Show expected output only for values you can compute from the docstring.
- Keep it under 60 lines. No badges.
Facts:
- Module: pricing.py, copied into the project (not published on PyPI).
- Python: 3.10 or newer.
- Public API and docstrings:
<output of public_api_summary here>Three parts of this prompt matter most.
- The TODO rule. It turns silent guesses into visible gaps. After the draft, you can search for
TODOand fill each one from a real source. - The signature rule. It ties examples to the actual code, which makes them easier to check.
- The length limit. Assistants pad READMEs with generic sections. A limit keeps the draft focused on facts you supplied.
What a good draft looks like
For this module, a reviewed README might look like the following. It is shown here as a plain excerpt, so the commands are indented rather than fenced.
# pricing
Apply a percentage discount to a price and get a rounded result.
## Requirements
- Python 3.10 or newer
## Installation
pricing is not published on PyPI. Copy pricing.py into your project.
## Quick start
from pricing import apply_discount
print(apply_discount(80, 25)) # 60.0
## Error handling
apply_discount raises ValueError if price is negative or if percent is
outside 0-100 inclusive.Notice what the draft does not contain. It has no pip install line, because the facts said the module is not published. It shows one example with an output you can compute by hand: 25% off 80 is 60. It states the error conditions exactly as the docstring does.
The workflow
The draft is the middle of the process, not the end. Facts go in, a draft comes out, and then you check the draft against the project.
The loop from the check back to the facts step matters. If an example fails, the usual cause is a missing or wrong fact in the prompt, not a one-off typo. Fix the input, then regenerate.
Make the quick start executable
A quick-start example is the most valuable part of a README and the part most likely to rot. Put it in a file that you can run, then paste the same code into the README.
from pricing import apply_discount
print(apply_discount(80, 25))
try:
apply_discount(10, 101)
except ValueError as error:
print(error)Running this prints two lines:
60.0
percent must be between 0 and 100Compare each printed line with what the README claims. If the assistant wrote a different output, or described a different error message, you have found an inaccuracy. The next chapter turns this manual check into something repeatable.
Usage guides beyond the README
A README should stay short. For longer material such as a task-based guide ("How to apply a promotional discount to a cart"), ask for a separate document and give the assistant a scenario.
- State the task and the reader's starting point.
- Supply the functions the guide may use, using the same facts block.
- Ask for numbered steps, with one action per step and the expected result after each.
- Ask for a "Troubleshooting" list built only from the error paths in the code. Each entry names an exception, its cause, and the fix.
Guides built this way stay tied to behavior you can verify. Guides built from a vague request tend to describe features you do not have.
Common failure modes
- Confident installation steps. The assistant adds
pip installfor a package you never published. Counter this by stating the install method in the facts. - Stale after a refactor. The README is correct on the day you generate it, then the signature changes. Regenerate the README, or at least rerun its examples, in the same change that edits the code.
- Outputs that look right. The assistant writes
# 60where Python prints60.0. Check printed values character by character. - Feature inflation. The one-line description promises more than the code does. Read the opening sentence against the actual public functions.
- Style drift. Each regeneration produces slightly different headings and tone. Fix the section list in the prompt and keep the prompt in the repository so the structure stays stable.
Review checklist for a drafted README
Before you commit the draft, go through these points:
- Every
TODOis resolved from a real source, or removed with the section. - Every command in the README is one you have run.
- Every code example runs unchanged, and its printed output matches the text.
- Version and dependency claims match
pyproject.tomlor the equivalent file. - Error handling matches the
raisestatements in the code. - Nothing in the text describes a feature you cannot point to in the code.
Exercise
You ask an assistant to draft a README for a module and you supply only the source file. The draft includes "pip install pricing-tools" and a "Configuration" section listing PRICING_ROUNDING. How do you find out whether these are real, and how should you change the prompt so the next draft is safer?
Show answer
Check the sources of truth. Look in the project metadata for the actual package name and whether it is published, and search the code for any read of PRICING_ROUNDING, such as os.environ or os.getenv. If neither exists, remove both items. Then change the prompt to include a Facts block with the install method, the supported Python version, and the public signatures and docstrings. Add a rule to use only those facts and to write TODO markers for anything missing, so gaps appear as visible markers and not as invented details.
Exercise
The README's quick start says apply_discount(19.99, 0) prints 19.99. How would you confirm the claim, and what does this tell you about which examples to choose for a README?
Show answer
Run the example in a Python session with the real module and compare the printed value to the text. Examples whose results you can derive by hand from the docstring, such as apply_discount(80, 25) giving 60.0, are the safest to publish, because both you and a later reader can verify them without trusting the assistant. For other values, run them and paste the real output rather than accepting the assistant's version.