DSPy Assertions: Computational Constraints for Self-Refining Language Model Pipelines
Open accessAbstract
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 introduce LM 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 to 164% more often and generating up to 37% more higher-quality responses. Our reference implementation of LM Assertions is integrated into DSPy at https://github.com/stanfordnlp/dspy
The claims and statistics of this paper haven’t been checked yet.
Run the check — free, no account needed. It reads the open-access text and checks the sources it cites.
Integrity signals
Registry
Cites
Statistics
Claims the paper makes
The claims and statistics of this paper haven’t been checked yet.
Run the check — free, no account needed. It reads the open-access text and checks the sources it cites.
Claims other papers make about this one
Each is checked against this paper’s own text: does it say what the citing paper says it does?
Some frameworks already experiment with using feedback from assertions or user grades to refine prompts.
“Some frameworks already experiment with using feedback from assertions or user grades to refine prompts [35, 39, 48].”
From Who Validates the Validators? Aligning LLM-Assisted Evaluation of LLM Outputs with Human Preferences (2024) · cites this paper as [48]
Self-check
Percentages, sums and reported differences re-computed from the paper’s own numbers.
The self-check needs the paper’s full text.
Cites
Chain-Of-Thought Prompting Elicits Reasoning in Large Language Models
Wei, J., Wang, X., Schuurmans, D., Bosma, M., Ichter, B., Xia, F., Chi, E., Le, Q. V., & Zhou, D. (2022) · Advances in Neural Information Processing Systems 35 · doi:10.52202/068431-1800 · Cited by 4,505 · see citing works
Reflexion: Language Agents with Verbal Reinforcement Learning
Shinn, N., Cassano, F., Berman, E., Gopinath, A., Narasimhan, K., & Yao, S. (2023) · arXiv · doi:10.48550/arxiv.2303.11366 · Cited by 1,087 · see citing works
Interleaving Retrieval with Chain-of-Thought Reasoning for Knowledge-Intensive Multi-Step Questions
Trivedi, H., Balasubramanian, N., Khot, T., & Sabharwal, A. (2022) · arXiv · doi:10.48550/arxiv.2212.10509 · Cited by 349 · see citing works
Self-Refine: Iterative Refinement with Self-Feedback
Madaan, A., Tandon, N., Gupta, P., Hallinan, S., Gao, L., Wiegreffe, S., Alon, U., Dziri, N., Prabhumoye, S., Yang, Y., et al. (2023) · arXiv · doi:10.48550/arxiv.2303.17651 · Cited by 219 · see citing works
Kahn, G. (2006) · Springer eBooks · doi:10.1007/bfb0039592 · Cited by 200 · see citing works
Bartetzko, D., Fischer, C., Möller, M., & Wehrheim, H. (2001) · Electronic Notes in Theoretical Computer Science · doi:10.1016/s1571-0661(04)00247-6 · Cited by 187 · see citing works
NeMo Guardrails: A Toolkit for Controllable and Safe LLM Applications with Programmable Rails
Rebedea, T., Dinu, R., Sreedhar, M. N., Parisien, C., & Cohen, J. (2023) · Proceedings of the 2023 Conference on Empirical Methods in Natural Language Processing: System Demonstrations · doi:10.18653/v1/2023.emnlp-demo.40 · Cited by 130 · see citing works
Prompting Is Programming: A Query Language for Large Language Models
Beurer-Kellner, L., Fischer, M., & Vechev, M. (2023) · Proceedings of the ACM on Programming Languages · doi:10.1145/3591300 · Cited by 117 · see citing works
Improved Lexically Constrained Decoding for Translation and Monolingual Rewriting
Hu, J. E., Khayrallah, H., Culkin, R., Xia, P., Chen, T., Post, M., & Durme, B. V. (2019) · Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies (NAACL-HLT) · doi:10.18653/v1/n19-1090 · Cited by 116 · see citing works
HotpotQA: A Dataset for Diverse, Explainable Multi-hop Question Answering
Yang, Z., Qi, P., Zhang, S., Bengio, Y., Cohen, W. W., Salakhutdinov, R., & Manning, C. D. (2018) · arXiv · doi:10.48550/arxiv.1809.09600 · Cited by 97 · see citing works
ColBERTv2: Effective and Efficient Retrieval via Lightweight Late InteractionPreprint
Santhanam, K., Khattab, O., Saad-Falcon, J., Potts, C., & Zaharia, M. (2021) · arXiv (Cornell University) · doi:10.48550/arxiv.2112.01488 · Cited by 90 · see citing works
Self-citationLexically Constrained Decoding for Sequence Generation Using Grid Beam SearchPreprint
Hokamp, C., & Liu, Q. (2017) · arXiv (Cornell University) · doi:10.48550/arxiv.1704.07138 · Cited by 34 · see citing works
LLMs cannot find reasoning errors, but can correct them given the error location
Tyen, G., Mansoor, H., Cărbune, V., Chen, P., & Mak, T. (2023) · arXiv · doi:10.48550/arxiv.2311.08516 · Cited by 32 · see citing works
Qiu, L., Jiang, L., Lu, X., Sclar, M., Pyatkin, V., Bhagavatula, C., Wang, B., Kim, Y., Choi, Y., Dziri, N., et al. (2023) · arXiv (Cornell University) · doi:10.48550/arxiv.2310.08559 · Cited by 4 · see citing works
The ART of LLM Refinement: Ask, Refine, and Trust
Shridhar, K., Sinha, K., Cohen, A., Wang, T., Yu, P., Pasunuru, R., Sachan, M., Weston, J., & Celikyilmaz, A. (2023) · arXiv · doi:10.48550/arxiv.2311.07961 · Cited by 1 · see citing works
LLMRefine: Pinpointing and Refining Large Language Models via Fine-Grained Actionable FeedbackPreprint
Xu, W., Deutsch, D., Finkelstein, M., Juraska, J., Zhang, B., Liu, Z., Wang, W. Y., Li, L., & Freitag, M. (2023) · arXiv (Cornell University) · doi:10.48550/arxiv.2311.09336 · Cited by 1 · see citing works
Flagged first, then by citations
Research works printed but not linked: 4
- Brown, T., Mann, B., Ryder, N., Subbiah, M., Kaplan, J. D., Dhariwal, P., Neelakantan, A., Shyam, P., Sastry, G., Askell, A., et al. Language models are few-shot learn- ers. Advances in neural information processing systems, 33:1877-1901, 2020.
- Khattab, O., Potts, C., and Zaharia, M. Baleen: Robust multi-hop reasoning at scale via condensed retrieval. Ad- vances in Neural Information Processing Systems, 34: 27670-27682, 2021.
- Khattab, O., Singhvi, A., Maheshwari, P., Zhang, Z., San- thanam, K., Vardhamanan, S., Haq, S., Sharma, A., Joshi, T. T., Moazam, H., Miller, H., Zaharia, M., and Potts, C. Dspy: Compiling declarative language model calls into self-improving pipelines. ICLR, 2024.
- Yao, S., Yu, D., Zhao, J., Shafran, I., Griffiths, T. L., Cao, Y., and Narasimhan, K. Tree of thoughts: Deliber- ate problem solving with large language models. arXiv preprint arXiv:2305.10601, 2023. DSPy Assertions: Computational Constraints for Self-Refining Language Model Pipelines TweetGen w/ Primitive Instructions (Dev/Test) Strategy No "#" Has Answer Concise Engaging Faithful Quality Vanilla 3.3 / 3.0 53.7 / 48.2 96.3 / 97.0 35.7 / 36.4 80.0 / 81.2 33.7 / 30.4 Infer w/ Assert 49.3 / 49.6 50.3 / 41.8 92.0 / 92.4 45.3 / 41.0 72.3 / 74.0 34.3 / 27.8 Compile 0.0 / 0.2 55.7 / 46.2 100 / 99.6 47.3 / 46.6 78.3 / 76.8 36.7 / 30.8 Compile w/ Assert 98.7 / 97.4 55.0 / 45.8 99.3 / 99.0 1.3 / 2.6 65.3 / 70.0 40.4 / 34.3 C+Infer w/ Assert 41.3 / 41.0 55.7 / 48.2 94.7 / 93.8 54.3 / 60.2 76.7 / 81.2 40.3 / 35.0 · arXiv
Cited by
Sorted by citations
Who Validates the Validators? Aligning LLM-Assisted Evaluation of LLM Outputs with Human Preferences
Shankar, S., Zamfirescu-Pereira, J. D., Hartmann, B., Parameswaran, A., & Arawjo, I. (2024) · Proceedings of the 37th Annual ACM Symposium on User Interface Software and Technology · doi:10.1145/3654777.3676450 · Cited by 151 · see citing works
TextGrad: Automatic "Differentiation" via Text
Yuksekgonul, M., Bianchi, F., Boen, J., Liu, S., Huang, Z., Guestrin, C., & Zou, J. (2024) · arXiv · doi:10.48550/arxiv.2406.07496 · Cited by 12 · see citing works
Fang, J., Peng, Y., Zhang, X., Wang, Y., Yi, X., Zhang, G., Xu, Y., Wu, B., Liu, S., Li, Z., et al. (2025) · arXiv · doi:10.48550/arxiv.2508.07407 · Cited by 6 · see citing works
- Authors
- Singhvi, A., Shetty, M., Tan, S., Potts, C., Sen, K., Zaharia, M., & Khattab, O.
- Venue
- arXiv
- Publisher
- arXiv
- Published
- 2023
- ISSN
- 2331-8422 (ISSN-L)
- arXiv
- 2312.13382
- OpenAlex
- W4390136380
- Cited by
- 2
- References
- 16
- Citations by year
- 2025: 2
- Type
- posted content
- Language
- en
- Open access
- green
- arxiv.org
- Locations
- arXiv (Cornell University) (submittedVersion)
- arXiv (Cornell University)
- Topics
- Physical Sciences › Computer Science › Artificial Intelligence › Natural Language Processing Techniques
- Physical Sciences › Computer Science › Artificial Intelligence › Topic Modeling
- Physical Sciences › Computer Science › Artificial Intelligence › Logic, programming, and type systems
- Keywords
- DSPy, computational resource constraints, self-refinement, prompt engineering, program compilation, text generation, constraint compliance
About these checks
Registry, retraction, Cites and statistics results are computed, not judged, with one exception: a printed reference that no index lists is read once by a model before it counts as a citation error. Claims are extracted by a model and kept only when their quote appears verbatim in the text; each cited source is then checked, with a model’s help, for whether it says what the claim relies on. Those verdicts can be wrong — read the claim’s quote and the cited source before relying on one.
Statistics and arithmetic are recomputed, not judged.
Confidence bands are shown only for fields calibrated on our annotated test set. None is calibrated yet, so no band is shown.
What counts as a claim ›How we measure confidence ›How we check this ›
Are you an author, or do you see a problem with this page? Request a review