Stanford NLP

DSPy

Programming — not prompting — language models.

DSPy lets you write LLM programs as typed Python modules with input/output signatures, then automatically optimizes the prompts and few-shot examples against a metric. Less prompt tweaking, more measurable iteration.

Install

bash
pip install dspy

Quickstart

A minimal example to verify your setup.

python
import dspy

dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))

class QA(dspy.Signature):
    """Answer the question concisely."""
    question: str = dspy.InputField()
    answer: str = dspy.OutputField()

qa = dspy.ChainOfThought(QA)
print(qa(question="Who wrote the Iliad?").answer)

Core concepts

Signatures

Declarative input/output specs replace hand-written prompts. The framework generates the actual prompt for the target model.

Modules

Predict, ChainOfThought, ReAct, and more — composable units you call like normal Python functions.

Optimizers

Teleprompters like BootstrapFewShot and MIPRO search for the best demos and instructions against your metric.

Metrics

Define what 'good' means once; let DSPy compile programs that maximize it across models.

Common use cases

  • Reducing prompt brittleness across model upgrades
  • Multi-hop question answering
  • Structured information extraction
  • Classifier and rater LLM pipelines

Resources