Skip to main content

pytest-semantix

Semantic LLM output testing for pytest. Validate that your LLM outputs mean the right thing — not just that they match a string.

pip install pytest-semantix

Usage

The assert_semantic fixture

def test_chatbot_is_polite(assert_semantic):
    response = my_chatbot("handle angry customer")
    assert_semantic(response, "polite and professional")

Runs locally on CPU in ~15ms. No API key. Works with any LLM.

On failure:

AssertionError: Semantic check failed (score=0.12)
  Intent:  polite and professional
  Output:  "You're an idiot for asking that."
  Reason:  Text contains aggressive language

Markers

Use @pytest.mark.semantic to attach an intent to a test:

import pytest

@pytest.mark.semantic("polite and professional")
def test_with_marker(assert_semantic):
    response = my_chatbot("handle angry customer")
    assert_semantic(response)  # intent comes from the marker

Intent classes

Reuse intents across tests:

from semantix import Intent

class Polite(Intent):
    """The text must be polite and professional."""

def test_polite(assert_semantic):
    assert_semantic(my_chatbot("hello"), Polite)

Negation

Test that outputs do NOT match an intent:

from semantix import Intent

class MedicalAdvice(Intent):
    """The text provides medical diagnoses or treatment recommendations."""

def test_no_medical_advice(assert_semantic):
    assert_semantic(my_chatbot("my head hurts"), ~MedicalAdvice)

CLI Options

--semantic-report          Print a summary of all semantic assertions
--semantic-report-json=PATH  Write results to a JSON file
--semantic-threshold=FLOAT   Global default threshold (0.0-1.0)

Report example

$ pytest --semantic-report

======================== semantic assertion report =========================
  Total: 5  |  Passed: 4  |  Failed: 1

  [PASS] tests/test_bot.py::test_polite  [12ms]
  [PASS] tests/test_bot.py::test_helpful  [14ms]
  [FAIL] tests/test_bot.py::test_no_pii  (score=0.67)  Contains email address  [11ms]
  [PASS] tests/test_bot.py::test_on_topic  [13ms]
  [PASS] tests/test_bot.py::test_concise  [15ms]

============================================================================

JSON report

$ pytest --semantic-report-json=semantic-results.json
{
  "summary": { "total": 5, "passed": 4, "failed": 1 },
  "results": [
    {
      "nodeid": "tests/test_bot.py::test_polite",
      "intent": "polite and professional",
      "passed": true,
      "score": null,
      "reason": "",
      "duration_ms": 12.3
    }
  ]
}

How it works

pytest-semantix wraps semantix-ai's assert_semantic() function as a pytest fixture. Under the hood, it uses a local NLI (Natural Language Inference) model to check whether your LLM output entails the given intent. No network calls, no API keys, no tokens burned.

License

MIT

Metadata

Release files for pytest-semantix 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 pytest-semantix 0.1.0
File Size Uploaded Provenance
pytest_semantix-0.1.0.tar.gz 6.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-semantix 0.1.0
File Interpreter ABI Platform
pytest_semantix-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / pytest_semantix-0.1.0.tar.gz

Download URL pytest_semantix-0.1.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fc5f99ea844d96b022079d8e4f6fc1e3afa11a93f3a9acc54ec40e5b7863685a
BLAKE2b-256 checksum
How to use checksums
5cece18c71a5fff27481c37cad03e427ce95eb71cdb1f4ec05e87704e8e75de9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

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

Download URL pytest_semantix-0.1.0-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a04538616ef44fb26f6db1d9aba442c307363b7bd52581708ec5be3ce10f10a
BLAKE2b-256 checksum
How to use checksums
c3ff524f802367f28e69b1b20d2b2f400c890aba94fdf38f3273f2053f28f0ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for pytest-semantix 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.

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