Chaining language model (LM)  calls as composable modules is fueling a new way of programming, but ensuring LMs adhere to important constraints requires heuristic “prompt engineering.” We introduceLM Assertions, a programming construct for expressing computational constraints that LMs should satisfy. We integrate our constructs into the recent DSPy programming model for LMs and present new strategies that allow DSPy to compile programs with LM Assertions into more reliable and accurate systems. We also propose strategies to use assertions at inference time for automatic self-refinement with LMs.
We report on four diverse case studies for text generation
and find that LM Assertions improve not only compliance with imposed rules but also downstream task performance, passing constraints up tomore often and generating up tomore higher-quality responses.