Retry & Runs
LLMs are non-deterministic. assertllm has built-in retry at two levels.
Decorator Retry
Decorator-level retry
Retry the entire test up to N times:
test_retry.py
@llm_test(
expect.contains("Paris"),
model="gpt-4o-mini",
retries=3,
retry_delay=0.5,
)
def test_capital_with_retry(llm):
llm("What is the capital of France?")Output
test_retry.py::test_capital_with_retry
Attempt 1/3: FAILED — contains("Paris") not found
Attempt 2/3: PASSED
✓ contains("Paris")
PASSED [1.4s]Use decorator retries when the whole test is flaky and you want it to pass if any single attempt succeeds.
When to Use Which
| Level | Use when |
|---|---|
Decorator retries | The whole test is flaky |
Decorator runs | You want statistical pass rate (e.g. 8/10) |
Fixture retry_if | A specific LLM call needs a particular output |
Last updated on