Skip to main content
Unit testing with Composo allows you to catch LLM quality regressions before they reach production. By integrating evaluations directly into your test suite, you can ensure consistent behavior across code changes and deployments.

Why Unit Test LLM Applications?

Traditional testing approaches fall short for LLM applications because:
  • Non-deterministic outputs: LLMs produce different responses for the same input
  • Subjective quality: Success isn’t just about correctness—it’s about tone, helpfulness, safety, and domain-specific requirements
  • Expensive manual review: Human evaluation doesn’t scale during development
Composo solves this by providing deterministic, quantitative scores for subjective qualities, enabling you to write automated tests like:

Basic Setup

First, install the required packages:
Set your API key as an environment variable:

Writing Your First Unit Test

Here’s a complete example showing how to test your LLM responses for accuracy and tone:
test_llm.py
Run your tests with:

Understanding Test Results

The first test passes because the response is factually correct. The second test fails because the tone is condescending, not friendly:
This demonstrates how Composo catches quality issues that traditional assertions miss.

Common Testing Patterns

Testing Multiple Criteria

Evaluate responses across multiple quality dimensions simultaneously:
Python

Testing with Parametrized Inputs

Use pytest’s parametrization to test multiple scenarios efficiently:
Python

Testing Safety Guardrails

Ensure your LLM properly handles harmful or out-of-scope requests:
Python

Best Practices

Set Appropriate Thresholds: Not all criteria require 0.95+. Adjust thresholds based on:
  • Critical quality aspects (accuracy, safety): 0.90-0.95+
  • Important but subjective (tone, style): 0.75-0.85
  • Nice-to-have improvements: 0.60-0.75
Test Edge Cases: Focus on boundary conditions where your LLM might struggle:
  • Ambiguous queries
  • Requests outside intended scope
  • Multilingual inputs
  • Adversarial prompts

Continuous Integration

Add Composo tests to your CI/CD pipeline to catch quality regressions automatically: