DSPy Assertions: Computational Constraints for Self-Refining Language Model Pipelines

Arnav Singhvi, Manish Shetty, Shangyin Tan, Christopher Potts, Koushik Sen, Matei Zaharia, Omar KhattabarXiv 2023 · DOI

Introduction

Large Language Models (LLMs) have demonstrated remarkable capabilities in complex reasoning and generation tasks. However, integrating them into reliable multi-stage pipelines remains challenging due to their propensity for hallucinations and formatting errors. Traditional mitigation strategies rely heavily on "prompt engineering"—a manual, iterative process of refining instructions to coax the model into compliance. This approach is inherently brittle and lacks the formal rigor required for robust software engineering.

To address this, we introduce LM Assertions, a systematic framework for enforcing computational constraints within the DSPy programming model. By defining programmatic invariants, developers can move beyond "begging" the model for specific outputs and instead enable the system to automatically self-refine and optimize its performance.

Methods: Assertions and Suggestions

The core of our approach involves two declarative constructs integrated into DSPy:

  1. dspy.Assert (Hard Constraints): Critical requirements that must be met. Failure to satisfy these after a maximum number of retries results in a pipeline halt.
  2. dspy.Suggest (Soft Constraints): Desirable properties. The system attempts to self-refine upon violation but proceeds if the constraint remains unsatisfied after retries.

These constructs enable three primary optimization mechanisms:

  • Assertion-driven Backtracking: During inference, if a constraint is violated, the framework automatically re-invokes the failing module. The new prompt includes the previous erroneous output and a specific error message, allowing the model to introspect and correct its mistake.
  • Assertion-driven Example Bootstrapping: During the compilation phase, assertions act as filters. A "teacher" model uses backtracking to ensure only high-quality, constraint-passing demonstrations are used for few-shot learning in the "student" model.
  • Counterexample Bootstrapping: The system captures traces of failed attempts and their subsequent successful fixes to serve as negative demonstrations, teaching the model to avoid common pitfalls.
Table 1: Comparison of strategies across compilation and assertion phases, illustrating how different methods utilize student and teacher feedback.

Experimental Evaluation

We evaluated the impact of LM Assertions across four diverse tasks derived from the HotPotQA dataset:

  • MultiHopQA: Generating search queries for multi-document information retrieval.
  • LongFormQA: Producing long-form answers with faithful citations.
  • TweetGen: Creating concise, engaging tweets based on retrieved facts.
  • QuizGen: Generating multiple-choice questions in strict JSON format with plausible distractors.

Results

The integration of assertions led to substantial improvements in both intrinsic constraint compliance and extrinsic task performance across all benchmarks.

One of the most striking results was observed in the QuizGen task. As shown in Table 2, the "Vanilla" model struggled significantly with formatting, producing correct JSON only 37.6% of the time on the test set. With the introduction of assertions and bootstrapping (C+Infer w/ Assert), this metric reached 100%. Furthermore, the overall "Validity" of the quizzes—a composite measure of both format and content quality—jumped from 30.5% to 87.2%.

Table 2: Performance metrics for the QuizGen task. Note the dramatic improvement in JSON correctness and overall validity when using assertion-driven strategies.

In the TweetGen task, we observed a similar trend. The ability to generate "Engaging" tweets rose from 2.0% (Compile) to 73.0% (Compile w/ Assert), demonstrating that assertions can effectively guide models toward subjective quality targets. In MultiHopQA, the number of suggestions passed increased by up to 164%, significantly enhancing the reliability of the retrieval pipeline.

Discussion and Conclusion

LM Assertions represent a shift from ad-hoc prompt engineering toward a more disciplined "LM programming" paradigm. By providing a formal mechanism for feedback, the framework reduces sensitivity to initial instruction quality and enables models to learn from their own mistakes. Future work will focus on resolving conflicts between multiple soft constraints and improving the precision of automated feedback to further scale the reliability of self-refining AI systems.

Summary updated

BibTeX
@misc{singhvi2023dspy,
  author = {Arnav Singhvi and Manish Shetty and Shangyin Tan and Christopher Potts and Koushik Sen and Matei Zaharia and Omar Khattab},
  title = {{DSPy Assertions: Computational Constraints for Self-Refining Language Model Pipelines}},
  year = {2023},
  eprint = {2312.13382},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2312.13382},
}
Figure