Sub topic 12
Handle drift and re-record deliberately
This chapter assumes you can write pytest tests, you know the RecordingClient from the previous chapters (a replay-first client that saves replies keyed by a prompt hash), and you pass the client into your code as an argument.
What drift is
A recording is a snapshot. It shows what the model or API returned on the day you captured it. Drift is the gap that opens between that snapshot and what the real service does now.
Replay tests stay green while drift grows, because they never contact the service. This is the main weakness of recordings. The tests are fast and stable, but they can't tell you the real API has changed.
Drift usually comes from four places:
- The provider retires or updates a model, so replies change in style, length, or format.
- The response shape changes, for example a renamed field or a new content block type.
- Your own prompt changes, so the old recording no longer matches.
- Your parsing assumptions were wrong from the start, and the one recorded reply happened to satisfy them.
Prompt changes fail loudly
Because RecordingClient keys each reply by a hash of the prompt, editing the prompt template changes the key. The replay then raises LookupError instead of returning a stale reply. This is deliberate. A failure here means "the recording no longer describes what the code sends," and the fix is to re-record.
Service-side drift is different. The prompt is unchanged, so the key still matches and replay succeeds. You only catch this kind with a live check.
Re-record on purpose
Re-recording should be an explicit action. It should never happen as a side effect of a test run. A test run that quietly calls the live API can cost money, leak data, and overwrite a good recording with a bad one.
A simple pattern is a small helper that builds the client from an environment variable:
import os
from recording import RecordingClient # the class from the previous chapter
def make_client(path, real_client_factory):
"""Replay by default. Call the real API only when RERECORD=1."""
if os.environ.get("RERECORD") == "1":
return RecordingClient(path, real_client=real_client_factory())
return RecordingClient(path)Replay is the default. Someone has to type RERECORD=1 pytest tests/test_invoices.py to refresh a recording. Note that this client only calls the real API for prompts it has not saved. To refresh a prompt whose key still matches, delete its recording file first.
A re-record checklist
- Delete the recording you want to refresh, or the specific entries in it.
- Run the tests once with
RERECORD=1and real credentials. - Run the tests again without the variable, to confirm they pass offline.
- Read the diff of the recording file. Check that no secrets appear.
- Commit the code change and the recording together, with a message that says why you re-recorded.
Because RecordingClient writes JSON with sorted keys and indentation, the diff is line-oriented and reviewable.
Catch service drift with a live check
Re-recording only helps if you know you need it. For that, keep a few live tests behind a marker and run them on a schedule, not on every commit.
import os
import pytest
live = pytest.mark.skipif(
os.environ.get("RUN_LIVE") != "1",
reason="live test: set RUN_LIVE=1 to run",
)
@live
def test_live_reply_still_parses():
client = build_real_client() # your project's real SDK wrapper
total = extract_total(client, "Invoice\nTotal: $42.50")
assert total == pytest.approx(42.5)The assertion checks the shape your code depends on, here that the reply parses as JSON with a numeric total. It does not check exact wording, because model output varies between runs. If this test starts failing while replay tests pass, the service has drifted. Re-record, then fix any code that relied on the old behavior.
Exercise
Your replay tests all pass, but production starts failing to parse replies after the provider updates its model. You did not change any prompts. Why didn't the replay tests catch it, and what would?
Show answer
The prompt hashes still match, so replay keeps returning the old recorded replies and never contacts the new model. A scheduled live smoke test that checks the reply's shape would fail and reveal the drift. You would then re-record deliberately, review the diff, and update the parsing code.