Skip to main content
AI Predicates are AI-powered comparison functions that evaluate inputs based on complex properties like semantic meaning or style matching.
Some other libraries call similar functions “LLM-as-a-Judge”.
This is a premium feature that requires a Merit API key.
Using AI Predicates enables:
  • Asserting on semantic properties like factual consistency and topic coverage
  • Asserting on formatting properties like style and layout matching
  • Asserting on behavioral properties like policy following

Basic Usage

Merit provides 8 built-in AI predicates for common LLM evaluation scenarios. All predicates are async functions that return PredicateResult objects with boolean values, confidence scores, and explanatory messages.

Factual Accuracy

has_conflicting_facts

Detects when generated text contradicts source material.

has_unsupported_facts

Catches hallucinations - facts the LLM invented that aren’t grounded in source material.

has_facts

Verifies that required information appears in the output. Use when certain facts must be mentioned.

matches_facts

Checks bidirectional factual equivalence - both texts convey the same information.

Topic Coverage

has_topics

Verifies output covers required subjects. Useful for content generation where specific themes must be addressed.

Policy Compliance

follows_policy

Ensures LLM outputs adhere to business rules, safety guidelines, or content policies.

Style and Structure

matches_writing_style

Validates tone, formality, and voice match a reference example.

matches_writing_layout

Checks document structure and formatting patterns match a template.

Saving Results for Investigation

When you run merits with database persistence enabled (default behavior), all AI predicate evaluations used inside assert statements are automatically saved to the Merit database. This enables post-run analysis, debugging, and quality monitoring. Every PredicateResult evaluated in an assertion is stored with full context. After the run completes, you can investigate these evaluations even if all tests passed.

Available Data for Analysis

The database stores comprehensive information for each predicate evaluation: Per-predicate data:
  • predicate_name: Which predicate function was used (e.g., “has_conflicting_facts”)
  • actual: The full text that was evaluated
  • reference: The reference text used for comparison
  • strict: Whether strict mode was enabled
  • confidence: The AI judge’s confidence score (0.0 to 1.0)
  • value: Boolean result (True/False)
  • message: The AI’s reasoning and explanation
Linkage to test context:
  • Which test execution it came from
  • Which assertion it was part of
  • Associated run ID for filtering by test session
Database persistence is controlled by the --save-to-db flag (enabled by default). Database location defaults to .merit/merit.db in your project root.

Building Custom Predicates

While Merit provides 8 built-in AI predicates, you can create custom predicates for domain-specific comparisons or integrate third-party LLM evaluation tools. Use the @predicate decorator to ensure your custom predicates integrate seamlessly with Merit’s assertion tracking and database persistence.

Protocol Conformance Requirements

The @predicate decorator transforms ordinary comparison functions into protocol-conforming predicates. To be eligible for decoration, your function must satisfy the Predicate protocol’s signature constraints: Signature Requirements:
  1. Return type: Must return bool representing the evaluation outcome
  2. Required parameters: Must accept actual and reference as either:
    • The first two positional parameters, or
    • Named keyword parameters (actual=, reference=)
  3. Execution model: Can be synchronous or asynchronous—the decorator adapts to both def and async def functions
  4. Additional parameters: May accept optional keyword arguments (e.g., strict, tolerance, domain-specific flags)

Custom Predicate Examples

Example: Integrating third-party LLM judge
When building custom predicates, follow the naming convention of starting with action verbs like has_, matches_, follows_, or contains_ to make assertions read naturally.

Recommendations

1. Use AI predicates for natural language assertions

AI predicates shine when evaluating LLM outputs where exact string matching is too brittle. Don’t do this:
Do this:

2. Combine multiple predicates for comprehensive validation

Layer semantic checks to validate different aspects of LLM outputs. This provides stronger guarantees than single assertions.

3. Use strict mode appropriately

The strict parameter controls comparison sensitivity. Use strict=False (default) for semantic flexibility, and strict=True when precision matters.