Skip to main content

Simple LLM-powered assertions for any pytest test

Project description

pytest-llm-assert

PyPI version Python versions CI License: MIT

Natural language assertions for pytest.

Testing a text-to-SQL agent? Validating LLM-generated code? Checking if error messages are helpful? Now you can:

def test_sql_agent_output(llm):
    sql = my_agent.generate("Get names of users over 21")
    
    assert llm(sql, "Is this a valid SQL query that selects user names filtered by age > 21?")

The LLM evaluates your criterion and returns pass/fail — no regex, no parsing, no exact string matching.

Features

  • Semantic assertions — Assert meaning, not exact strings
  • Multiple LLM providers — OpenAI, Azure, Anthropic, Gemini, Groq via Pydantic AI
  • pytest native — Works as a standard pytest plugin/fixture
  • Response introspection — Access tokens and reasoning via llm.response
  • Type-safe — Built with Pydantic for structured outputs

Installation

pip install pytest-llm-assert

Quick Start

# conftest.py
import pytest
from pytest_llm_assert import LLMAssert

@pytest.fixture
def llm():
    return LLMAssert(model="openai:gpt-4o-mini")
# test_my_agent.py
def test_generated_sql_is_correct(llm):
    sql = "SELECT name FROM users WHERE age > 21 ORDER BY name"
    assert llm(sql, "Is this a valid SELECT query that returns names of users over 21?")

def test_error_message_is_helpful(llm):
    error = "ValidationError: 'port' must be an integer, got 'abc'"
    assert llm(error, "Does this explain what went wrong and how to fix it?")

def test_summary_captures_key_points(llm):
    summary = generate_summary(document)
    assert llm(summary, "Does this mention the contract duration and parties involved?")

Setup

Works out of the box with cloud identity — no API keys to manage:

# Azure (Entra ID)
export AZURE_API_BASE=https://your-resource.openai.azure.com
az login

# Google Cloud (Vertex AI)
gcloud auth application-default login

# AWS (Bedrock)
aws configure  # Uses IAM credentials

Supports multiple providers via Pydantic AI — including API key auth for OpenAI, Anthropic, and more.

Documentation

Related

  • pytest-aitest — Full framework for testing MCP servers, CLIs, and AI agents
  • Contributing — Development setup and guidelines

Requirements

  • Python 3.11+
  • pytest 9.0+
  • An LLM (OpenAI, Azure, Anthropic, etc.) or local Ollama

Security

  • Sensitive data: Test content is sent to LLM providers — consider data policies

License

MIT

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

pytest_llm_assert-0.2.4.tar.gz (243.3 kB view details)

Uploaded Source

Built Distribution

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

pytest_llm_assert-0.2.4-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest_llm_assert-0.2.4.tar.gz.

File metadata

  • Download URL: pytest_llm_assert-0.2.4.tar.gz
  • Upload date:
  • Size: 243.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_llm_assert-0.2.4.tar.gz
Algorithm Hash digest
SHA256 a1c306ffb03db5599dd93389f51ac48cedda83492d64b2d0173bd181c0a6a68a
MD5 4674f3b65f0df0e8e3e97011510e04b7
BLAKE2b-256 4a3b8ff3e098895008c751b5742eabd600f8b86e83649b7572768de05eebec8c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_llm_assert-0.2.4.tar.gz:

Publisher: release.yml on sbroenne/pytest-llm-assert

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_llm_assert-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_llm_assert-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a34539be0ebe124794a70d7e39f04223b8855c6d7f31cffb2e6367dbb4bc378f
MD5 f5ce1c873cb9f602ba66d52790153343
BLAKE2b-256 223bdcc9e61cfb12248937f73a17c90885d555f664f46ca156edc03541a88af6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_llm_assert-0.2.4-py3-none-any.whl:

Publisher: release.yml on sbroenne/pytest-llm-assert

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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