Servirtium for
Python
No shipped package: vendor the binding's source at a release tag, with the native library from the release. Release v2.0.0-alpha.20.
- FFI mechanism
- ctypes. A pure-Python package that loads the native library at run time through the standard-library ctypes module. No compiler and no runtime dependencies.
- Toolchain
- Python 3.9+ (setup.py python_requires)
- Distribution
- No PyPI package (the PyPI name 'servirtium' is the old 1.x): pip install the python/ subdirectory of the tag, plus the release native library
- Finding the library
- SERVIRTIUM_VCR_LIB (absolute path to the library), or native_lib='/abs/path' on servirtium.playback(...) / record(...). Set it in whatever process runs the tests.
- On GitHub
- python/ · README
Getting it
There is no published package for Python. You vendor the binding's source into your project at the release tag v2.0.0-alpha.20 and take the native library from the same release.
- Fetch the native library for your OS/arch from the release into a directory outside your git tree and verify the checksum.
REL=https://github.com/servirtium/servirtium-vcr/releases/download/v2.0.0-alpha.20 LIBDIR="$HOME/.servirtium-vcr/v2.0.0-alpha.20"; mkdir -p "$LIBDIR" LIB=libservirtium_vcr-v2.0.0-alpha.20-macos-arm64.dylib # or -linux-x86_64.so, -linux-arm64.so, ... curl -fsSL -o "$LIBDIR/$LIB" $REL/$LIB curl -fsSL -o "$LIBDIR/$LIB.sha256" $REL/$LIB.sha256 ( cd "$LIBDIR" && { sha256sum -c $LIB.sha256 || shasum -a 256 -c $LIB.sha256; } ) - Install the package from the tag (the python/ subdirectory of the GitHub archive). Do not run pip install servirtium: that is the unrelated 1.x package.
python3 -m venv .venv && . .venv/bin/activate python -m pip install "https://github.com/servirtium/servirtium-vcr/archive/refs/tags/v2.0.0-alpha.20.tar.gz#subdirectory=python" - Point the package at the library wherever your tests run, and check servirtium.__version__ is 2.0.0a20.
export SERVIRTIUM_VCR_LIB="$HOME/.servirtium-vcr/v2.0.0-alpha.20/libservirtium_vcr-v2.0.0-alpha.20-macos-arm64.dylib" python -c "import servirtium; print(servirtium.__version__)" - As a requirements.txt / pyproject dependency line.
servirtium @ https://github.com/servirtium/servirtium-vcr/archive/refs/tags/v2.0.0-alpha.20.tar.gz#subdirectory=python
Record
Point your client at the VCR's base URL; the VCR forwards to the real service and hands back the real response. Nothing is written until you commit.
import urllib.request
import servirtium
with servirtium.record("tapes/climate_api.md",
"https://climatedataapi.worldbank.org").port(0).start() as vcr:
urllib.request.urlopen(f"{vcr.base_url}/api/v1/countries", timeout=10).read()
vcr.commit() # the only thing that writes the tape
# Leaving the with block WITHOUT commit() writes nothing and raises VcrError.
# A with block that raises discards the recording and re-raises your exception.
Playback
The same tape, replayed with no network.
import urllib.request
import servirtium
with servirtium.playback("tapes/climate_api.md").port(0).start() as vcr: # 0 = OS-assigned
body = urllib.request.urlopen(f"{vcr.base_url}/api/v1/countries").read()
assert vcr.last_kind is servirtium.Outcome.OK, vcr.last_error # optional: assert a clean match
Idiomatic extras
A failing with-block discards the recording
If your code raises inside the with block, the recording is dropped, no tape is written, and your exception propagates.
try:
with servirtium.record("tapes/x.md", upstream).port(0).start() as vcr:
urllib.request.urlopen(vcr.base_url + "/x").read()
raise RuntimeError("test failed") # -> no tapes/x.md
except RuntimeError:
pass
Redact credentials and JSON fields at record time
Applied when the tape is written; the live test still sees real bytes.
with servirtium.record(tape, upstream) \
.redact_credentials() \
.redact_json_field("api_key", "[REDACTED]", field=servirtium.Field.RESPONSE_BODY) \
.port(0).start() as vcr:
... # drive the SUT
vcr.commit()
Drift detection
With fail_if_changed, commit() writes the fresh tape and raises VcrError if it differs from the one on disk.
with servirtium.record(tape, upstream).fail_if_changed().port(0).start() as vcr:
... # drive the SUT
vcr.commit() # raises VcrError on drift
Gotchas
- The version in the install URL and the library must be the same tag; a library older than the package fails at the first call naming a missing aether_vcr_embed_ symbol.
- OSError 'could not load native VCR library' means SERVIRTIUM_VCR_LIB is unset in the process running the tests (an IDE or CI step does not inherit your shell) or is not an absolute path; on Alpine take the -musl library.
- Do not copy the library into site-packages/servirtium/native by hand (lost on reinstall); do not commit it.
- Windows: take the .dll asset by hand and give SERVIRTIUM_VCR_LIB its full path.
Prompt for your coding agent
Paste this into Claude, Codex, Gemini or any coding agent, in your Python project. It wraps one of your existing integration tests with Servirtium. The general prompt has a slot for the language instead.
Hey Gemini or Claude or Codex,
I want a test in this Python project to run without calling a real web service every time. Use Servirtium (https://servirtium.dev), which records HTTP conversations as readable Markdown "tapes" and replays them. Do these steps in order and tell me what you did at each one.
1. Find an existing integration test in this project that calls a web service as a side effect of what it is testing. Prefer one that is slow, flaky, or needs credentials or network because of that call. Tell me which test you chose and why.
2. Add the Servirtium binding for Python. Follow https://servirtium.dev/languages/python/ exactly. Servirtium ships no package for this language. Vendor the binding's source into the project at the release tag v2.0.0-alpha.20 and put the native library from the same release where the page says: No PyPI package (the PyPI name 'servirtium' is the old 1.x): pip install the python/ subdirectory of the tag, plus the release native library.
Toolchain and minimum version: Python 3.9+ (setup.py python_requires).
How the binding finds the native library: SERVIRTIUM_VCR_LIB (absolute path to the library), or native_lib='/abs/path' on servirtium.playback(...) / record(...). Set it in whatever process runs the tests.
3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic Python way of setting a base URL or HTTP client. The VCR starts on its own port (use port 0, so it picks a free one) and gives you its base URL; give that URL to the client and never hard-code a port.
4. Run the test once in record mode against the real service. Commit the recording when the test passes and discard it when the test fails: a record VCR that is closed without a commit or a discard writes no tape and raises an error. Use redact_credentials (the spelling for Python is on the page) so secrets become numbered tokens, and remove volatile request headers such as User-Agent and Date from the tape so playback does not depend on them.
5. Keep the tape beside the test as readable Markdown, in version control. Open it and check there are no secrets or personal data in it before committing.
6. Make the test play back by default, with an explicit switch (for example an environment variable such as SERVIRTIUM_RECORD=1) that re-records.
7. Prove it works offline: make the real service unreachable (network off, or a wrong host) and show the test still passes in playback.
Traps to avoid:
- A record VCR needs Commit() or Discard(); closing it without either writes nothing and raises.
- The VCR runs in your test process on its own port; the system under test must use the VCR's base URL, not the real one.
- The version in the install URL and the library must be the same tag; a library older than the package fails at the first call naming a missing aether_vcr_embed_ symbol.
- OSError 'could not load native VCR library' means SERVIRTIUM_VCR_LIB is unset in the process running the tests (an IDE or CI step does not inherit your shell) or is not an absolute path; on Alpine take the -musl library.
- Do not copy the library into site-packages/servirtium/native by hand (lost on reinstall); do not commit it.
- Windows: take the .dll asset by hand and give SERVIRTIUM_VCR_LIB its full path.
References:
- This language's page: https://servirtium.dev/languages/python/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/python
- Concepts, features and every language's quick start: https://servirtium.dev/llms-full.txt (index: https://servirtium.dev/llms.txt)
How these snippets were checked
Real run on macOS arm64 (system Python 3.9.6, the stated floor): pip install of the python/ subdirectory from the v2.0.0-alpha.20 GitHub archive into a fresh venv (servirtium.__version__ == 2.0.0a20), release macos-arm64 dylib via SERVIRTIUM_VCR_LIB (sha256 verified; the unmodified dylib loads fine under ctypes). Ran: the README 'Getting it' playback check (ok-body, Outcome.OK); the README 'Record and replay a local service' script with vcr.commit() added (records hello.md, replays it with the upstream stopped); the same script WITHOUT commit() (raises VcrError, no hello.md); a with-block that raises (no tape); redact_credentials + redact_json_field (tape has neither the secret nor the bearer token, has [REDACTED]). fail_if_changed extra checked against _vcr.py, not run.
They mirror these files in the repository:
python/test/test_playback.pypython/test/test_record.pypython/test/test_secrets.pypython/test/test_record_mutation.pypython/example/consumer_example.py
See also the feature reference, llms.txt and the python/ directory.