Sub topic 6
Why and how to version prompts
This chapter assumes you can send prompts to a language model, write basic Python, use Git, and read YAML. You have already seen how to write templates and render them. Now you will treat those templates as artifacts that change over time.
Why prompts need versions
A prompt is part of your program's behavior. Changing one sentence can change the format, length, or accuracy of every output. Unlike a code change, the effect is hard to predict by reading the diff.
Without versions, three problems appear quickly:
- You cannot say which prompt produced a bad output from last Tuesday.
- You cannot return to the earlier wording when a change makes things worse.
- Two people edit the same prompt, and nobody knows which copy is live.
A version gives each prompt text a stable identifier. With that identifier you can compare, test, release, and roll back.
What counts as a change
Treat a new version as needed whenever any of these change:
- The wording of the template.
- The set of variables it expects.
- The output format it asks for, such as plain text versus JSON.
A changed variable list matters most. Code that renders the template must change in the same release, or rendering will fail.
Choosing a versioning scheme
Two schemes are common. The table compares them.
| Scheme | Example | Strength | Weakness |
|---|---|---|---|
| Sequential number | `v7` | Simple, always ordered | Says nothing about the size of the change |
| Semantic version | `1.2.0` | Signals the kind of change | Needs agreement on the rules |
For prompts, a workable reading of semantic versions is:
- Major (
2.0.0): the variables or the output format changed. Calling code must change. - Minor (
1.3.0): the wording changed to alter behavior, with the same variables and format. - Patch (
1.2.1): a typo or whitespace fix that should not change behavior.
Whichever scheme you pick, write the rules down and apply them consistently. A scheme nobody follows is worse than a simple counter.
A versioned template file
Store each version as its own file. Here is version 1.2.0 of a summarizing prompt:
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 file holds the name, version, change note, and declared variables next to the template text. The next chapter covers the metadata in detail. Here the point is that the version lives inside the file and the file lives in Git.
Laying out files
Use one directory per prompt and one file per version:
prompts/
summarize/
1.1.0.yaml
1.2.0.yamlDo not edit a released file. If 1.2.0 is in use and you find a problem, create 1.2.1. Old files stay in place so that anyone can load them later. Git history also records changes, but separate files let the running application load an old version by name without checking out an old commit.
Loading a specific version
This function reads a version by name, and a mapping decides which version the application uses:
import yaml
from pathlib import Path
def load_prompt(name, version):
path = Path("prompts") / name / f"{version}.yaml"
return yaml.safe_load(path.read_text())
PINNED = {"summarize": "1.2.0"}
prompt = load_prompt("summarize", PINNED["summarize"])PINNED is the only place that says which version is live. To move forward or back, you change one line. Later chapters build on this idea.
How a change moves through versions
The key rule is that step B creates a new file instead of overwriting the old one.
Common mistakes
- Editing in place. Overwriting
1.2.0breaks the link between the version number and the behavior. Logs that say1.2.0now point to different text. - Skipping the change note. In a month, "tweaked prompt" tells you nothing. Say what changed and why.
- Changing variables in a patch release. This breaks callers that rely on the old variable list.
- Bundling many edits. If one version changes five things and quality drops, you cannot tell which edit caused it. Prefer small changes.
Exercise
Exercise
Your summarize prompt at version 1.2.0 asks for a plain-text answer. You change it to return JSON with the keys "summary" and "length". Which part of the version number should increase, what is the new version, and why?
Show answer
Increase the major number, giving 2.0.0. The output format changed from plain text to JSON, so any code that reads the result must change too. Save it as a new file, prompts/summarize/2.0.0.yaml, and leave 1.2.0 unchanged so you can roll back.