DSPy — Programming Language Models
Declarative signatures, modules & compilers
The Problem: You spent hours crafting the perfect prompt. Then the model updated, and it broke. You switched to a different LLM — broken again. How do you build prompts that survive change?
The Solution: Program, Don't Prompt
DSPy (Declarative Self-improving Python) is a framework that replaces fragile hand-written prompts with declarative programs. You define signatures — what input goes in and what output comes out — then choose a reasoning module (Predict, ChainOfThought, ReAct), and a compiler automatically optimizes the prompt with the best few-shot examples and instructions for your specific data and metric.
Think of it like an ORM for databases:
- 1. Define signature: Declare input -> output fields: "context, question -> answer"
- 2. Choose module: Pick a reasoning pattern: Predict, ChainOfThought, ReAct, or ProgramOfThought
- 3. Compile with optimizer: BootstrapFewShot or MIPRO auto-selects best examples and instructions using your metric
- 4. Deploy compiled program: The optimized program works with any LLM — portable and reproducible
Where Is This Used?
- NLP Pipelines: Chain multiple modules (summarize -> classify -> extract) with auto-optimized prompts at each step
- RAG Optimization: Automatically optimize retrieval queries and generation prompts together for better answers
- Multi-hop QA: Complex questions requiring multiple reasoning steps — DSPy chains modules and optimizes the full pipeline
- Classification: Auto-optimized prompts with best few-shot examples selected by the compiler for your specific data
Fun Fact: DSPy was created by the same Stanford NLP team behind ColBERT and Baleen. The name "DSPy" is a play on PyTorch's nn.Module approach — signatures are like tensor shapes, and modules are like neural network layers.
Try It Yourself!
Explore the interactive visualization below to see how DSPy transforms a manual prompt into a compiled, optimized program.
DSPy: Programming Language Models
Explore signatures, modules, optimizers and compilation
"context, question -> answer"
ChainOfThought(QA)
BootstrapFewShot + metric
Auto-optimized with best examples
Try it yourself
Interactive demo of this technique
Build a question-answering system that takes a context paragraph and a question, then returns a precise answer
Based on the context provided, Python was first released in 1991. It was created by Guido van Rossum and is known for its emphasis on code readability, particularly through the use of significant whitespace.
Reasoning: The context directly states that Python was "first released in 1991." Answer: 1991
The manual prompt produced a correct but verbose answer with unnecessary paraphrasing. The DSPy-compiled prompt produced a concise, focused answer because the compiler selected demonstrations that teach the model the desired output format.
Create a free account to solve challenges
3 AI-verified challenges for this lesson
This lesson is part of a structured LLM course.
My Learning Path