DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines
By Omar Khattab, Arnav Singhvi, Paridhi Maheshwari, Zhiyuan Zhang, Keshav Santhanam, Sri Vardhamanan, Saiful Haq, Ashutosh Sharma, Thomas T. Joshi, Hanna Moazam and 3 more. 2023
867 citations recorded by the source corpus.
The ML community is rapidly exploring techniques for prompting language models (LMs) and for stacking them into pipelines that solve complex tasks. Unfortunately, existing LM pipelines are typically implemented using hard-coded"prompt templates", i.e. lengthy strings discovered via trial and error. Toward a more systematic approach for developing and optimizing LM pipelines, we introduce DSPy, a programming model that abstracts LM pipelines as text transformation graphs, i.e. imperative computational graphs where LMs are invoked through declarative modules. DSPy modules are parameterized, meaning they can learn (by creating and collecting demonstrations) how to apply compositions of prompting, finetuning, augmentation, and reasoning techniques. We design a compiler that will optimize any DSPy pipeline to maximize a given metric. We conduct two case studies, showing that succinct DSPy programs can express and optimize sophisticated LM pipelines that reason about math word problems, tackle multi-hop retrieval, answer complex questions, and control agent loops. Within minutes of compiling, a few lines of DSPy allow GPT-3.5 and llama2-13b-chat to self-bootstrap pipelines that outperform standard few-shot prompting (generally by over 25% and 65%, respectively) and pipelines with expert-created demonstrations (by up to 5-46% and 16-40%, respectively). On top of that, DSPy programs compiled to open and relatively small LMs like 770M-parameter T5 and llama2-13b-chat are competitive with approaches that rely on expert-written prompt chains for proprietary GPT-3.5. DSPy is available at https://github.com/stanfordnlp/dspy
Subfields: Artificial Intelligence, Information Systems, Large Language Models, Code Generation & Program Synthesis
How researchers see this paper
The researchers whose trusted networks rank it highest, strongest advocate first. Every researcher gets a different order.
In Artificial Intelligence
- Kalpesh Krishna
- Satyapriya Krishna
- Manaal Faruqui
- Anhad Mohananey
- Chris Dyer
- James R. Glass
- Yoshua Bengio
- Mohit Bansal
In Information Systems
In Large Language Models
Citations in the corpus
- Fact, Fetch, and Reason: A Unified Evaluation of Retrieval-Augmented Generation (2024, methodology): “We can explore process supervision methods like those used in PRM-800K (Lightman et al., 2023), or investigate distillation techniques on successful trajectories, similar to approaches in ToolFormer (Schick et al., 2024) and DSPy (Khattab et al., 2023).”
- Quiet-STaR: Language Models Can Teach Themselves to Think Before Speaking (2024, neutral): “We also visualize the distribution of improvements across tokens in the training set.”
- SGLang: Efficient Execution of Structured Language Model Programs (2023, neutral): “Multi-turn chat (short) means short output (4-8 tokens) and multi-turn chat (long) means long output (256-512 tokens); DSPy retrieval-augmented generation (RAG) pipeline [20] in its official example.”
- Why Do Multi-Agent LLM Systems Fail? (2025, neutral): “DSPy (Khattab et al., 2023) tackles issues in programming agentic flows, while StateFlow (Wu et al., 2024b) focuses on state control within agentic workflows to improve task-solving capabilities.”