Built-in assertion library for LLM outputs
20+ purpose-built assertions for LLM outputs: text content, safety, JSON structure, quality, and numeric checks. Use in test suites or standalone.The assertion library gives you 20+ purpose-built checks for LLM outputs — covering content correctness, safety, structure, style, and performance. Import
expect from @evalgate/sdk (TypeScript) or evalgate_sdk (Python) and chain assertion methods directly against any string or value your model produces.
Import
Text and content
These assertions check what the output contains, matches, or excludes.toEqual(expected) / to_equal(expected)
toEqual(expected) / to_equal(expected)
expected.toContain(substring) / to_contain(substring)
toContain(substring) / to_contain(substring)
substring as a literal substring.toContainKeywords(keywords[]) / to_contain_keywords(keywords[])
toContainKeywords(keywords[]) / to_contain_keywords(keywords[])
toNotContain(substring) / to_not_contain(substring)
toNotContain(substring) / to_not_contain(substring)
substring.toMatchPattern(regex) / to_match_pattern(regex)
toMatchPattern(regex) / to_match_pattern(regex)
toHaveLength({ min, max }) / to_have_length({ min, max })
toHaveLength({ min, max }) / to_have_length({ min, max })
Safety and compliance
These assertions catch outputs that could expose sensitive data or violate content policies.toNotContainPII() / to_not_contain_pii()
toNotContainPII() / to_not_contain_pii()
toBeProfessional() / to_be_professional()
toBeProfessional() / to_be_professional()
toNotHallucinate(facts[]) / to_not_hallucinate(facts[])
toNotHallucinate(facts[]) / to_not_hallucinate(facts[])
facts[] must be grounded in the output. This is a local, heuristic check. For model-backed factual checks, use the exported async helpers such as hasNoHallucinationsAsync() after configuring assertion provider access.JSON and structure
These assertions verify the shape and contents of structured outputs.toBeValidJSON() / to_be_valid_json()
toBeValidJSON() / to_be_valid_json()
toMatchJSON(schema) / to_match_json(schema)
toMatchJSON(schema) / to_match_json(schema)
schema must be present in the parsed JSON output.toContainCode() / to_contain_code()
toContainCode() / to_contain_code()
Quality and style
These assertions check the tone and grammatical correctness of the output.toHaveSentiment(type) / to_have_sentiment(type)
toHaveSentiment(type) / to_have_sentiment(type)
type. Accepted values: 'positive', 'negative', 'neutral'.toHaveProperGrammar() / to_have_proper_grammar()
toHaveProperGrammar() / to_have_proper_grammar()
Numeric and performance
These assertions are useful for checking latency, numeric scores, or any value-based property of your AI system.toBeFasterThan(ms) / to_be_faster_than(ms)
toBeFasterThan(ms) / to_be_faster_than(ms)
ms milliseconds.toBeGreaterThan(n) / to_be_greater_than(n)
toBeGreaterThan(n) / to_be_greater_than(n)
n.toBeLessThan(n) / to_be_less_than(n)
toBeLessThan(n) / to_be_less_than(n)
n.toBeBetween(min, max) / to_be_between(min, max)
toBeBetween(min, max) / to_be_between(min, max)
[min, max].toBeTruthy() / to_be_truthy()
toBeTruthy() / to_be_truthy()
toBeFalsy() / to_be_falsy()
toBeFalsy() / to_be_falsy()
Tagging assertions by cost tier
UsewithCostTier() to label fluent assertion results for cost tracking and prioritization. The tier is metadata; it does not change the assertion implementation or make the runner skip a check by itself.
'low', 'medium', and 'high'. They are labels for budget tracking/prioritization; choosing a tier does not turn a local assertion into a model call.
LLM-backed hallucination check
hasNoHallucinationsAsync() is the exported model-backed hallucination helper. Configure assertions with an explicit provider/API key (or pass the config directly) before calling it:
configureAssertions() configures the external assertion provider. This is a model-provider credential boundary, not an EvalGate platform API-key shortcut.Using assertions in a test suite
The most common pattern is to pass assertions as functions insidecreateTestSuite cases. Each assertion receives the executor’s output and returns a result: