DSPy (Declarative Self-improving Python) is a framework for programming language models rather than prompting them. Instead of hand-tuning prompt strings, you write compositional Python code that describes what each step of an AI system takes in and produces, and DSPy supplies algorithms that optimize the prompts and weights of the resulting program.
The same approach covers classifiers, retrieval-augmented generation (RAG) pipelines, and agent loops. Programs are built from signatures, which declare the inputs and outputs of a step, and modules, which implement a step and can be composed; a ReAct module adds tool use. A metric scores program outputs, and an optimizer such as GEPA uses it to improve the program, after which the compiled program can be saved and loaded.
The framework grows out of the Demonstrate-Search-Predict work on composing retrieval and language models, and its optimization methods are described in papers including DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines (ICLR 2024), Optimizing Instructions and Demonstrations for Multi-Stage Language Model Programs, and GEPA: Reflective Prompt Evolution Can Outperform Reinforcement Learning.
Features
- Signatures: inline or class-based declarations of a step's input and output fields
- Modules: composable building blocks for LM calls that you swap and combine to change program behavior
- Tools with ReAct: an agent module that calls tools as part of its reasoning loop
- Metrics: scoring functions that define what a good output looks like for optimization
- Optimizers: algorithms such as GEPA that tune instructions, demonstrations, and weights against a metric
- Saving and loading: persist a compiled program and reload it for inference



