Modularity organizes an application as small components with clear inputs, outputs, and purposes. This makes components easier to test, debug, review, and monitor, while allowing one component to be replaced without rewriting the rest. section 1.2.1
In DSPy, modularity supports practical substitutions:
- Swap one language model for another without changing the surrounding code. section 1.2.1
- Replace a prompting technique, such as Predict, with another while keeping the rest of the application intact. section 1.2.1
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 example configures a language model and a Predict component separately, then calls the predictor with a question and context. section 1.2.1









