DSPy
By Stanford NLP
DSPy is an open-source Python framework from Stanford NLP for programming, rather than manually prompting, language model pipelines, letting developers define pipeline steps as declarative modules with typed input and output signatures and…
Definition
DSPy is an open-source Python framework from Stanford NLP for programming, rather than manually prompting, language model pipelines, letting developers define pipeline steps as declarative modules with typed input and output signatures and then automatically optimize the underlying prompts and few-shot examples using data and metrics. Its optimizers, called teleprompters, search for effective prompts the way a gradient-based optimizer tunes neural network weights, but operate over prompts and examples instead of differentiable parameters.
Overview
DSPy's central premise is that hand-writing and hand-tuning prompts for a multi-step LLM pipeline is brittle and does not scale well as models, tasks, or requirements change. Instead, developers describe what each step of a pipeline should accomplish using a signature, essentially a typed specification of inputs and outputs, and compose these signatures into modules that can be chained together, similar in spirit to defining layers in a neural network framework. Once a pipeline is defined, DSPy's optimizers, referred to as teleprompters, automatically search for effective prompts and few-shot demonstrations for each module, using a training set of examples and a metric function that scores pipeline outputs. This shifts the work of prompt engineering from manual trial and error to an automated, data-driven optimization process, conceptually similar to how gradient-based optimizers tune neural network weights, though DSPy's optimization operates over prompts and examples rather than differentiable parameters. This approach is particularly useful when a pipeline needs to be adapted to a new underlying model, since re-running the optimization process against the new model can produce a new set of effective prompts without a developer manually rewriting them by hand for each swap. It also helps when a pipeline has several chained LLM calls, where manually tuning each step's prompt while accounting for interactions between steps becomes difficult to do by hand. DSPy has a steeper learning curve than writing prompts directly, since it introduces its own vocabulary of signatures, modules, and optimizers, and effective use requires assembling representative training examples and a meaningful metric to guide optimization. Teams with simpler, single-call use cases may not need this level of infrastructure. The framework originated from academic research and has been adopted for building complex, multi-step LLM pipelines such as retrieval-augmented question answering and multi-hop reasoning systems, where automated prompt optimization offers a clearer benefit over manual tuning. Because DSPy's optimization is only as good as the metric guiding it, a substantial part of adopting the framework well involves the less glamorous work of defining a metric function that actually captures what "good" output means for a given task, which can be as much effort as writing prompts manually would have been for a simple pipeline. Teams tend to see the clearest benefit on pipelines with several chained LLM calls, where the interaction effects between steps make manual prompt tuning genuinely difficult to reason about by hand, rather than on single-call tasks where direct prompt engineering remains simpler and faster to iterate on. Because optimization runs consume LLM calls against the training set, there is a real cost and time investment to each optimization pass, and teams typically budget for periodic re-optimization when swapping models or when task requirements shift meaningfully, rather than treating a compiled pipeline as permanently finished.
Key Features
- Declarative signatures defining typed inputs and outputs for pipeline steps
- Composable modules that chain multiple LLM calls into a pipeline
- Automated prompt and few-shot example optimization via teleprompters
- Data-driven optimization using training examples and metric functions
- Ability to re-optimize a pipeline automatically when swapping models
- Designed for complex, multi-step reasoning and retrieval pipelines
- Open-source framework originating from Stanford NLP research