Sub topic 3
Redact Secrets and Keep Recordings Reviewable
Assume you can write pytest tests, pass dependencies into functions, and handle JSON replies from an LLM API. This chapter focuses on test data that is safe to store and easy to inspect.
Redact secrets before saving
A recording, sometimes called a cassette, stores a request and response so tests can replay them without making a network call. Recordings may contain API keys, authorization headers, customer names, invoice text, or personal data.
Remove secrets before writing the file. Prefer filtering at the HTTP-recording library level, then inspect the result manually.
import re
SECRET = re.compile(r"(?i)(api[_-]?key|authorization|token)=?[^\s,]+")
def redact(text: str) -> str:
return SECRET.sub(r"\1=[REDACTED]", text)
request = "authorization=Bearer abc123 invoice=public-demo"
print(redact(request))Keep recordings small. Use representative prompts, stable fixture data, and only the response fields your code reads. Review recordings like source code. They should be deterministic, readable, and safe to commit.
Fake streaming replies and tool calls
A streaming fake can yield chunks in order. A tool-call fake can return a scripted tool name and arguments. These fakes let you test assembly, validation, and dispatch without depending on timing or model decisions.
class StreamFake:
def __init__(self, chunks):
self.chunks = list(chunks)
def stream(self, prompt):
yield from self.chunks
def collect(client, prompt):
return "".join(client.stream(prompt))
assert collect(StreamFake(["Hel", "lo"]), "greet") == "Hello"Choose the right test double
Use a hand-written fake for fast unit tests and precise failures. Use a recording when SDK serialization, authentication wiring, response shape, or provider-specific behavior matters.
| Technique | Best use | Main cost |
|---|---|---|
| Fake | Your parsing, retry, and dispatch logic | It may hide SDK changes |
| Recording | Integration behavior and response shapes | It can become stale |
Handle drift deliberately
Drift means the real API changes while your recording remains unchanged. Re-record after an intentional SDK, prompt, schema, or model change. Review the diff and update assertions rather than blindly accepting it. A changed prompt should usually produce a new recording key.
Live smoke tests are not evals
A small live smoke test checks that credentials, connectivity, and basic API compatibility work. Run it rarely and keep it separate from ordinary tests.
An evaluation measures answer quality against labeled cases or a rubric. A smoke test only proves that one live request completed. It cannot establish consistent quality.
Exercise
Which technique should test retry behavior after malformed JSON?
Show answer
Use a scripted fake that returns malformed JSON first and valid JSON second.