Sub topic 7
Recording Metadata and Change Notes
This chapter assumes you can write Python, read YAML, use Git, and already store prompt templates as files, as covered in earlier chapters. Here you will attach metadata to each prompt version so that anyone can tell what changed, why, and what the template expects.
Why a version number is not enough
A version string such as 1.2.0 tells you that something changed. It does not tell you what changed, why, or what inputs the template needs. Without that information, a reviewer must compare two files by eye, and a developer who rolls back has to guess which behavior they are giving up.
Metadata fixes this. Each prompt version file carries a small, structured header next to the template text. Because the header lives in the same file, the two cannot drift apart.
What to record
A useful prompt version records a handful of fields:
| Field | Purpose | Example |
|---|---|---|
| `name` | Identifies the prompt | `summarize` |
| `version` | Identifies this revision | `1.2.0` |
| `changelog` | Says what changed and why | `Ask for a one-sentence answer when the text is short.` |
| `variables` | Lists the inputs the template expects | `[style, text]` |
| `template` | The prompt text itself | A block of text with `$style` and `$text` |
Keep the changelog to one or two sentences. Describe the intended effect on behavior, not just the edit. "Ask for a one-sentence answer when the text is short" is more useful than "Edited line 2".
A versioned prompt file
This file follows the layout prompts/<name>/<version>.yaml.
name: summarize
version: 1.2.0
changelog: Ask for a one-sentence answer when the text is short.
variables: [style, text]
template: |
Summarize the following text in $style style.
If the text is under 50 words, answer in one sentence.
$textThe variables list is the contract. It states which values a caller must supply. The earlier chapter on rendering showed a check against a REQUIRED set. With metadata in the file, that set no longer needs to be hard-coded in Python. You can read it from the prompt itself.
Using the metadata in code
The function below loads a prompt file and validates the supplied values against the declared variables. It reuses the file layout and Template class from earlier chapters.
import yaml
from pathlib import Path
from string import Template
def load_prompt(name, version):
path = Path("prompts") / name / f"{version}.yaml"
return yaml.safe_load(path.read_text())
def render(prompt, values):
declared = set(prompt["variables"])
missing = declared - values.keys()
extra = values.keys() - declared
if missing or extra:
raise ValueError(
f"{prompt['name']} {prompt['version']}: "
f"missing={sorted(missing)} extra={sorted(extra)}"
)
return Template(prompt["template"]).substitute(values)
prompt = load_prompt("summarize", "1.2.0")
print(render(prompt, {"style": "concise", "text": "Cats sleep a lot."}))Notice that the error message includes the prompt name and version. When a failure occurs in a pipeline, the message points to the exact file.
Checking that metadata matches the template
Metadata can go stale. A developer adds $audience to the template but forgets to add it to variables. Rendering then fails at runtime, or worse, the validation passes on an incomplete list. You can catch this with a small check that runs in tests or in CI.
import re
def check_variables(prompt):
found = set(re.findall(r"\$(\w+)", prompt["template"]))
declared = set(prompt["variables"])
if found != declared:
raise ValueError(
f"declared={sorted(declared)} used={sorted(found)}"
)This simple pattern does not handle the escaped form $$, so treat it as a starting point. Run it over every prompt file before release.
Writing good change notes
Change notes are read by reviewers during review and by whoever investigates a bad output later. A few habits make them useful:
- State the behavior change, not the edit. Write "Answers in one sentence for short text", not "Added a line".
- Give the reason when it is not obvious, for example "Users complained about long replies to short inputs".
- Note a changed variable list. Adding or removing a variable breaks callers, and the note should say so.
- Never edit a published version file. Create a new version and describe the difference.
How metadata fits the workflow
Metadata is written when a change is made, read during review, and consulted again when you pin or roll back a version.
Common errors
- Stale
variableslist. The template and the list disagree. Use the check above. - Vague changelog. Entries like "improved prompt" give reviewers nothing to evaluate.
- Edited in place. Changing a published file while keeping its version number makes logs and test results unreliable.
- Missing version in error messages. Without it, you cannot tell which file failed.
Exercise
A teammate adds an $audience placeholder to the summarize template at version 1.2.0 and edits the file in place, without changing variables or version. Name two problems this causes and say how you would fix them.
Show answer
First, the variables list no longer matches the template, so validation will not require audience, and rendering fails with a missing-value error or the check_variables test fails. Second, editing a published file in place means logs and tests that reference 1.2.0 now describe different text. Fix it by restoring 1.2.0 to its original content, creating a new file such as 2.0.0 (a major bump, because the variable list changed) that lists audience in variables, and writing a changelog that explains the new behavior.