A language-model application can work well on one input and miss a crucial fact on the next. Rewording the prompt after each failure quickly becomes slow and hard to maintain, especially when testing multiple models. DSPy—Declarative Self-improving Python—offers a programmatic alternative: specify the task, its inputs and outputs, and how success will be evaluated rather than hand-writing every prompt. section 1.0 section 1.2
A DSPy program separates the language model from the operation it performs. A signature describes fields flowing into and out of an operation; a module such as `Predict` executes it. This makes it practical to swap a model or prompting technique without rewriting the rest of the application. A baseline can run without optimization; prompt optimization is a subsequent step, not a requirement for every call. section 1.2.1 section 1.8
Listing 1.1 DSPy example
import dspy
OPENAI_API_KEY = [indicate your API key]
lm = dspy.LM("openai/gpt-4o-mini", api_key=OPENAI_API_KEY)
dspy.configure(lm=lm)
predictor = dspy.Predict("question, context -> answer, confidence")
prediction = predictor(question="What is the capital of France?", context="")
print(prediction.answer, prediction.confidence)
The question-answering listing configures an LM, creates `dspy.Predict("question, context -> answer, confidence")`, and reads the returned answer and confidence. Its API key is a placeholder. Notice that the program contains no hand-written prompt and does not optimize one. section 1.2.1
For a task where response quality matters, use a measurable development loop:
- Define the operation and run a baseline. section 1.8
- Evaluate its outputs against a task-specific measure. section 1.0
- Optimize by generating and evaluating candidate prompts, potentially over several iterations, then retain the strongest candidate. section 1.1.2 section 1.8
Intent classification
For a customer-service intent classifier, candidate prompts can be scored against the same task. In the illustrated comparison, the second of three candidates scores highest at 90% and is selected. The point is not that every task reaches that score, but that prompt selection can follow evaluation rather than guesswork. section 1.1.2
DSPy is particularly useful for long-running applications, complex workflows, or prompts that must perform reliably. Optimizing for a smaller LM may also make a less expensive model viable. For casual interactions where response quality is not critical, direct prompting may be enough; DSPy still requires learning its syntax and writing code. section 1.2.7 section 1.4


