Skip to main content

agentsnap-py

PyPI Python License: MIT

Snapshot tests for AI agents. Record an agent's tool-call trace, diff it against a baseline, fail CI on regressions. Zero runtime dependencies. Drops into pytest or any test runner.

Python port of @mukundakatta/agentsnap.

Install

pip install agentsnap-py

Usage

from agentsnap import record, trace_tool, expect_snapshot

search = trace_tool("search", lambda q: fetch_results(q))
summarize = trace_tool("summarize", lambda docs: llm_summarize(docs))

def agent(question):
    docs = search(question)
    return summarize(docs)

def test_research_agent_stays_on_rails():
    trace = record(lambda: agent("What is RLHF?"))
    expect_snapshot(trace, "tests/__snapshots__/research.snap.json")

First run writes the snapshot. Every run after that diffs against it. If the agent calls a different tool, calls them in a different order, or starts erroring, the test fails with a readable diff. Regenerate with AGENTSNAP_UPDATE=1.

Async agents

import asyncio
from agentsnap import arecord, trace_tool, expect_snapshot

asearch = trace_tool("search", async_fetch)

async def agent(q):
    return await asearch(q)

def test_async_agent():
    trace = asyncio.run(arecord(lambda: agent("hello")))
    expect_snapshot(trace, "tests/__snapshots__/async.snap.json")

Diff statuses

Status When Default action
PASSED Bytewise match green
OUTPUT_DRIFT Tools + args identical, only output text or external result hashes differ warn (non-failing)
TOOLS_REORDERED Same tool names, different order fail
TOOLS_CHANGED Different tool names called, or different args fail
REGRESSION New error in the trace, or a tool that used to work now throws fail

Override per snapshot via expect_snapshot(trace, path, fail_on=[...]).

API

record(fn, *, input=None, model=None, capture_results=False) -> Trace

Run fn (sync) and capture every trace_tool-wrapped call inside it. Returns a JSON-serializable dict.

arecord(fn, ...) -> Trace

Async variant for async def agents. Use with asyncio.run(arecord(lambda: agent())) or inside an async test.

trace_tool(name, fn) -> wrapped_fn

Wraps a tool. Inside record, calls go into the trace; outside, transparent pass-through. Works with sync and async tools (returns the same shape).

expect_snapshot(trace, path, *, update=False, fail_on=None) -> dict

Compare against an on-disk JSON baseline. Writes if missing, regenerates if AGENTSNAP_UPDATE=1 (or update=True), otherwise diffs and raises AgentSnapshotMismatch on a failing status.

diff(baseline, current) -> DiffResult

Low-level diff engine. Returns a DiffResult(status=..., changes=[Change(...)]).

format_diff(result, path=None) -> str

Render a colored terminal block for the diff (used in the failure message).

pytest plugin

Installing this package registers a pytest plugin that exposes the same API as fixtures:

def test_my_agent(agentsnap_record, trace_tool, expect_snapshot):
    fn = trace_tool("hello", lambda: "world")
    trace = agentsnap_record(lambda: fn())
    expect_snapshot(trace, "tests/__snapshots__/hello.snap.json")

API differences from the JS sibling

  • Tracing uses contextvars (Python's AsyncLocalStorage equivalent) instead of node:async_hooks.
  • Sync agents use record(); async agents use arecord() -- Python doesn't have JS's "async by default" assumption.
  • Trace is a dict (not a class) so it serializes / inspects naturally.
  • Change.to_dict() produces the JS-style {"path": ..., "from": ..., "to": ...} -- the dataclass uses from_ because from is a Python keyword.
  • Adds a pytest plugin (pyproject.toml pytest11 entry point).

See the JS sibling's README for the full design notes.

Metadata

Release files for agentsnap-py 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agentsnap-py 0.1.0
File Size Uploaded Provenance
agentsnap_py-0.1.0.tar.gz 11.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for agentsnap-py 0.1.0
File Interpreter ABI Platform
agentsnap_py-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.5 kB

Release files / agentsnap_py-0.1.0.tar.gz

Download URL agentsnap_py-0.1.0.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b5addd94b47aa81e5f2d6b500d3b15eb429a083597b1f9d101902370d2d0f5f8
BLAKE2b-256 checksum
How to use checksums
1a1508f789727648accce377ef2da663871f33865235944f0e511edddac91599
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / agentsnap_py-0.1.0-py3-none-any.whl

Download URL agentsnap_py-0.1.0-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ba3620c75240485f13b25288058a992f73f3edfea7d8b304b515db0724f35735
BLAKE2b-256 checksum
How to use checksums
f57d18a4ac514f5293473a8e2267ca705c0ad8be94f7b83180ab2dcd13bd8918
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for agentsnap-py 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page