How to beginner · 3 min read

DSPy assertions explained

Quick answer
In dspy, assertions are used to validate AI model outputs against expected conditions or Pydantic schemas, ensuring the generated data meets your requirements. They help catch errors early by enforcing constraints on the output fields during prediction.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install dspy openai pydantic

Setup

Install dspy along with openai and pydantic to enable AI model integration and structured output validation.

bash
pip install dspy openai pydantic

Step by step

This example shows how to define a dspy.Signature with output fields and add assertions to validate the AI response. Assertions ensure the output meets specified conditions, such as type checks or custom logic.

python
import os
import dspy
from pydantic import BaseModel, validator

# Initialize the LM with OpenAI GPT-4o-mini
lm = dspy.LM("openai/gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"])
dspy.configure(lm=lm)

# Define a signature with input and output fields
class QA(dspy.Signature):
    question: str = dspy.InputField()
    answer: str = dspy.OutputField()

    # Add an assertion to ensure answer is not empty
    def assert_answer_not_empty(self):
        assert self.answer.strip() != "", "Answer must not be empty"

# Create a predictor instance
qa = dspy.Predict(QA)

# Run prediction
result = qa(question="What is DSPy?")
print("Answer:", result.answer)

# Run assertions explicitly
qa.assertions(result)  # Raises AssertionError if any assertion fails
output
Answer: DSPy is a Python library for declarative AI programming that uses Pydantic models to validate and structure AI outputs.

Common variations

You can define multiple assertions in your dspy.Signature class to validate different output aspects. Assertions can be simple Python assert statements or use Pydantic validators for complex validation.

For async usage, dspy supports async prediction methods with the same assertion mechanism.

python
import asyncio

async def async_example():
    result = await qa.apredict(question="Explain assertions in DSPy.")
    print("Async answer:", result.answer)
    qa.assertions(result)

asyncio.run(async_example())
output
Async answer: Assertions in DSPy are used to enforce output correctness by validating fields after prediction.

Troubleshooting

If an assertion fails, dspy raises an AssertionError with the message you provided. To debug, check the AI output and adjust your assertions or prompt accordingly.

Ensure your environment variable OPENAI_API_KEY is set correctly to avoid authentication errors.

Key Takeaways

  • Use dspy assertions to enforce output validity and catch errors early.
  • Assertions can be simple assert statements or Pydantic validators within your signature class.
  • Run assertions after prediction to ensure AI responses meet your structured requirements.
Verified 2026-04 · gpt-4o-mini
Verify ↗