# DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines

**Authors:** Omar Khattab, Arnav Singhvi, Paridhi Maheshwari, Zhiyuan Zhang, Keshav Santhanam, Sri Vardhamanan, Saiful Haq, Ashutosh Sharma, Thomas T. Joshi, Hanna Moazam, Heather Miller, Matei Zaharia, Christopher Potts  
**arXiv:** [2310.03714](https://arxiv.org/abs/2310.03714)  
**Date:** 2023-10-05  
**Summary page:** https://www.summarizepaper.com/en/paper/2310.03714v1/

## Key points

- The researchers are exploring techniques for training language models (LMs) to solve complex tasks.
- Existing methods rely on hard-coded "prompt templates" discovered through trial and error.
- DSPy is a programming model that abstracts LM pipelines as text transformation graphs.
- LMs are invoked through declarative modules in DSPy, which can learn how to apply compositions of prompting, finetuning, augmentation, and reasoning techniques by creating and collecting demonstrations.
- A compiler has been designed to optimize DSPy pipelines based on a given metric.
- Two case studies demonstrate the effectiveness of DSPy programs in expressing and optimizing sophisticated LM pipelines.
- DSPy programs can reason about math word problems, tackle multi-hop retrieval tasks, answer complex questions, and control agent loops.
- DSPy pipelines outperformed standard few-shot prompting by over 25% and 65% in two case studies.
- DSPy programs remained competitive with approaches relying on expert-written prompt chains for proprietary GPT-3.5 even when compiled for open and relatively small LMs like the 770M-parameter T5 and llama2-13b-chat models.

## Summary

The  is actively exploring techniques for  and building  to solve complex tasks. However, existing  often rely on hard-coded "prompt templates" discovered through trial and error. To address this limitation, researchers have introduced DSPy, a programming model that abstracts LM pipelines as text transformation graphs. In DSPy, LMs are invoked through declarative modules that can learn how to apply compositions of prompting, finetuning, augmentation, and reasoning techniques by creating and collecting demonstrations. To optimize DSPy pipelines, the researchers have designed a compiler that maximizes a given metric. They conducted two case studies to demonstrate the effectiveness of DSPy programs in expressing and optimizing sophisticated LM pipelines. These pipelines can reason about math word problems, tackle multi-hop retrieval tasks, answer complex questions, and control agent loops. Remarkably, within minutes of compiling a few lines of DSPy code, and  were able to self-bootstrap pipelines that outperformed standard few-shot prompting by over 25% and 65%, respectively. Moreover, surpassed expert-created demonstrations by up to 5-46% and 16-40%, respectively.<br>
<line-break>
Even when compiled for open and relatively small LMs like the 770M-parameter T5 and llama2-13b-chat models,< kd>DSPy programs </ kd > remained competitive with approaches relying on expert-written prompt chains for proprietary GPT-3.5.
