DSPy
An open-source framework from Stanford for programming, rather than manually prompting, language models, using declarative signatures and modules whose prompts and weights are automatically optimised by compilers called optimisers.
DSPy, short for Declarative Self-improving Python, is an open-source framework created by the Stanford NLP group for building applications with language models by programming them rather than manually writing prompts. Instead of handcrafting long and brittle prompt strings, developers describe what each step of a system should do using structured Python code, and DSPy automatically generates and optimises the underlying prompts, and can even fine-tune model weights. The framework was introduced in 2023 and has become a widely used approach for engineering reliable language model pipelines.
The core idea
Conventional development with language models often revolves around prompt engineering, in which a developer iterates by hand on the exact wording of a prompt until the model behaves acceptably. This process is fragile: prompts that work for one model may fail on another, small wording changes have unpredictable effects, and there is no systematic way to improve results. DSPy reframes the problem. It treats a language model pipeline as a program made of well-defined components, and it treats the specific prompts as parameters to be optimised rather than artefacts to be written by hand.
Signatures, modules and optimisers
DSPy is organised around three central abstractions. A signature is a declarative specification of a task in terms of its inputs and outputs, for example stating that a step takes a question and produces an answer, without specifying the exact prompt wording. A module is a reusable strategy for invoking a language model, such as a module that performs chain-of-thought reasoning or one that retrieves supporting documents before answering; modules are parameterised and can be composed into larger pipelines. An optimiser, sometimes called a compiler, takes a program built from modules together with a metric and a small amount of example data, and searches for the prompts, few-shot demonstrations or fine-tuned weights that maximise the metric.
The workflow is therefore: define the pipeline structure with signatures and modules, supply a metric that measures success and some example inputs, then run an optimiser that compiles the program into an effective set of prompts. Because the structure is separate from the prompts, the same program can be re-optimised for a different model without rewriting it.
Benefits and results
The main benefit of DSPy is that it makes language model systems more robust and maintainable. Developers focus on the architecture of a solution rather than on prompt wording, and improvements come from the optimiser rather than from manual trial and error. The Stanford team reported that compiling a short DSPy program allowed smaller models to bootstrap pipelines that outperformed both standard few-shot prompting and pipelines built from expert-written prompt examples. This portability across models and the automation of prompt tuning are the framework's main attractions.
DSPy occupies a distinct place among language model tooling. Frameworks such as LangChain and LlamaIndex focus on orchestrating components, retrieval and integrations, whereas DSPy focuses specifically on optimising the behaviour of the model calls themselves, and the approaches can be used together.
| Concept | Role in DSPy | | --- | --- | | Signature | Declarative input and output specification of a task | | Module | Reusable, parameterised prompting strategy | | Optimiser | Compiles and tunes prompts, examples or weights against a metric |
References
- Khattab, O. et al. (2023). DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines. arXiv:2310.03714.
- Stanford NLP. (2024). DSPy: The framework for programming, not prompting, language models. github.com/stanfordnlp/dspy.
- Stanford HAI. (2024). DSPy: Compiling Declarative Language Model Calls into State-of-the-Art Pipelines. hai.stanford.edu.