Core

Matcher

One value-matching language for eval expectations and policy conditions, with tri-state results and errors that point at the typo.

A matcher is plain data, so a YAML case, a JSON policy and inline Python or TypeScript are the same document.

  • A literal (string, number, boolean, null) matches by deep equality. 1 equals 1.0; true never equals 1.
  • An array matches an array of the same length, element by element.
  • An object whose keys are all operators applies every operator.
  • Any other object is a partial field pattern: listed keys must match, extra keys are allowed, a missing key fails.
  • An object that mixes operators and field names is a load error. To match a field whose name is an operator, use fields: { fields: { is: "x" } }.
main.py
from viewportai.core import match

call = {"tool_name": "issue_refund", "args": {"order_id": "A-1001", "amount": 42.5, "reason": "Damaged on arrival"}}

print(str(match({"tool_name": "issue_refund"}, call).passed).lower())  # true
print(
    str(match({"args": {"order_id": {"regex": "^A-\\d+$"}, "amount": {"gt": 0, "lte": 100}}}, call).passed).lower()
)  # true
print(
    str(match({"args": {"reason": {"icontains": "DAMAGED"}, "note": {"exists": False}}}, call).passed).lower()
)  # true
print(str(match({"jsonpath": {"$.args.amount": {"is": "number"}}}, call).passed).lower())  # true
print(str(not match({"args": {"amount": {"lt": 10}}}, call).passed).lower())  # true: 42.5 is not below 10
main.ts
import { match } from "@viewportai/core";

const call = { tool_name: "issue_refund", args: { order_id: "A-1001", amount: 42.5, reason: "Damaged on arrival" } };

console.log(match({ tool_name: "issue_refund" }, call).passed); // true
console.log(match({ args: { order_id: { regex: "^A-\\d+$" }, amount: { gt: 0, lte: 100 } } }, call).passed); // true
console.log(match({ args: { reason: { icontains: "DAMAGED" }, note: { exists: false } } }, call).passed); // true
console.log(match({ jsonpath: { "$.args.amount": { is: "number" } } }, call).passed); // true
console.log(!match({ args: { amount: { lt: 10 } } }, call).passed); // true: 42.5 is not below 10

Tri-state results

Every result has a status: pass, fail or unknown. unknown means the question could not be answered: a typed operator met the wrong JSON type (gt on a string, regex on a number), or a string that should hold JSON did not parse. A missing value is always fail. Combination follows Kleene logic: in an AND (operator objects, field patterns, arrays, all_of) fail beats unknown beats pass; in an OR (any_of, contains over arrays) pass beats unknown beats fail; not swaps pass and fail and keeps unknown.

passed is true only for pass. A policy engine that fails closed, like Viewport HITL, treats unknown as matching its deny and ask rules and as not matching its allow rules, so an argument of the wrong type can never slip through an allow rule. Tools validate their arguments strictly as JSON ("false" is not a boolean) before any middleware runs, so the value a policy condition matched is exactly the value the tool receives.

Operator names are exact. A field-pattern key that is an operator written differently (anyOf, startsWith, NOT) or one edit away from one (contans) is a load error with a did-you-mean, so a typo can never quietly turn a condition into a field pattern that never matches.

Compile once, with checks

compile_matcher / compileMatcher validates a spec once and returns an immutable matcher you can share across threads and tasks. Every problem is reported at once, with did-you-mean hints, and an optional allowlist of operators keeps, for example, judge out of policies.

main.py
from viewportai.core import SpecError, compile_matcher

try:
    compile_matcher({"args": {"amount": {"lte": 100, "contans": "x"}}}, at="rules[0].when", file="policy.yaml")
except SpecError as err:
    print(err)
    # policy.yaml is invalid (1 issue)
    #   policy.yaml rules[0].when.args.amount.contans: not an operator; an object that uses operators cannot also
    #   list fields (wrap fields in `fields`). Did you mean "contains"?
main.ts
import { compileMatcher, type SpecError } from "@viewportai/core";

try {
  compileMatcher({ judge: { rubric: "polite" } }, { operators: ["equals", "regex"], at: "rules[0].when" });
} catch (err) {
  console.log((err as SpecError).issues[0]?.code); // operator_not_allowed
}

json_schema and jsonpath read a string value as a JSON document by default (what an eval wants when a model returns JSON as text). Compile with parse_json_strings=False / { parseJsonStrings: false } to check a string as the string itself: the mode HITL uses for policy conditions, where a tool argument that happens to hold JSON text must not be read as an object. The option is fixed at compile time and shown on compiled.options, so a policy digest covers it. In that mode JSON Schema rules apply as written, so maximum only constrains numbers: pair it with type: number. A value holding NaN or Infinity anywhere is unknown as a whole: neither is JSON.

Judges

The judge operator asks a grader you supply. Core ships no model code; Viewport Evals provides LLM judges. Specs that use judge need the async form (amatch in Python, matchAsync in TypeScript).

main.py
import asyncio

from viewportai.core import JudgeRequest, JudgeVerdict, amatch


async def polite(request: JudgeRequest) -> JudgeVerdict:
    return JudgeVerdict(passed="please" in str(request.value).lower(), score=0.9)


async def main() -> None:
    result = await amatch(
        {"reply": {"judge": {"rubric": "polite", "threshold": 0.8}}}, {"reply": "Please hold."}, judge=polite
    )
    print(str(result.passed).lower())  # true


asyncio.run(main())
main.ts
import { matchAsync } from "@viewportai/core";

const result = await matchAsync(
  { reply: { judge: { rubric: "polite", threshold: 0.8 } } },
  { reply: "Please hold." },
  {
    judge: async (request) => ({ passed: String(request.value).toLowerCase().includes("please"), score: 0.9 }),
  },
);
console.log(result.passed); // true

Every operator is listed in the operator reference.

On this page