Skip to main content

Testing library for AI Agents by InvariantLabs

Project description

Invariant testing

Helps you build better AI agents through debuggable unit testing

Documentation


Invariant testing is a lightweight library to write and run AI agent tests. It provides helpers and assertions that enable you to write robust tests for your agentic applications.

Using localized assertions, testing always points you to the exact part of the agent's behavior that caused a test to fail, making it easy to debug and resolve issues (think: stacktraces for agents).



Installation

pip install invariant-ai

A quick example

The example below uses extract(...) to detect locations from messages. This uses the gpt-4o model from OpenAI.

Setup your OpenAI key as

export OPENAI_API_KEY=<your-key>

Code:

# content of tests/test_weather.py
import invariant.testing.functional as F
from invariant.testing import Trace, assert_equals

def test_weather():
    # create a Trace object from your agent trajectory
    trace = Trace(
        trace=[
            {"role": "user", "content": "What is the weather like in Paris?"},
            {"role": "agent", "content": "The weather in London is 75°F and sunny."},
        ]
    )

    # make assertions about the agent's behavior
    with trace.as_context():
        # extract the locations mentioned in the agent's response using OpenAI
        locations = trace.messages()[-1]["content"].extract("locations")

        # assert that the agent responded about Paris and only Paris
        assert_equals(1, F.len(locations),
            "The agent should respond about one location only")

        assert_equals("Paris", locations[0], "The agent should respond about Paris")

Execute it on the command line:

$ invariant test
________________________________ test_weather _________________________________
ERROR: 1 hard assertions failed:

 
    # assert that the agent responded about Paris and only Paris
    assert_equals(1, locations.len(), 
        "The agent should respond about one location only")
    
>   assert_equals("Paris", locations[0], "The agent should respond about Paris")
________________________________________________________________________________

ASSERTION FAILED: The agent should respond about Paris (expected: 'Paris', actual: 'London')
________________________________________________________________________________

#       role:  "user"
#       content:  "What is the weather like in Paris?"
#     },
#     {
#       role:  "agent"
        content:   "The weather in London is 75°F and sunny."
#     },
#  ]

The test result precisely localizes the failure in the provided agent trace.

Visual Test Viewer (Explorer):

As an alternative to the command line, you can also visualize test results on the Invariant Explorer:

$ invariant test --push

image

Like the terminal output, the Explorer highlights the relevant ranges, but does so even more precisely, marking the exact words that caused the assertion to fail.

Features

  • Comprehensive Trace API for easily navigating and checking agent traces.
  • Assertions library to check agent behavior, including fuzzy checkers such as Levenshtein distance, semantic similarity and LLM-as-a-judge pipelines.
  • Full pytest compatibility for easy integration with existing test and CI/CD pipelines.
  • Parameterized tests for testing multiple scenarios with a single test function.
  • Visual test viewer for exploring large traces and debugging test failures in Explorer

To learn more read the documentation

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

invariant_ai-0.1.1.tar.gz (46.4 kB view details)

Uploaded Source

Built Distribution

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

invariant_ai-0.1.1-py3-none-any.whl (61.2 kB view details)

Uploaded Python 3

File details

Details for the file invariant_ai-0.1.1.tar.gz.

File metadata

  • Download URL: invariant_ai-0.1.1.tar.gz
  • Upload date:
  • Size: 46.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.2 Linux/6.5.0-1025-azure

File hashes

Hashes for invariant_ai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 be3dd3f6e9e2a12c81ffacca074df5fa63a72eb4f2185a440063899943b06265
MD5 931dceb21ba345a2a4acbf03fa3add87
BLAKE2b-256 e1be54f2c2ef41c4381e6f6af699dc97c645c73fb6536e2e4908a8220b3efc25

See more details on using hashes here.

File details

Details for the file invariant_ai-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: invariant_ai-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 61.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.2 Linux/6.5.0-1025-azure

File hashes

Hashes for invariant_ai-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b5620105b122f6ecd3fd65cdc4fc8e6f62270fb983a410a5855693f72183263a
MD5 5c6037c847b1164e08e18865ed62ecf8
BLAKE2b-256 f4700eb9250810fd5b81bb5458a16fd1729e226ebd319dd033234bd8c1c73746

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