Skip to main content

A Python library for verifying code properties using natural language assertions.

Project description

intentguard

IntentGuard

GitHub Sponsors PyPI - Downloads GitHub License PyPI - Version PyPI - Python Version

IntentGuard lets you test code intent with natural language assertions.

Use it when a property is real and test-worthy, but awkward to encode with ordinary assertions: architecture rules, security practices, documentation contracts, error-handling conventions, or other cross-cutting code qualities. IntentGuard checks referenced code with a local model and raises AssertionError when the judgement fails.

IntentGuard complements traditional tests. Keep unit tests for exact outputs, edge cases, state changes, and safety-critical behavior. Use IntentGuard where a custom AST walk, linter rule, or review checklist would be noisy or expensive.

[!IMPORTANT] The current default model is IntentGuard-1-qwen2.5-coder-1.5b, with 92.5% accuracy and 92.3% precision in the validation suite.

Installation

pip install intentguard

Quick Start

With pytest

import intentguard as ig

def test_code_properties():
    ig.assert_code(
        "Classes in {module} should follow the Single Responsibility Principle",
        {"module": my_module}
    )

    ig.assert_code(
        "All database queries in {module} should be parameterized to prevent SQL injection",
        {"module": db_module}
    )

With unittest

import unittest
import intentguard as ig

class TestCodeQuality(unittest.TestCase):
    def test_error_handling(self):
        ig.assert_code(
            "All API endpoints in {module} should have proper input validation",
            {"module": api_module}
        )

Good Fits

IntentGuard works best for high-level properties that are easy to describe and hard to check directly:

  • "All public methods in {module} should have docstrings with Parameters and Returns sections."
  • "All API endpoints in {module} should validate input before using it."
  • "All methods in {module} should log errors before re-raising them."

Avoid using it for exact numeric results, runtime behavior that must be executed, or anything that needs perfect determinism. Model judgement is useful signal, not proof.

How It Works

  1. assert_code() receives a natural language assertion and code references.
  2. Code references are converted into source snippets.
  3. IntentGuard builds a structured prompt and checks the cache.
  4. On cache miss, the local model evaluates the assertion num_evaluations times.
  5. A strict majority decides the result. Ties fail.
  6. The result is cached for repeat runs.

Near-Deterministic Results

IntentGuard is designed for repeatable judgements, not guaranteed determinism. It uses low-temperature sampling, repeated evaluation, strict majority voting, and caching to make results stable in normal test runs. Fresh model evaluations can still vary, especially after changing the assertion, code, model, temperature, or evaluation count.

Configure repeatability:

import intentguard as ig

ig.set_default_options(
    ig.IntentGuardOptions(
        num_evaluations=7,  # More evaluations make majority vote more stable
        temperature=0.1,    # Lower temperature reduces sampling variance
    )
)

Use module-level ig.assert_code(...) for ordinary tests. Use ig.IntentGuard(options) when one test class, suite, or subsystem needs isolated options.

Model

IntentGuard uses a custom 1.5B parameter model, fine-tuned from Qwen2.5-Coder-1.5B for code property verification. It runs locally through llamafile, so code is not sent to a hosted API by default.

Performance

Model Accuracy Precision Recall
(current model) IntentGuard-1-qwen2.5-coder-1.5b 92.5% 92.3% 89.4%
(previous model) IntentGuard-1-llama3.2-1b 92.4% 91.0% 91.0%
(reference model) gpt-4o-mini 89.3% 85.3% 90.2%

Validation Methodology

The validation suite is intentionally strict:

  • Each test example gets 15 total evaluations (5 trials x 3 evaluations per trial)
  • A voting mechanism is applied within each group (jury size = 3)
  • A test passes only if all 5 trials succeed with majority agreement (2 out of 3 or better)

For more details, see the validation documentation.

Compatibility

IntentGuard requires Python 3.10+. OS and architecture support come from llamafile:

  • Linux 2.6.18+
  • macOS 23.1.0+ (GPU support on ARM64)
  • Windows 10+ (AMD64)
  • FreeBSD 13+
  • NetBSD 9.2+ (AMD64)
  • OpenBSD 7+ (AMD64)

Local Development Environment Setup

  1. Prerequisites: Python 3.10+, uv.
  2. Clone: git clone <repository_url> && cd intentguard
  3. Install dev dependencies: make install or uv sync --dev --group validation --group dataset
  4. Run tests & checks: make test

Useful commands:

  • make install: Installs development dependencies.
  • make install-prod: Installs production dependencies only.
  • make check: Runs linting checks (ruff check).
  • make format-check: Checks code formatting (ruff format --check).
  • make mypy: Runs static type checking (mypy).
  • make unittest: Runs unit tests.
  • make test: Runs all checks and tests.
  • make clean: Removes the virtual environment.
  • make help: Lists available make commands.

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

intentguard-2.2.0.tar.gz (174.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

intentguard-2.2.0-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file intentguard-2.2.0.tar.gz.

File metadata

  • Download URL: intentguard-2.2.0.tar.gz
  • Upload date:
  • Size: 174.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for intentguard-2.2.0.tar.gz
Algorithm Hash digest
SHA256 56932da67c2ccfad7e488f2d695dc0bc8bb15e00b088d7dbbfb7c15a1da370ef
MD5 892c51fefce93026b51c15759e55336f
BLAKE2b-256 9c384ffd8053a36a4140d31ba060b07add60f123fb3f977a40d5099d976fbd37

See more details on using hashes here.

File details

Details for the file intentguard-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: intentguard-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for intentguard-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89ea5d655a063cb1ff068c7e09a6e59decdd8778bb6e7066a6c7a17706d85bfb
MD5 80870bd56c8b769683c23bfc276eb3bb
BLAKE2b-256 dc1cc2482dfcee556f7c4b15b0f5969451a5bf3147f793e703eb288de46875dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page