Agentic Property-Based Testing: Finding Bugs Across the Python Ecosystem

Muhammad Maaz, Liam DeVoe, Zac Hatfield-Dodds, Nicholas Carlini

TL;DR

The authors introduce an autonomous LLM-powered coding agent that crawls Python codebases, infers behavioral invariants, and generates property-based tests (PBTs) using the Hypothesis framework. Tested across 100 popular Python packages, the agent generated 984 bug reports, with manual validation confirming a 56% true bug rate (reaching 86% among top-ranked reports). Multiple findings led to upstream acknowledgments and merged patches in major open-source libraries, including NumPy and AWS tooling.

What Problem Does This Paper Address?

Traditional unit testing relies on hand-crafted examples, which frequently miss subtle edge cases that human developers fail to anticipate. Property-based testing (PBT)—pioneered by Haskell’s QuickCheck and implemented in Python via Hypothesis—addresses this by testing general program invariants (such as "reconstructing slices should yield the original data" or "outputs must be non-negative") across hundreds of automatically generated, randomized inputs.

However, PBT is underutilized because defining formal, mathematically sound properties across complex codebases requires significant domain expertise and time. While recent work has explored using large language models (LLMs) to generate PBTs, previous attempts were strictly "single-shot" and targeted isolated functions, capturing only a fraction of valid properties. The authors investigate whether an agentic, multi-step LLM workflow can autonomously explore whole codebases, deduce cross-function invariants, execute tests, triage failures, and surface legitimate, reproducible bugs.

Methods

The agent is built on Anthropic’s Claude Code (driven by Claude Opus 4.1) using an iterative prompt-based workflow with terminal and tool access:

  1. Target Analysis & Introspection: Inspects the target Python module, class, or function using runtime reflection (inspect, dir) and file tools to map call graphs, signatures, and documentation.
  2. Property Proposal: Mined invariants are grounded strictly in code and documentation. Candidate patterns include round-trip transformations (decode(encode(x)) == x), algebraic properties (commutativity, idempotence), invariant bounds, and differential execution between implementations.
  3. PBT Synthesis: Translates properties into Hypothesis tests with smart input strategies. The agent prioritizes soundness over completeness to avoid testing invalid precondition states.
  4. Execution & Self-Triage: Executes tests with pytest. If a test fails, the agent applies a triage rubric to verify consistency, rule out out-of-domain caller assumptions, and filter out false alarms.
  5. Actionable Bug Reporting: For confirmed bugs, the agent produces a Markdown report containing a minimal standalone reproduction script, an explanation of the violated contract, and often a git diff patch.

Datasets

The authors evaluated the agent across 100 Python packages encompassing 933 modules:

  • Standard Library Modules ($n = 15$): Core packages including json, pathlib, datetime, urllib, and decimal.
  • High-Profile PyPI Packages ($n = 15$): Widely used libraries such as numpy, pandas, requests, scipy, django, pydantic, and black.
  • Randomly Sampled Packages ($n = 70$): Sampled from the top 5,000 most-downloaded PyPI packages across domains like machine learning, cloud SDKs, and data engineering.

Key Insights and Ideas

  • Iterative Reflection Beats Single-Shot Generation: Single-turn LLMs often hypothesize properties that violate implicit preconditions. By inspecting caller call-sites and self-triaging failures, the agent refines input strategies to match realistic usage.
  • Diverse Real-World Bug Discovery: PBT is exceptionally well-suited for discovering bugs that escape human intuition. Discovered bugs included:
  • Numerical issues: Catastrophic floating-point cancellation in numpy.random.wald yielding negative sample values.
  • Stateful iteration bugs: aws_lambda_powertools.shared.functions.slice_dictionary repeatedly yielding the first chunk because the internal iterator was not advanced.
  • Silent logic flaws: item_hash() in AWS CloudFormation CLI calling .sort() in-place on a list comprehension, causing every list to hash to the digest of None.
  • Automated Triage Enables Practical Economics: At an average API cost of $5.56 per generated report, finding confirmed, valid bugs cost approximately $9.93 per bug.

Key Results

  • Scale: The agent ran for 136.6 hours across 933 modules, consuming 2.21 billion tokens and generating 984 bug reports across 786 modules (84.2%).
  • Accuracy: In a blind manual review of a random sample ($n = 50$):
  • 56.0% (95% CI: 42.2%–69.8%) were verified as valid bugs.
  • 32.0% (95% CI: 19.1%–44.9%) were high-value bugs worth reporting to maintainers.
  • Scoring Rubric: After filtering reports with a 15-point heuristic scoring rubric, 86% of the 21 top-scoring bugs were valid, and 81% (17 bugs) were verified maintainer-reportable.
  • Real-World Impact: 5 bugs were submitted to upstream maintainers (4 with patches). 3 patches were merged into upstream repositories (NumPy, AWS Lambda Powertools, and Hugging Face Tokenizers), 1 patch was submitted (CloudFormation CLI), and 1 generated issue clarification in python-dateutil.

Limitations and Future Work

  • Intent Ambiguity: The agent occasionally flags behavior that violates general programming intuition but is intentional according to the library authors (e.g., requests.LookupDict inheriting from dict but intentionally omitting standard dictionary behavior).
  • Incomplete Human Verification: Due to scale, not all 984 reports were manually audited; the estimated false discovery rate remains between 30.2% and 57.8%.
  • Dual-Use Security Concerns: Fully automated, scalable property testing could be repurposed by malicious actors to autonomously uncover zero-day vulnerabilities in critical infrastructure.

Summary updated

BibTeX
@inproceedings{maaz2025agentic,
  author = {Muhammad Maaz and Liam DeVoe and Zac Hatfield-Dodds and Nicholas Carlini},
  title = {{Agentic Property-Based Testing: Finding Bugs Across the Python Ecosystem}},
  booktitle = {DL4C @ NeurIPS},
  year = {2025},
  url = {https://openreview.net/forum?id=0ajBvBWKrB},
}
Figure