Back to Glossary
Prompting

DSPy

Definition

DSPy is a framework for programmatically optimizing LLM prompts and pipelines, replacing manual prompt engineering with automated optimization based on metrics and examples.

Why It Matters

Manual prompt engineering is time-consuming and often produces suboptimal results. DSPy treats prompts as learnable parameters that can be optimized automatically. Given examples and metrics, DSPy finds prompts that perform better than hand-crafted ones, often significantly so.

How It Works

DSPy uses three key abstractions:

  • Signatures: Define input/output behavior (e.g., “question -> answer”)
  • Modules: Composable building blocks (ChainOfThought, ReAct, etc.)
  • Optimizers: Automatically improve prompts based on training data

You define what you want, provide examples, and DSPy optimizes how to achieve it.

When to Use

Use DSPy when: you have labeled examples to optimize against, manual prompt iteration isn’t yielding good results, you’re building complex LLM pipelines, or you need reproducible optimization. For simple tasks or rapid prototyping, traditional prompting may be faster to get started.

Source

DSPy introduces a programming model that abstracts prompting into declarative modules, enabling systematic optimization of LLM programs.

https://arxiv.org/abs/2310.03714