Skip to content

Get started

The Python API

Everything the CLI does, the library does. Use it when the evaluation belongs inside a script, a notebook or a test suite rather than beside a config file.

from oloproof import evaluate, system, current_case
from oloproof.evaluators import ExactMatch, Contains, JsonSchema, Regex, RubricJudge, evaluator

Everything under oloproof_core is engine internals and is not part of this surface.

A system takes a case's input, not the case

This is the one thing worth getting right first, because getting it wrong fails quietly.

@system(name="support-bot", version="1")
def answer(case):
    return {"label": "refund" if "refund" in case["question"].lower() else "other"}

A dataset row looks like this:

{"id":"refund_00","input":{"question":"Can I get a refund? #0"},"expected":{"label":"refund"}}

The function receives the input object, so case["question"] is the question and there is no case["expected"]. What it returns is the output an evaluator reads, so ExactMatch(field="label") compares the returned label against the case's expected label.

A system can also be a method of an object, such as evaluate(system=model.answer, ...), or a callable object. It must then declare a version, as in evaluate(system=system(model.answer, version="v2"), ...): what it does depends on the object's state, which Oloproof cannot see, and two objects with the same method would otherwise share cached results. Change the version whenever that state changes. @system(version=...) written on the method inside the class, or on a callable class, is refused, since it would be one version for every object; use system(model, version="v2") for a callable object.

Running one

result = evaluate(
    system=answer,
    dataset="data/example.jsonl",
    evaluators=[ExactMatch(criterion="exact_label", field="label")],
)

for metric in result.metrics:
    print(metric.metric, metric.estimate, metric.interval, metric.n_observed, metric.n_missing)
exact_label 1.0 lower=0.8842966917779722 upper=1.0 30 0

Thirty cases, all correct, and the interval still reaches down to 88.4%. Thirty cases cannot establish more than that, whatever the estimate says.

Reading a case that failed

If the system raises, that case is missing rather than wrong, and the metric reports it:

exact_label None lower=0.0 upper=1.0 0 30

An estimate of None with an interval of the whole range means nothing was observed. Check n_missing before trusting an estimate: a system whose every case raised produces a result object that is structurally fine, and the count is what tells you it is empty.

That is the denominator contract doing its job rather than a failure of it (a case that errored is bounded, not dropped), but nothing raises on your behalf, so the check is yours.

Writing your own evaluator

@evaluator turns a function into an evaluator. The function takes one argument, the case, and reads what it needs from it: case.output is what the system returned and case.expected is the row's expected object.

from oloproof.evaluators import evaluator

@evaluator(criterion="known_label", reads=("output",))
def known_label(case):
    return case.output["label"] in {"refund", "other"}

result = evaluate(system=answer, dataset="data/example.jsonl", evaluators=[known_label])
known_label 1.0 lower=0.8842966917779722 upper=1.0 30 0

reads declares which parts of the case it reads, for provenance; the default is ("output", "expected"). It returns True or False, or a score if it declares value_type="score" and the score_range its scores lie in. Its version includes a digest of the file that defines it, so editing it invalidates its cached judgments.

A function written as def f(output, expected) is refused when it is declared, with the form that works, rather than failing on every case.

A custom evaluator runs from evaluate(...); oloproof.yaml cannot name one yet. Cases, outputs and judge prompts are JSON text; images, audio and video are recorded by reference and shown to a judge declared with media (Images, audio and video).

Applying a policy

evaluate takes a policy and decides the run, the same rules the CLI reads from release.yaml:

result = evaluate(
    system=answer,
    dataset="data/example.jsonl",
    evaluators=[ExactMatch(criterion="exact_label", field="label")],
    policy="release.yaml",
)
print(result.gate)

Without a policy, result.gate is None: there is no rule to fall short of, so there is nothing to decide.

In a pytest test

assert_gate turns a decided result into a test outcome, so a failing test reads as the verdict:

from oloproof import assert_gate, evaluate


def test_release_bar():
    result = evaluate(
        system=answer,
        dataset="data/example.jsonl",
        evaluators=[ExactMatch(criterion="exact_label", field="label")],
        policy="release.yaml",
    )
    assert_gate(result)

It passes silently when the gate allows the release, raises an AssertionError when the gate blocks it, and warns (GateWarning) when the policy only warns. A rule that is not established yet blocks unless the policy moves it to warn_on. Without a policy it raises rather than passing, since nothing was decided. The message names each rule that did not pass, with its estimate, range and bar:

Oloproof gate BLOCK: of 1 rule, 1 not established yet.
- quality (resolved): not established yet; the bar is at least 99.0%
    100.0%, range 69.1% to 100.0% (exact binomial interval); 10 of 10 cases

It takes the result of evaluate_comparison too.

The job summary plugin

Run pytest with -p oloproof.pytest to collect every gate assert_gate saw. The plugin is opt-in: installing Oloproof does not register it, so it does nothing unless named. At the end of the session it appends a section to the file GITHUB_STEP_SUMMARY names, one block per gated test with the gate line and each rule's answer, so a GitHub Actions job shows the verdict on its summary page. Outside GitHub Actions it prints the same section in the terminal report. Gates that block or warn are shown too. It decides nothing itself and makes no network call.

pytest -p oloproof.pytest tests/

Custom checks

A check of your own has three routes, each decided the same way:

  • an @evaluator function in Python, passed in evaluators=[...];
  • a deterministic check in YAML (exact match, contains, a regex, a field path), see Writing a suite;
  • a file of recorded answers checked by oloproof eval FILE, with no system to call.

Keys in CI

import oloproof keys the process's digests from the oloproof.yaml found in the working directory or one of its parents, so run pytest from the project (where oloproof.yaml sits beside the tests). In a project that withholds content, a CI job then needs OLOPROOF_EXPORT_KEY; a missing key is raised when the store opens, not at import. The CI guide says how.

Where to go next