liveBook · Manning

DSPy

As explained in Building LLM Applications with DSPy

The programmatic interface for defining and executing DSPy language-model operations.

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

How Building LLM Applications with DSPy covers it

How deep it goes
Works through model configuration and a Predict call, then outlines baseline evaluation and iterative optimization without optimizing the introductory example.
Examples
Uses question answering with answer and confidence fields, a scored customer-service intent classifier, and a poem-rewriting signature.
What it assumes
Assumes enough Python to follow a short script and an LM API key; introduces declarative operations without requiring hand-written prompts.
What you can do after
Apply it via DSPy to define and run an LM operation, then evaluate a baseline before optimizing prompts when quality warrants it.
How it connects
Connects signatures and Predict modules to prompt programming, language-model evaluation, prompt optimization, retrieval-augmented generation, and agentic workflows.

Also explained in

Compare how 3 books explain DSPy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.