Skip to Content
Documentationpytest Plugin@llm_test Decorator

@llm_test Decorator

The primary way to write LLM tests.

Basic usage

from llmtest import expect, llm_test @llm_test( expect.contains("Paris"), expect.latency_under(2000), model="gpt-5-mini", ) def test_capital(llm): output = llm("What is the capital of France?") assert "Paris" in output.content

Parameters

ParameterTypeDefaultDescription
*assertionsBaseAssertionAssertions to check after test
providerstr"openai"LLM provider
modelstrModel name
system_promptstrNoneSystem prompt
temperaturefloatNoneSampling temperature
max_tokensintNoneMax output tokens
tagslist[str]NoneTest tags for filtering
retriesint0Retry count on failure
retry_delayfloat1.0Seconds between retries

How it works

  1. The decorator injects the llm callable into your test function
  2. Your test runs, making LLM calls via llm()
  3. After the test completes, all assertions are checked against the last LLMOutput
  4. If any assertion fails, the test fails with a detailed error message
  5. If retries > 0 and the test fails, it retries up to N times

Per-call overrides

@llm_test( expect.is_not_empty(), provider="anthropic", model="claude-sonnet-4-6-20260218", ) def test_with_override(llm): output = llm( "Explain gravity", model="claude-haiku-4-5-20251001", temperature=0.0, ) assert output.content
Last updated on