Sub topic 8
Pinning Versions, Aliases, and Rollback
Prerequisites
This chapter assumes you have read the earlier chapters on storing templates as files and on versioning schemes. You should be comfortable with these ideas:
- A template lives in a file such as
prompts/summarize/1.2.0.yaml. - Each file declares its own
version,changelog, andvariables. - Python, YAML, and basic Git commands are familiar to you.
Versioned files only help if the application uses the right one. This chapter covers how the application chooses a version, how a team can move between versions without redeploying code, and how to undo a bad release quickly.
What pinning means
Pinning means the application names the exact prompt version it will use. It does not take whatever file is newest.
Without pinning, the behavior of your application depends on what happens to be in a directory. Someone adds 1.3.0.yaml and the app silently changes behavior in production. Nobody reviewed that change as a release, and nothing in the application's code history records it.
With pinning, the choice of version is data that you review, commit, and deploy like any other change.
The simplest form is a mapping in code or configuration:
import yaml
from pathlib import Path
PROMPTS_DIR = Path("prompts")
def load_prompt(name, version):
path = PROMPTS_DIR / name / f"{version}.yaml"
if not path.exists():
raise FileNotFoundError(f"No prompt '{name}' version '{version}' at {path}")
return yaml.safe_load(path.read_text())
PINNED = {"summarize": "1.2.0"}
prompt = load_prompt("summarize", PINNED["summarize"])Two details are worth noting.
- The loader raises an error when the file is missing. It does not fall back to another version. A fallback would hide a typo in the pin, and you would not know which prompt was running.
- Version strings are always strings. YAML and JSON parsers turn an unquoted
1.2into the float1.2, and1.10into1.1. Quote short versions in configuration ("1.10"), or use three-part versions such as1.10.0, which parsers always read as strings.
Rules for version files
Pinning only works if a version never changes after release. Follow these rules:
- Never edit a released file. A fix becomes a new version, for example
1.2.1. - Never delete a released file while any pin, alias, or log entry still refers to it.
- Review any change to a pin as carefully as a change to the template itself.
Aliases
An exact pin is safe but rigid. Every prompt change requires a code change and often a deployment. An alias is a movable name that points to an exact version. Common names are production, staging, and candidate.
The aliases live in one small file:
# prompts/aliases.yaml
summarize:
production: "1.2.0"
staging: "1.3.0"The application asks for summarize at production. A resolver turns that into an exact version, and the loader reads that file.
import os
import yaml
from pathlib import Path
PROMPTS_DIR = Path("prompts")
def read_aliases():
return yaml.safe_load((PROMPTS_DIR / "aliases.yaml").read_text())
def resolve_version(name, ref=None):
"""Turn an alias or an exact version into an exact version string."""
ref = ref or os.environ.get("PROMPT_ENV", "production")
aliases = read_aliases().get(name)
if aliases is None:
raise KeyError(f"No aliases defined for prompt '{name}'")
return str(aliases.get(ref, ref))
def load_prompt(name, ref=None):
version = resolve_version(name, ref)
path = PROMPTS_DIR / name / f"{version}.yaml"
if not path.exists():
raise FileNotFoundError(f"Alias or version '{ref}' resolved to {version}, but {path} does not exist")
return yaml.safe_load(path.read_text())If ref is an alias, it is replaced by its version. If it is not an alias, it is treated as an exact version. That lets tests and debugging sessions request 1.1.0 directly.
Because of this fallthrough, never name an alias like a version number. An alias called 1.2.0 that pointed to 1.1.0 would be very confusing.
How the resolution flows
Environments use different aliases
The same code runs everywhere. Only the alias differs.
| Environment | Alias requested | Resolves to | Purpose |
|---|---|---|---|
| Local development | `staging` | `1.3.0` | Try the new prompt on real code paths |
| Staging deployment | `staging` | `1.3.0` | Final checks before release |
| Production | `production` | `1.2.0` | Reviewed and tested prompt |
| Incident debugging | `1.1.0` (exact) | `1.1.0` | Reproduce an old output |
Promoting a prompt is then a one-line change to aliases.yaml. Setting production to 1.3.0 is the release.
Choosing between exact pins and aliases
Both approaches are valid. They trade control against speed.
| Approach | Strength | Weakness |
|---|---|---|
| Exact pin in code | Prompt and code ship together, easy to reason about | Every prompt change needs a code release |
| Alias file in the repository | Prompt releases are one reviewed line, with Git history | Still deployed with the app unless the file is read at runtime |
| Alias in an external store | Change takes effect without a deployment | Changes can bypass review, and the store becomes a dependency |
An alias file kept in Git and deployed with the application is a good default. It gives you review, history, and quick promotion without a new moving part. Move the alias to an external store only when you need to change production behavior faster than you can deploy. If you do, add an audit log and access control for who can change an alias, because that change is a production release.
Rollback
Rollback means returning production to the last known good prompt. With immutable versions and aliases, it is a small change.
Suppose 1.3.0 went to production and users report that summaries have become too long. The previous good version was 1.2.0, which is still in the repository.
summarize:
- production: "1.3.0"
+ production: "1.2.0"
staging: "1.3.0"You can make this change by hand. Or you can revert the release commit with git revert <commit>, which restores the old alias line and records why in history. Do not use git reset --hard or force push on a shared branch for this. The revert keeps an honest record of what happened.
Rollback does not change 1.3.0. The file stays in place, so the team can study the failure, write a test case for it, and ship 1.3.1.
What can make rollback fail
Rollback sounds easy, but several things can break it.
- Running processes keep the old value. If the application reads
aliases.yamlonce at startup and caches it, nothing changes until it restarts. Decide how the alias is refreshed. Options are reading on a short timer, reading on each request, or restarting during release. Test that your rollback takes effect within the time you expect. - Variables changed between versions. Suppose
1.3.0added a variableaudience, and the code was changed to always supply it. The old1.2.0declares onlystyleandtext. If your validation rejects extra values, rollback now fails with a validation error. Validate against the variables declared by the loaded version, not a constant in code, and make code pass only what that version declares. When a prompt change requires a code change, release them in an order that keeps both versions working. For example, make the code tolerate both variable sets first, and promote the new prompt afterwards. - The model changed too. A prompt version tested on one model may behave differently on another. If the same release also switched models, rolling back the prompt alone may not restore the old behavior. Record the model with the prompt version and roll back both together.
- Outputs already produced. Rolling back does not fix summaries that were already generated and stored. Plan for that separately, for example by regenerating items that were produced by the bad version. The log entries from the later chapter on observability tell you which items those are.
- The old file is gone. If someone deleted
1.2.0.yamlas clutter, you cannot roll back to it. Git history can restore it, but that costs time during an incident.
Checking pins automatically
A pin that points to a nonexistent file fails only when the code path runs. Catch that earlier with a check in your test suite or CI that confirms every alias points to a real file, and that the file's own version field matches its name.
import yaml
from pathlib import Path
PROMPTS_DIR = Path("prompts")
def check_aliases():
problems = []
aliases = yaml.safe_load((PROMPTS_DIR / "aliases.yaml").read_text())
for name, refs in aliases.items():
for alias, version in refs.items():
path = PROMPTS_DIR / name / f"{version}.yaml"
if not path.exists():
problems.append(f"{name}:{alias} -> {version} (file missing)")
continue
data = yaml.safe_load(path.read_text())
if str(data.get("version")) != str(version):
problems.append(f"{name}:{alias} -> {path} declares version {data.get('version')}")
return problems
if __name__ == "__main__":
found = check_aliases()
for line in found:
print(line)
raise SystemExit(1 if found else 0)Run this on every pull request that touches prompts/. It takes a fraction of a second and prevents the most common release mistake, which is a pointer to a version that does not exist.
Common mistakes
- Using "latest" as an alias. It moves whenever someone adds a file, so it gives none of the stability pinning is for.
- Using unquoted versions such as
1.10in YAML, which are read as numbers. - Editing a released version in place "just to fix a typo". Every log entry that names that version now describes a prompt that no longer exists.
- Keeping the alias and the version history in different places with no review on the alias.
- Not testing rollback. Practice it in staging, so the first time you do it is not during an incident.
Exercise
Exercise
Your aliases.yaml sets production to "1.3.0". After release, users report worse summaries. You change production back to "1.2.0" and merge, but the service still produces the bad output ten minutes later. Name two possible causes and the check you would make for each.
Show answer
First, the running service may have cached the alias file at startup, so it never reads the new value. Check how and when the application reads aliases.yaml, and whether it restarted or refreshed after the merge. Second, the deployment may not have picked up the change, or code may have been changed to pass variables that 1.2.0 does not declare, causing errors or a different path. Check which version the service logged for recent calls, and compare the variables the code supplies with the ones declared in 1.2.0.yaml. The logged prompt version shows quickly whether the rollback took effect.