DSPy Explained: Programming, Not Prompting, Language Models

What DSPy is, how it differs from hand-writing prompts, and a basic example of defining a module and letting DSPy optimize it

This guide was checked against DSPy 3.5.0 on 14 November 2026. Most LLM frameworks give you a nicer way to write prompt strings. DSPy takes a different position entirely: prompts shouldn’t be hand-written at all — they should be compiled, the same way you don’t hand-write assembly when you write Python.

The core idea

You define what a task needs to do in Python — inputs, outputs, and a signature describing their relationship — not the exact wording of the prompt that accomplishes it. DSPy’s compiler then generates and iteratively optimizes the actual prompt text against a metric you specify, using your training examples. When the underlying model changes, or the task shifts slightly, you re-optimize instead of manually rewriting prompt strings and hoping the new phrasing still works.

A basic example

import dspy
from dspy.utils import DummyLM

# In real use: dspy.configure(lm=dspy.LM("<provider>/<model>")). DummyLM returns
# a canned answer so this example runs offline.
dspy.configure(lm=DummyLM([{"answer": "Paris"}]))


class AnswerQuestion(dspy.Signature):
    """Answer questions with short, factual responses."""
    question: str = dspy.InputField()
    answer: str = dspy.OutputField()

predictor = dspy.Predict(AnswerQuestion)
result = predictor(question="What is the capital of France?")
print(result.answer)  # Paris

The Signature describes the task’s shape, not its wording. Nothing runs until you configure a model with dspy.configure(lm=dspy.LM(...)); without one, calling the predictor raises ValueError: No LM is loaded. dspy.Predict (or more advanced modules like dspy.ChainOfThought) handles turning that into an actual prompt sent to the configured model — and DSPy’s optimizers (BootstrapFewShot, MIPROv2, GEPA and SIMBA all exist in DSPy 3.5.0) can later tune that prompt automatically against a dataset of examples and a scoring function, without you touching the prompt text yourself.

Why this matters in practice

Hand-tuned prompts are brittle: a prompt tuned carefully against one model often needs re-tuning for another, or even for a new version of the same model family. Because DSPy treats the prompt as a compiled artifact rather than source code you maintain, switching models or improving accuracy becomes a re-optimization run rather than a manual rewrite — a meaningfully different maintenance story for any LLM application expected to survive more than one model generation.

When to reach for it

DSPy adds real complexity — you need training examples and a metric, which is more setup than string-formatting a prompt template. It’s worth that cost once you’re maintaining a prompt that keeps breaking as requirements or models shift, or building a pipeline with multiple chained LLM calls where manually tuning each step’s prompt in isolation stops working. For a one-off script calling an LLM once, a plain prompt string is still the right amount of engineering.

Frequently Asked Questions

What is DSPy?

DSPy is a framework that treats LLM prompts as compiled artifacts rather than hand-written strings. You define a task’s inputs, outputs, and signature in Python, and DSPy’s compiler generates and optimizes the actual prompt text against a metric you specify.

How does DSPy differ from hand-writing prompts?

Hand-tuned prompts are brittle — a prompt tuned for one model often needs re-tuning for another. DSPy treats the prompt as something to re-optimize via a compiler run rather than manually rewrite, which matters for applications expected to survive more than one model generation.

When should you use DSPy instead of a plain prompt string?

Once you’re maintaining a prompt that keeps breaking as requirements or models shift, or building a pipeline with multiple chained LLM calls. For a one-off script calling an LLM once, a plain prompt string is still the right amount of engineering.

What does dspy.Predict actually do?

It takes a Signature describing a task’s shape (not its wording) and turns it into an actual prompt sent to the language model you configured with dspy.configure(lm=…). Without a configured model it raises an error. DSPy’s optimizers (such as MIPROv2 and GEPA) can later tune that prompt automatically against training examples and a scoring function.